Search code examples
assemblyadditionfasm

Addition of two numbers from user input using FASM


I have successfully got the user's input and placed into bl and bh, but when i attempt to add them together, the result is not in decimal form even if I already added '0' to the result. Any ideas on how to solve this problem? Thanks

org 100h

mov ah, 9
mov dx, msg
int 21h

mov ah, 1
int 21h

mov [number], al
mov bl, [number]

mov ah, 2
mov dl, 0ah
int 21h

mov ah, 9
mov dx, msg2
int 21h

mov ah, 1
int 21h

mov [number], al
mov bh, [number]

add bh, bl
add bh, 30h

mov ah, 2
mov dl, 0ah
int 21h

mov ah, 9
mov dx, sum
int 21h

mov ah, 2
mov dl,bh
int 21h

mov ax, 4C00h
int 21h

msg db 'Enter first number: ', 24h
msg2 db 'Enter second number: ',24h
sum db 'The sum is: ',24h
number db 0

Solution

  • You seems to misunderstood the relation between ASCII codes and values.

    Assume the user input 34. With your code, you set bl to the ASCII value of the character 3 (which is 0x33) and bh to the ASCII value of the character 4 (0x34).

    Now, to add them together, you need to convert them into values. By subtracting 0x30.

    sub bl, 30h
    sub bh, 30h
    

    Now bl = 3, bh = 4. just add them together.

    add bh, bl
    

    Now bh = 7. Convert it to ASCII value of the digit and display it to the user. That part you already did it correctly.

    add bh, 30h
    ...
    

    In summary:

    • After read a digit, subtract it by 30h.
    • Before printing a digit, add 30h to it.