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

  • \$\begingroup\$ Please throw in a link to the instruction set documentation and what page you are referring to. \$\endgroup\$
    – jippie
    Commented Apr 14, 2013 at 10:54
  • \$\begingroup\$ atmel.com/Images/doc0856.pdf pg 20 - Sorry about that! \$\endgroup\$ Commented Apr 14, 2013 at 11:09
  • \$\begingroup\$ Why don't you use the assembler? You'll find it much easier! It's 0x7120. \$\endgroup\$ Commented Apr 14, 2013 at 11:18
  • \$\begingroup\$ Honestly didn't even think of that - I'm very new to this...will install it now and have a look. Thanks! \$\endgroup\$ Commented Apr 14, 2013 at 11:30

1 Answer 1


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.

  • \$\begingroup\$ Oh my, I feel very stupid now. That makes so much sense. Thank you so much. \$\endgroup\$ Commented Apr 14, 2013 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.