Question

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?

Était-ce utile?

La solution

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.

Autres conseils

((a<<16)/b) will give you the 16-bit fractional portion of the division.

It's easier to see in base-10: If we want to find the first three fractional-digits of, say, 5/3, we can move 5 three places over (5000), divide that by 3, then take the last three whole digits. 5000/3 is 1666, so the first three digits after the decimal of 5/3 are .666.


This works because "moving 5 one digit over" is the same as "multiplying by 10", and multiplication/division are commutative (the order can be swapped around), so (5 * 1000) / 3 = (5 / 3) * 1000 = 1.6666... * 1000 = 1666.666....

In other words, shifting 5 over a few digits and dividing by 3 is the same as shifting (5/3) by a couple of digits.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top