1

I'm trying to sum 3 numbers with 2 digits. I'm doing a program that asks for the 3 numbers but I have no idea how to add them.
Example:

99 + 99 + 99 = 270 (Example)

This is my program where I ask for the numbers but the sum is wrong. How can I fix it?

.model small
.stack 64
.data
.code
M1 DB 10,13,' First number : $'
M2 DB 10,13,' Second number : $' 
M3 DB 10,13,' Third number : $'
M4 DB 10,13,' Result : $'     
b1 db 0  
b2 db 0 
b3 db 0
b4 db 0
.startup
inicio:

;------ask first number-------
mov AH, 9
lea DX, M1
int 21h
mov ah,01h
int 21h 
sub al,30h 
mov b1,al
mov ah,01h
int 21h  
sub al,30h 
mov b2, al


;------ask second number-------
mov AH, 9
lea DX, M2
int 21h
mov ah,01h
int 21h 
sub al,30h 
mov b3,al
mov ah,01h
int 21h  
sub al,30h 
mov b4, al



;------Ascii adjustment for the sum------
aaa
mov bx,ax
sub bh,01h

;------ask third number------------

mov AH, 9
lea DX, M2
int 21h
mov ah,01h
int 21h
sub al,30h
mov bl,al
mov ah,01h
int 21h
sub al,30h
add al,bl
mov b1, bh
add al, bl 
;mov ah, 02h
;mov dl, bl 
;int 21h 


;------Ascii adjustment for the sum------
aaa
mov bx,ax
sub bh,01h  
add bh, b1  
or bx,3030h
;------High part sum printing-----------

mov ah, 9
lea dx, m4
int 21h
mov ah,02h
mov dl,bh
int 21h

;-----low print-----------

mov ah,02h
mov dl,bl
int 21h

;exit:
;mov ah,04ch
;int 21h

.exit
end
Fifoernik
  • 9,779
  • 1
  • 21
  • 27
  • 1
    Please learn how to format code so that it is actually readable. There is a nice tutorial, BTW. – U. Windl Feb 19 '19 at 22:54
  • This isn't a [mcve], you don't show what actually happens when you run this, or any register values from single-stepping with a debugger. But using `aaa` after getting the 2nd number looks wrong, because the last flag-setting instruction was `sub al, 30h`. (Not an ADD of two unpacked-BCD digits into AL, with a high digit in AH.) – Peter Cordes Feb 19 '19 at 23:03
  • Normally you don't want to use BCD stuff anyway, and convert decimal strings to binary integers. See https://stackoverflow.com/tags/x86/info for the FAQ entry about multi-digit numbers. – Peter Cordes Feb 19 '19 at 23:06
  • 2
    "99 + 99 + 99 = 270" How is this correct? Should be __297__. – Fifoernik Feb 21 '19 at 11:59

1 Answers1

1

First input the numbers

Instead of storing the individual digits of the 1st number in b1 and b2, you should combine them in order to obtain the true value of the 1st number.
A nice instruction that can do this is aad. Put the first inputted digit in AH, put the secondly inputted digit in AL, then let aad do AH * 10 + AL.

;------ask first number-------
mov AH, 09h
lea DX, M1
int 21h
mov ah, 01h
int 21h 
sub al, 30h 
mov b1, al
mov ah, 01h
int 21h  
sub al, 30h
mov ah, b1
aad 
mov b1, al      ; b1 is now a number from 0 to 99

Act similarly for the 2nd and 3rd numbers.

Then sum them up (result = b1 + b2 +b3)

The sum of b1 and b2 can never be more than 198 and so it will fit in a byte.

mov al, b1
add al, b2

but adding the 3rd number could overflow the byte range (0-255) and so we have to deal with the carry

mov ah, 0
add al, b3
adc ah, ah

Now AX holds a number from 0 to 297.

To display this (at most) 3-digit number read Displaying numbers with DOS.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76