Reputation: 297
Previously when I had "division by zero" error in Turbo Debugger, I solved it by putting a zero into the DX register before DIV-iding by a two-byte value.
The problem returned when I added the di-register-containing lines (marked in the code with the "recently added" comments).
Looking at turbo debugger, the Division By Zero error occurs after the first return to the "Lab:" label.
Besides in this case, what other things cause the Division By Zero error?
.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
Upvotes: 1
Views: 902
Reputation: 10371
Here is the explanation of your problem :
.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
The problem is that [di] is affecting two variables, like this :
Upvotes: 2