Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DCPU-16 DIV instruction

I'm looking at the specification for the DCPU-16 and I'm having trouble understanding the purpose of the overflow value with the DIV instruction:

DIV a, b - sets a to a/b, sets O to ((a<<16)/b)&0xffff.

Can anybody explain the semantic meaning of O here, what it would be useful for?

like image 734
Chad Avatar asked Feb 20 '23 20:02

Chad


1 Answers

It looks like O gives the fractional part of the result (as a fixed-point number). For example, consider 5 / 2:

a = 5 / 2 = 2 (integer part)
O = ((5 << 16) / 2) & 0xffff = (327680 / 2) & 0xffff = 32768

If you consider O as the 16 binary fractional digits, then this represents the 0.5 fractional part of the result.

Another way of looking at it is the binary result in bits is:

aaaaaaaaaaaaaaaa.OOOOOOOOOOOOOOOO

5 / 2 is

0000000000000010.1000000000000000

As you can tell by inspection, that result is 5 (101 binary) shifted right by one bit into the fractional bits.

like image 81
Greg Hewgill Avatar answered Feb 27 '23 23:02

Greg Hewgill