I managed to make the addition of two-digit numbers based on the solutions provided by helpful people from the thread I created last time:
How do I use ADC in assembly?
So now, there seems to be a problem when I add 2 numbers and their result will be a 3-digit number. The jump named IS_3DIGIT handles that possibility, but addition of some numbers like 80 + 80, 99 + 99, 89 + 82 all give wrong results. 56 + 77 works well. So my hypothesis is that adding two numbers higher than 79 will give wrong results. How can I resolve this problem? BTW, additions like 99 + 23 or 89 + 43 give correct results.
.MODEL SMALL
.STACK 1000
.DATA
MSGA DB 13,10,"Input first number: ","$"
MSGB DB 13,10,"Input second number:","$"
MSGC DB 13,10,"The sum is: ","$"
NUM1 db ?
NUM2 db ?
NUM3 db ?
.CODE
MAIN PROC NEAR
MOV AX, @DATA
MOV DS, AX
; get first number
LEA DX, MSGA
MOV AH, 09h
INT 21h
MOV AH, 01
INT 21H
SUB AL, '0'
MOV BL, AL
MOV AH, 01
INT 21H
SUB AL, '0'
MOV CL, AL
; get second number
LEA DX, MSGB
MOV AH, 09h
INT 21h
MOV AH, 01
INT 21H
SUB AL, '0'
MOV DL, AL
MOV AH, 01
INT 21H
SUB AL, '0'
MOV DH, AL
; add
MOV AL, CL
MOV AH, BL
ADD AL, DH
AAA
ADD AH, DL
MOV NUM1, AL
ADD NUM1, '0'
; if tens digit is less than or equal to 9
CMP AH, 9
JLE NOT_3DIGIT
IS_3DIGIT:
MOV AL, AH ; move value of ah to al
SUB AH, AH ; clear ah
ADD AL, 0 ; al + 0 = al (tens digit)
AAA ; move for addition
ADD AH, 0 ; ah + 0 + 1 = ah + 1 (hundreds digit)
MOV NUM2, AL
MOV NUM3, AH
ADD NUM2, '0'
ADD NUM3, '0'
; output sum
LEA DX, MSGC
MOV AH, 09h
INT 21h
MOV DL, NUM3
MOV AH, 02H
INT 21h
MOV DL, NUM2
MOV AH, 02H
INT 21h
JMP PRINT_LASTDIGIT
NOT_3DIGIT:
MOV NUM2, AH
ADD NUM2, '0'
; output sum
LEA DX, MSGC
MOV AH, 09h
INT 21h
MOV DL, NUM2
MOV AH, 02H
INT 21h
PRINT_LASTDIGIT:
MOV DL, NUM1
MOV AH, 02H
INT 21h
EXIT:
MOV AH, 4Ch
INT 21h
MAIN ENDP
END MAIN
Well you did it the "hard way", really. Again, aaa
can do all the hard work, as long as the right things are in ah
and al
, so different cases for overflow and no overflow aren't really necessary.
Something like this (untested):
; ah:al = tens:ones
add al,dh
aaa
; now make ah:al hundres:tens
mov bl,al
mov al,ah
xor ah,ah ; this will be the hundreds digit
add al,dl
aaa
; result in ah:al:bl
I took the use of dh
and dl
from your source, and bl
is just some extra place. They're not important, but the things in ah
and al
really have to be there, just as last time.
I've read the descriptions of aaa
and add
very closely, and I think that should work even though the second add can be adding 10 to something, but I'm only about 90% sure about that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With