当我用汇编语言划分时溢出

时间:2017-04-02 23:01:47

标签: assembly overflow x86-16

我正在使用汇编语言(intel 8086)添加2个4位数的代码。在代码中的某个时刻,我想将AX除以BX寄存器(使用div BX),例如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




1 个答案:

答案 0 :(得分: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