Electronic – AVR Assembler Labels

assemblyavr

According to doc1022, p. 17 (AVR Assembler User Guide):

User defined labels which are given the value of the location counter at the place they appear.

My question is, what goes into the opcode when a label is used in a jmp and in an rjmp instruction since a label is really the value of the location counter at the place the label appears? Secondly, if there exists an official (or non official) document answering my question, do you know where can I find it?

Best Answer

The opcode will have most of its bits set aside to hold an address. The number of bits can vary because sometimes the address is absolute, and sometimes it is relative (relative addresses are offsets and therefore smaller numbers). The actual value that's placed in these bits is a calculation based on the value of the label that is used. For absolute addressing it is just the label's value. For relative addressing, the value for the operand field is calculated by subtracting the current program counter (where the jump instruction appears) from the address referred to in the label. Those are two different locations.

When the assembler sees a label, it uses its value just as if you had specified the number instead. Part of the usefulness of an assembler is that it keeps track of the PC as the program is compiled, and therefore can assign the correct address to a label when it appears.

This answer is somewhat generic, as I haven't worked with AVR assembly code.

Update: I searched on "AVR Assembler User Guide" to find the reference in your question. At the same time if found "AVR Assembler - - Atmel Corporation" which links to a document that shows how the instructions are put together. See the section called "Instructions".