divide 64bit in two 32bit registers by 32bit
I am trying to write an x86 emulator in JavaScript for education purposes. I have alre开发者_如何学Cady written a compiler and at the moment I am trying to write the x86 emulator in JavaScript.
I have however a problem with the DIV instruction. According to http://siyobik.info/index.php?module=x86&id=72 DIV takes a 64bit number as input by interpreting EDX as the higher 32bit and EAX as the lower 32bit. It then divides this number by the DIV parameter and puts the result (if it is not greater than 0xFFFFFFFF) into EAX and the remainder into EDX.
As JavaScript does not support 64bit integer I need to apply some tricks. But so far I did not come up with something useful.
Does someone here have an idea how to implement that correctly with 32bit arithmetic?
My previous version was incorrect, so i will rewrite it.
Maximum int in JS is 53 bits, we can make use of it.
You take x[63:16]
(48 bits) and divide. Res = x[63:16] / y * 16
or << 0x01
. Rem = x[63:16] % y * 16
.
Then do: Res |= (Rem | x[15:0]) / y
* - x[a:b]
means bits of x from a-th to b-th. like X = 0110
, x[3:1] = 011
精彩评论