So im just starting to learn assembly and I got this exercise to write a code that takes an input from user of 3 digit number, adds 999 and prints the result of the 4 digit number. (Example: User inputs 100, code does math adds 999, prints 1099)
Here is what I came up with:
section .data
numToAdd db "999",0x00
userPrompt db "Please enter a 3 digit number (000-999):", 0x0a, 0x00
len_userPrompt equ $-userPrompt
section .bss
userNum resb 4 ; user input: (3 digit num + \n)
resultNum resb 4 ; Result with a 4 digit number
section .text
global main
main:
mov eax, 0x04
mov ebx, 0x01
mov ecx, userPrompt
mov edx, len_userPrompt
int 0x80
mov eax, 0x03
mov ebx, 0x00
mov ecx, userNum
mov edx, 4
int 0x80
mov edx, 0x02 ; Digit counter
mov ecx, 0x03 ; No. of times to loop 3 digits - 3 looops
clc
loop_here:
mov al, [numToAdd+edx] ; Moves: 99|9 -> AL
adc al, [userNum+edx] ; Adds: AL(9) + 3rd number\n
aaa ; Adjust after ADC ( It just adds 96h ? )
pushf
add al, 0x30
popf
mov [resultNum+edx], al ; Moves the result to the corresponding index in resultNum
dec edx
loop loop_here
mov eax, 0x04
mov ebx, 0x01
mov ecx, resultNum
mov edx, 0x04
int 0x80
mov eax, 0x01
int 0x80
The problem persists in this line mov [resultNum+edx], al ; Moves the result to the corresponding index in resultNum
Since it will grab only AL which is 2 Bytes and put it in the resultNum var. meaning if I add 9+1, which is 39h+31h=6ch AAA will return 102 than I add 30h to al will return 132. The command above will only take '32' and put in the resultNum memory location and not the full representation of the real result (which is 10)
To summarize what is the problem here.
TL;DR: I need to add 999 decimal to any 3 digit number from user input and print the real math result to stdout.