I'm having a problem with my Assembly Code. Whenever I debug this code Division Overflow was the always error. The code runs smoothly when the value of AX is only two digits.
What changes do I need to make the division work with 4 digit values? Thanks.
ASSUME DS:DATA, CS:CODE
DATA SEGMENT
X DW 0
Y DW ?
s DB "The aswer is", 0
Z DW 4
DATA ENDS
CODE SEGMENT
MAIN PROC
MOV DS:[Y],23
MOV AX,[Y]
ADD AX,4556
MOV [X],AX
PUSH[X]
CALL WRITE
POP[X]
MOV AH,4CH
INT 21H
MAIN ENDP
WRITE PROC
PUSH BP
MOV BP,SP
MOV AX,0B800H
MOV ES,AX
MOV ES,AX
MOV DI,5*160
MOV AX,[BP+4]
MOV BL,10
DIV BL
ADD AL,'0'
STOSB
XCHG AH,AL
ADD AL,'0'
STOSB
MOV AL, 00001111B
STOSB
POP BP
RET
WRITE ENDP
CODE ENDS
END MAIN
When you do DIV r/m8
(e.g. DIV BL
) the quotient will be stored in AL
, so if the quotient is greater than 255 you will get a division overflow.
If you want to be able to handle quotients up to (but not including) 65536, use DIV r/m16
:
XOR DX, DX ; DIV r/m16 divides the 32-bit value DX:AX by the divisor, so we need to clear DX
MOV BX, 10
DIV BX
; The quotient is now in AX, and the remainder in DX
To be able to handle even larger quotients, use DIV r/m32
or DIV r/m64
. I'll leave it as an exercise for you to look them up in Intel's instruction set reference.