Search code examples
assemblyavr

AVR Assembler Labels


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?

Thanks


Solution

  • The assembler uses the correct encoding of a jump instruction that jumps to the label given as operand. That is after the instruction executes the value of PC will be equal to the address of the label. For the RJMP instruction this means that the assembler doesn't use the address of label directly in the encoded instruction, instead it encodes it as a constant operand k, where k solves the equation label = PC + k + 1.

    I don't know if this is explicitly stated in any official document, but this how all assemblers work. Otherwise it's would pretty dumb. In particular the following example given in section 4.3 of the AVR Assembler Guide wouldn't actually be an infinite loop if the AVR assembler didn't work this way:

    test: rjmp test ; Infinite loop (Instruction)