Beccabecz23
Beccabecz23

Reputation: 11

How to Add and Subract 3 digit numbers in Assembly. Im using EMU8086

Its my first time to post here, hope I get answers.

I have this code, and it can add/subtract 2 digit numbers and come up with 3 digit answers, the problem is, I don't know how would I convert this to 3 digit number input with 4 digit numbered answers. help

My Code: 


.model small      
.stack 100h
.data
a         db  13, 'Please Enter first number (2 Digit): $'
b         db  10,13, 'Please Enter second number (2 Digit): $'
c         db  0AH, 10,13, 'sum is:  $'
d         db  10,13, 'difference is: $'
n1         db  0
n2         db  0
d1         db  0
d2         db  0
ans        db  0
nega       db  '-$' 


.code
main proc

mov ax, @data
mov ds, ax

1st:
mov dx, offset a  
mov ah, 9
int 21h

mov ah, 1      
int 21h

cmp al, '0'      
jb 1st
cmp al, '9'
ja 1st

sub al, 30h      
mov d1, al
mov ah, 1      
int 21h

cmp al,'0'      
jb 1st
cmp al,'9'
ja 1st

sub al, 30H      
mov d2, al
mov al, d1     
mov bl, 10
mul bl
mov n1, al     
mov al, d2
add n1, al

2nd:
mov dx, offset b  
mov ah, 9
int 21h

mov ah, 1      
int 21h

cmp al, '0'     
jb 2nd
cmp al, '9'
ja 2nd
sub al, 30H     
mov d1, al
mov ah, 1      
int 21h

cmp al, '0'   
jb 2nd
cmp al, '9'
ja 2nd
sub al, 30H     
mov d2, al
mov al, d1
mov bl, 10
mul bl
mov n2, al
mov al, d2
add n2, al


Addition:
mov bl, n1
add bl, n2

call change
mov dx, offset c
mov ah, 9
int 21h
mov dl, bl
mov ah, 2
int 21h
mov dl, bh 
mov ah, 2
int 21h

mov dl, ans
mov ah, 2
int 21h

Subtraction:
mov bl, n1
cmp bl, n2
jl sign
sub bl, n2

call change
mov dx, OFFSET d
mov ah, 09H
int 21h
mov dl, bl
mov ah, 02h
int 21h

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

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

mov ah, 4ch
int 21h

sign:
mov bl, n2
sub bl, n1

call change
mov dx, offset d
mov ah, 09H
int 21h

mov dx, offset nega  
mov ah,09H
int 21h

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

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

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

mov ah, 4Ch      
int 21h

main endp

change proc
mov ah, 0
mov al, bl
mov bl, 10
div bl
mov bl, al
mov bh, ah
add bh, 30h     
mov ans, bh
mov ah, 0
mov al, bl
mov bl, 10
div bl
mov bl, al
mov bh, ah
add bh, 30h      
add bl, 30h    
ret

change endp

end

Thanks! >////<

Upvotes: 1

Views: 7206

Answers (2)

Fifoernik
Fifoernik

Reputation: 9899

First to accomplish what you want change these data defines.

n1    dw 0
n2    dw 0
d1    db 0
d2    db 0
d3    db 0

To input a 3-digit number (extending your logic) use :

1st:
mov dx, offset a
mov ah, 9
int 21h

mov ah, 1    
int 21h
cmp al, '0'      
jb 1st
cmp al, '9'
ja 1st
sub al, 30h
mov d1, al

mov ah, 1     
int 21h
cmp al,'0'     
jb 1st
cmp al,'9'
ja 1st
sub al, 30h
mov d2, al

mov ah, 1   
int 21h
cmp al,'0'     
jb 1st
cmp al,'9'
ja 1st
sub al, 30h
mov d3, al

mov al, 100
mul d1
mov n1, ax    n1 is now defined as a word!
mov al, 10
mul d2
add n1, ax
mov al, d3
cbw
add n1, ax

The conversion routine change needs to cope with 4 digits :

mov ax, bx    The addition or subtraction result is a word!
mov bl, 10
div bl
mov ch, ah
mov ah, 0
div bl
mov cl, ah
cbw
div bl
mov bx, ax
add bx, 3030h Turns 2 digits into characters
add cx, 3030h idem
ret

Now you're ready to print these results in the correct order.

mov dx, offset c
mov ah, 9
int 21h
mov dl, bl
mov ah, 2
int 21h
mov dl, bh
mov ah, 2
int 21h
mov dl, cl
mov ah, 2
int 21h
mov dl, ch
mov ah, 2
int 21h

Upvotes: 1

sense milla
sense milla

Reputation: 19

bytes can only hold signed -127 to + 127. use words to do your computation.

Upvotes: 1

Related Questions