I'm trying to divide 859091
by 11
to obtain the quotient and the remainder, but I'm getting Floating Point Exception on line:
div bx
This is my code for SASM:
%include "io.inc"
section .data
dividend dd 859091
divisor dw 11
section .text
global CMAIN
CMAIN:
push ebp
mov ebp, esp
xor eax, eax
xor ebx, ebx
xor edx, edx
mov ax, word [dividend]
mov dx, word [dividend + 2]
mov bx, word [divisor]
test bx, bx
jz exit
div bx
exit:
leave
ret
You're getting divide overflow because the quotient doesn't fit within a 16 bit integer.
You can split up the dividend into upper and lower halves to produce up to a 32 bit quotient and 16 bit remainder. The remainder of
dx = 0000 : ax = upper_dividend / divisor
becomes the upper half of 2nd dividend for the 2nd division, so the 2nd division calculatesdx = remainder : ax = lower_dividend / divisor
, neither of which can't overflow because the remainder is strictly less than the divisor. This process can be extended for longer dividends and quotients, one step per word of dividend and quotient, with the remainder of each divide step becoming the upper half of the partial dividend for the next step.Example using MASM syntax:
example for quad word: