如何在Assembly中添加和减去3位数字。我正在使用EMU8086

时间:2015-07-29 15:48:43

标签: assembly emulation

我第一次在这里发帖,希望得到答案。

我有这个代码,它可以加/减2位数字并得出3位数的答案,问题是,我不知道如何将其转换为3位数输入,4位数字编号答案。帮助

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

谢谢! > ////<

2 个答案:

答案 0 :(得分:1)

字节只能保存有符号-127到+ 127.使用单词进行计算。

答案 1 :(得分:1)

首先要完成你想要改变这些数据的定义。

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

要输入3位数字(扩展逻辑),请使用:

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

转换例程更改需要处理4位数字:

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

现在您已准备好以正确的顺序打印这些结果。

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