当我用汇编语言划分时溢出
Getting overflow when I divide in assembly language
我正在编写一个代码,用汇编语言(intel 8086)添加 2 个 4 位数字。在代码中的某个点,我想通过 BX 寄存器 divide AX(使用 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
试试这个改变:
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
我正在编写一个代码,用汇编语言(intel 8086)添加 2 个 4 位数字。在代码中的某个点,我想通过 BX 寄存器 divide AX(使用 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
试试这个改变:
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