2017-04-02 93 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

謝謝你,我感謝你的幫助 –