Search code examples
assemblybit-manipulationavratmega

Bitwise operations, assembly


I've got a task to change the bit location from

START bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0

FINAL bit3' | bit2' | bit7 | bit6 | bit1 | bit0 | bit5' | bit4'

Using the bitwise and shift/rolling instructions.

I don't know how to move them different way than shifting and rolling. Should I use only one or multiple registers to do that?


Solution

  • Using shift and rol, you should be able to do it with two registers, wasting the original in the progress.

    Shifting A right through carry copies the left most bit into carry. Shifting B right through carry copies that bit to register B and shifts the LSB to carry.

    By sequence of

    1 - ROR A,A,1     // A = _7654321, --> carry = '0'
    2 - ROR B,B,1     // B = 0_______, --> carry = 'x' / don't care
    3 - ROR A,A,1     // A = x_765432, --> carry = '1'
    4 - ROR B,B,1     // B = 10______, --> carry = 'x' / don't care
    // this has copied two bits from A to B while shifting both
    
    5 - ROR B,B,1     // B = x10_____,
    6 - ROR B,B,1     // B = xx10____,
    
    repeat steps 1-4 again for B to contain bits '3 2 _ _ 1 0 _ _'
    

    (And this is half of the solution)