除以非零时“除以零”错误

时间:2015-04-17 20:52:16

标签: assembly divide

以前我曾经"除以零"在Turbo Debugger中出错,我通过在DIV之前将一个零值放入DX寄存器中来解决它。

当我添加含有di-register的行时(在代码中使用&#34标记;最近添加的"注释)返回了问题。

看一下turbo调试器,在第一次返回" Lab:"标签

除此之外,还有哪些因素会导致除零错误?

.MODEL SMALL

Print   EQU 2
Exit    EQU 4Ch

.DATA ;------------------------------------------------------

a   DW  59
ten DW  10

.CODE ;------------------------------------------------------

Start   PROC

    mov ax, SEG DGROUP
    mov ds, ax

    mov     ax, a
    mov di, 1       ; recently added
Lab:
    mov dx, 0
    div     ten     ; the "ten" variable is always non-zero (...right?)

    mov [di], dx    ; recently added
    inc di          ; recently added

    mov bx, ax

    add dx, '0' 
    mov ah, Print      
    int 21h        

    mov ax, bx

    cmp ax, 0
    jne Lab

    mov ah, Exit
    int 21h

; -----------------------------------------------------------

Start   ENDP
    .STACK 512

    END Start

1 个答案:

答案 0 :(得分:2)

以下是您的问题的解释:

.MODEL SMALL

Print   EQU 2
Exit    EQU 4Ch

.DATA ;------------------------------------------------------

a   DW  59
ten DW  10

.CODE ;------------------------------------------------------

Start   PROC

    mov ax, SEG DGROUP
    mov ds, ax

    mov     ax, a
    mov di, 1       ;DI POINTS TO SECOND BYTE (1) IN DATA SEGMENT.
                    ;THIS SECOND BYTE BELONGS TO "A". SO DI IS
                    ;POINTING TO THE SECOND BYTE OF "A".
Lab:
    mov dx, 0
    div     ten     

    mov [di], dx    ;DI IS NOT THE SAME THAN [DI]. MOVING DX (REMAINDER 
                    ;OF DIVISION) TO [DI], AND DI POINTS TO SECOND BYTE 
                    ;OF "A", SO THE VALUE OF DX IS OVERWRITING THE SECOND 
                    ;BYTE OF "A" AND THE FIRST BYTE OF "TEN", BECAUSE
                    ;DX SIZE IS 2 BYTES. SO "TEN" IS NO LONGER 10, IT'S 0.
    inc di

    mov bx, ax

    add dx, '0' 
    mov ah, Print      
    int 21h        

    mov ax, bx

    cmp ax, 0
    jne Lab

    mov ah, Exit
    int 21h

; -----------------------------------------------------------

Start   ENDP
    .STACK 512

    END Start

问题是[di]正在影响两个变量,如:

enter image description here