assemblydcpu-16

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?


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.