after I've gotten some answers yesterday i was able to progress a bit but still can't figure out what's wrong, sorry if this is too much posting
im pretty new to coding and I have to do the following:
Write a program that wants you to input your ID-Card-Number (It's a 9 digit long Number, it can contain either Letters from A to Z or Numbers from 0 to 9) (Its based on ascii and apparently A=10, B=11 etc.)
the positions of the input are weighted differently (Pos 1,4,7 = weight 7; Pos 2,5,8 = weight 3; Pos 3,6,9 = weight 1)
So each digit is either multiplied by 7,3 or 1 All products are added up and the last digit of the sum of all products is the desired check-digit. (these calculations have to be in a subprogram)
The program should then output the input number and the checkdigit at the end so the output should be a 10 digit number (e.g. T22000129 (input) = T220001293 (output))
global main
extern printf
section .text
main:
mov eax, [esp+4] ; argc
cmp eax, 2 ; 2 parameters = 1 argument
jne badCommand
mov eax, [esp+8] ; argv
mov eax, [eax+4] ; starting adress string
push eax
call checksum ; calling checksum
xor eax, eax
mov eax, edx
xor edx, edx ; getting the last digit of the value thats saved in edx
mov ebx, 10
div ebx
xor eax, eax
pop eax
;; output ID Card number (input) + checkdigit ;;
push edx ; check digit
push eax ; string - ID-card-number
push stringFormat
call printf
add esp, 12 ; esp 3DWORD
ret ; end main
;; subprogram ;;
checksum:
push ebx
push esi ; Call-Saved Register - safe before use
push edi
mov esi, [esp+16] ; String-Adresse - get the parameter
mov edi, 9 ; jump counter
start:
xor ebx, ebx
cmp edi, 0
je finish
mov al, [esi]
cmp al, 65
jge letterweight
jl numberweight
letterweight:
cmp edi, 9
je letterseven
cmp edi, 8
je letterthree
cmp edi, 7
je letterone
cmp edi, 6
je letterseven
cmp edi, 5
je letterthree
cmp edi, 4
je letterone
cmp edi, 3
je letterseven
cmp edi, 2
je letterthree
cmp edi, 1
je letterone
numberweight:
cmp edi, 9
je numberseven
cmp edi, 8
je numberthree
cmp edi, 7
je numberone
cmp edi, 6
je numberseven
cmp edi, 5
je numberthree
cmp edi, 4
je numberone
cmp edi, 3
je numberseven
cmp edi, 2
je numberthree
cmp edi, 1
je numberone
letterseven:
sub al, 55
mov ebx, 7
mul ebx
mov ecx, eax
add edx, ecx
jmp ediesi
numberseven:
mov ebx, 7
mul ebx
mov ecx, eax
add edx, ecx
jmp ediesi
letterthree:
sub al, 55
mov ebx, 3
mul ebx
mov ecx, eax
add edx, ecx
jmp ediesi
numberthree:
mov ebx, 3
mul ebx
mov ecx, eax
add edx, ecx
jmp ediesi
letterone:
sub al, 55
mov ebx, 1
mul ebx
mov ecx, eax
add edx, ecx
jmp ediesi
numberone:
mov ebx, 1
mul ebx
mov ecx, eax
add edx, ecx
jmp ediesi
ediesi:
dec edi
inc esi
jmp start
finish:
pop ebx
pop esi ; Call-Saved Register - pop after use
pop edi
ret
badCommand:
push badArgumentFormat
call printf
add esp, 4 ; esp 1DWORD
ret
section .data
badArgumentFormat:
db 'bad argument', 10, 0 ; 10 = LF
stringFormat:
db '"%s%1d"', 10, 0
I know this might not be the most optimal way of doing it but it kinda does what it should but it also doesnt.
My thought process maybe:
main function loads the input into eax so it can be the first part of the output later
pushing eax so i can work with it calling the subprogram to calculate
subprogram: pushing the call-saved registers before I use them in the subprogram
moving the input to esi so I can work with it setting edi to 9 so i can use it as a jump counter
setting ebx to 0 so i can work with it comparing edi with 0, if thats the case program ends
then I'm moving the first digit of the input into al comparing it to 65 because since A=65 in ascii its the first value that would be a letter
jumping to weight determination so I know whats the multiplier once i figured out what multiplier it is, it jumps in either the letter or number calculating scheme
if its a letter it does the following: subtract 55 from the value of digit from the input that i stored in al (do this because if it jumped to letter then the value must be atleast 65 so subtracting 55 will give me the value of the letter (A=65-55 = 10)
im then moving the multiplier into ebx each time so i can mul eax with ebx which is then stored into ecx ecx is then added to edx
after this I jump down to dec the jump counter (edi) and also inc esi so it gives me the value of the 2nd digit from my input
I do this until my edi is 0, after this I return back to the main program
the part starting and ending with xor eax,eax is giving me the last digit of the sum of the products
after this I pop eax to get my input back
printing eax and edx
Im sorry for the long write-up but for some reason I can't figure out why my results are wrong.
The output for my check digits with the code from above is: First time running the program its T220001297 Second time: T220001297 Third: T220001291 Fourth: T220001291 Fifth: T220001299
I'd appreciate any help, something in my thought process is wrong but I just can't figure out what it is.