2017-04-02 51 views
-1

我正在做一个代码,用汇编语言(英特尔8086)添加2个4位数的数字。在代码中的某一点,我想通过BX寄存器(使用div BX)来分割AX,例如AX = 2AB3(十进制为10931)和BX = 2710(十进制为10000)。当我用汇编语言划分时出现溢出

正常情况下,结果我应该有AX = 1(商)和DX = 3A3(余数),问题是模拟器显示溢出消息。

下面是代码:

DATA SEGMENT 
 

 
MSG1 DB 0DH,0AH, "first number : $" ,0DH 
 
MSG2 DB 0DH,0AH, "second number : $" ,0DH 
 
RST DB 0DH,0AH, "result : $" ,0DH 
 

 
DATA ENDS 
 
PILE SEGMENT PARA STACK 
 
    DB 128 DUP (?) 
 
PILE ENDS 
 

 
CODE SEGMENT 
 
    ASSUME CS:CODE,DS:DATA,SS:PILE 
 

 
DEBUT: 
 
mov ax,data 
 
mov ds,ax 
 

 
mov dx,offset MSG1  ; first msg 
 
mov ah,9 
 
int 21h 
 

 
mov ah,1  ; multipling first number by 1000 and store it 
 
int 21h 
 
sub al,30h 
 
mov ah,0 
 
mov bx,1000 
 
mul bx 
 
push ax 
 

 
mov ah,1   ; multipling second number by 100 and store it 
 
int 21h 
 
sub al,30h 
 
mov ah,0 
 
mov bx,100 
 
mul bx 
 
push ax 
 

 
mov ah,1  ; multipling third number by 10 and store it 
 
int 21h 
 
sub al,30h 
 
mov ah,0 
 
mov bx,10 
 
mul bx 
 
push ax 
 
      
 
mov ah,1   ; last number 
 
int 21h 
 
sub al,30h 
 
mov ah,0 
 
push ax 
 

 

 
mov dx,offset MSG2 ;same thing for the second 4 digits number 
 
mov ah,9 
 
int 21h 
 

 
mov ah,1 
 
int 21h 
 
sub al,30h 
 
mov ah,0 
 
mov bx,1000 
 
mul bx 
 
push ax 
 

 
mov ah,1 
 
int 21h 
 
sub al,30h 
 
mov ah,0 
 
mov bx,100 
 
mul bx 
 
push ax 
 

 
mov ah,1 
 
int 21h 
 
sub al,30h 
 
mov ah,0 
 
mov bx,10 
 
mul bx 
 
push ax 
 
      
 
mov ah,1   
 
int 21h 
 
sub al,30h 
 
mov ah,0 
 
push ax 
 
     ;------------------------------- 
 
     ;here I'm doing the addition to all the stored numbers to have the result number 
 
     ;in the DX register 
 
pop dx 
 
pop bx 
 
add dx,bx 
 
pop bx 
 
add dx,bx 
 
pop bx 
 
add dx,bx 
 
pop bx 
 
add dx,bx 
 
pop bx 
 
add dx,bx 
 
pop bx 
 
add dx,bx 
 
pop bx 
 
add dx,bx 
 
push dx ; I've stored the result number because I'm going to use the DX register to show the 
 
      ; third message 
 

 
mov dx,offset RST 
 
mov ah,9 
 
int 21h 
 

 
pop dx ;restore the result number 
 
mov ax,dx 
 
mov bx,10000 
 
div bx  ;I'm dividing the result number by 10000 to have the first number of the result number 
 
      ;in AX register (quotient) , the error message show up here 
 
push dx  
 
push ax 
 

 
pop dx 
 
add dl,30h 
 
mov ah,2 
 
int 21h 
 

 
pop ax 
 
mov bx,1000 ;deviding by 1000 to have the second number 
 
div bx  ;same error 
 
push dx 
 
push ax 
 
pop dx 
 
add dl,30h 
 
mov ah,2 
 
int 21h 
 

 
pop ax 
 
mov bx,100 
 
div bx 
 
push dx 
 
push ax 
 
pop dx 
 
add dl,30h 
 
mov ah,2 
 
int 21h 
 

 
pop ax 
 
mov bx,10 
 
div bx 
 
push dx 
 
push ax 
 
pop dx 
 
add dl,30h 
 
mov ah,2 
 
int 21h 
 

 
pop dx 
 

 
add dl,30h 
 
mov ah,2 
 
int 21h

+0

愿我们看到你的代码'回覆工作或其相关部分? – halfer

+0

您是否咨询了指令集参考?你有没有看到股息是在'DX:AX'?您是否正确设置了“DX”?你为什么没有显示你的代码?你看过SO上的gazillion重复吗? – Jester

+0

(你的英语对我来说似乎很好,我们更喜欢这里的问题不要包含[关于语言的道歉](https://stackoverflow.com/search?tab=newest&q=sorry%20for%20my%20English) - 这只是另一件事编辑出来)。 – halfer

回答

0

试试这个变化:

pop dx   ;restore the result number 
mov ax,dx 
xor dx,dx   ;; added this line to clear dx 
mov bx,10000 
div bx 

,或者它可能是:

pop ax   ;; restore the result into ax 
xor dx,dx   ;; added this line to clear dx 
mov bx,10000 
div bx 
+0

我尝试了第一件事,它确实有效!你能向我解释为什么没有你的解决方案它不工作吗? –

+1

@OmarAsrih除法指令将dx:ax中的32位值除以bx中的值(在此情况下),在ax中产生一个16位商,并在dx中产生一个16位余数。如果dx在除法指令之前未被清零,则可能发生溢出,在这种情况下,需要将dx清零才能得到正确答案。 – rcgldr

+0

谢谢你,我感谢你的帮助 –