Home > Back-end >  How does div function in assembly language using fixed point arithmetic?
How does div function in assembly language using fixed point arithmetic?

Time:11-08

Hi I am new to assembly language and I am trying to understand how to use fixed point numbers in a division in x86 assembly language. When I use div (using registers ax, bx and dx) the result is not exact and I don't know why.

Thank you for your help!

Update #1 (1000 and 900 are already fixed point numbers)

mov ax, 1000
mov bx, 900
shl dx, 8
div bx

CodePudding user response:

The "pre-shift" before a division, if the dividend was in ax, should shift and widen ax into dx:ax. In a diagram, it could look like this:

Take this, in ax:

 ------ ------ 
| high | low  |
 ------ ------ 

Turn it into this, in dx:ax  

 ------ ------   ------ ------ 
|  0   | high |:| low  |   0  |
 ------ ------   ------ ------ 

shl dx, 8 is not enough, that affects only dx but not ax. You could do it like this:

mov dx, ax
shr dx, 8
shl ax, 8
div bx

Or you could do something with the 8-bit registers that make up dx and ax, but I do not recommend that.

CodePudding user response:

The div command and other commands in x86 Assembly are integer-only operations. Modern computers support floating-point operations (rather than fixed-point), using the FPU. This is done by using FPU-specific commands, with the FPU's own registers. You can read more about it here. This is only applicable to 32 and 64-bit assemblers since floating points were not introduced in 16-bit architectures.

You can also create your own floating-point system, though I do not recommend it (it wouldn't be as accurate and fast and would be a challenge to program).

  • Related