views:

60

answers:

1

I am trying to write an x86 emulator in JavaScript for education purposes. I have already 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?

+1  A: 

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

Andrey
This is, I believe, not correct. If x[63:48] is 4 and x[15:0] is 4 too and the rest is 0, the this would give 4/2 + 4/2 = 4 which is of course incorrect.
Simon
@Simon, yes, now it is fixed
Andrey
Ok, wrong again, because if x[31:16] = 1 and you divide it by 16 you will get 0 where it should be 1
Simon
@Simon you should carry remainder from previous operation, read carefully
Andrey
@Simon check new version
Andrey