I racked my brain for several hours, tried to dinf the answer on the Internet, but I still can’t understand why I can’t divide the numbers correctly. I read on the Internet that if you need to divide by a 16-bit number, then the dividend should be divided into two registers: the high bits are written in the DX register, the low bits are written in the AX register. In this case, the quotient will be in the AX register, the remainder - in the DX register. Here is the part of my code:
A EQU -59 ;in data section
B EQU 219
xor eax, eax
xor edx, edx
xor ebx, ebx
xor ecx, ecx
mov ax, A
cwd ;dx = -1, ax = -59
mov bx, B
idiv bx
PRINT_STRING 'idiv -59/219 = '
PRINT_DEC 2, ax
PRINT_STRING '.'
PRINT_DEC 2, dx
The answer should be 0,27 (Output: 0.-27 or something like that, I don't care about minuses, the question is not about that).
In AX I got 0, as it should be, BUT in DX = -59, which is A... Why? When abs(A) > abs(B), I got the right answer, but not when the absolute value of dividend is less than divider. In this case, DX = previous value of AX = A...
How can I get the right answer with idiv?
CodePudding user response:
How can I get the right answer with
idiv
?
You can't. The idiv
instruction performs a signed integer division. You won't receive a result that has a fraction like in 0.27.
-59 can't contain 219 so the quotient is 0, and the remainder is everything that is left which is -59.
You can use FPU or SSE instructions to calculate non-integer values.