In AVR Assembly – I want to work out the machine code representation of the following:
ANDI r18,$10
I know the opcode for ANDI is
0010 KKKK dddd KKKK
And I know that r18
is the destination register (Rd), and that $10
(16) is KKKKKKKK – my intuition thinks that I should replace dddd
with 18
but that is not possible with only 5 bits (unless I'm severely missing something here…) as 18 in binary is 10010
.
I'm thinking I just replace KKKK
KKKK
with 0001
0000
– but I just don't see how I'm meant to address register 18 with only 4 bits.
The instruction set summary says that r16…r31 can all be represented with dddd
which I would think means 4 bits also, so I know I've got something horribly wrong.
Any help would be great.
Reference: http://www.atmel.com/Images/doc0856.pdf – page 20
Best Answer
You have the op code for ANDI wrong, it's 0111 KKKK dddd KKKK. Using the assembler, I get 0x7120 as the machine code for ANDI r18, $10, which is 0111 0001 0010 0000.
0010 for dddd is 2, which represents r18, because it's the third register from r16 to r31. Only the upper 16 registers are valid for the ADDI instruction. 0000 is r16, 0001 is r17, and 0010 is r18.