Search code examples
assemblybitbyte-shifting

Assembly procedure to convert binary/hex to split octal


This is an assignment, I don't know where to start.

Assignment:

A byte can be represented using three octal digits. Bits 7 and 6 determines the left octal digit (which is never higher than 3); bits 5, 4 and 3 are the middle digit; and bits 2, 1 and 0 are the right digit.

For instance, 11010110 b is 11 010 110 b OR 326 oct. The value of a word is represented in split octal by applying the 2-3-3 system to the high-order and low-order bytes separately.

Write a procesure splitOctal that converts a word to a stringof exactly 7 characters representing the value of the number in split octal; two groups of three separated by a space.

Follow cdecl protocols. The procedure will have two parameters: 1) The word value (passed as the low order word of a doublewowd) 2) the address of the 7-byte-long destination string.

MODIFICATIONS: Instead of pushing the value convert as a word onto the stack, only use doublewords on the stack. So push the value to convert as a DOUBLEword onto the stack

I don't know where to start to accomplish this by shifting bits and rotating bits. Maybe give me some material to read, or a little kickstart?


Solution

  • Easy way:

    convert hex -> 8bit integer by subtracting ASCII '0' or 'A', then left-shift the 4bit value from the first digit, and OR with the 4 bit value from the second digit.

    Then convert that 8bit integer to octal by shifting/masking to extract the three sets of bits you want, and add '0' to each one.


    More complicated way: don't combine into a single 8bit integer at any point. There are many ways to get the 4th bit from the first hex digit combined with the low two bits from the second hex digit. Your best bet might still involve an OR.


    If you're actually targeting 8086, and not just normal x86, then shift-by-one and shift by CL are the only shifts you have available. So to shift by 4, you probably have to mov cl, 4 / shl al, cl, instead of just shl al, 4.