Reputation: 2177
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
Upvotes: 1
Views: 2638
Reputation: 8459
Labels appear in assembly code, but the assembly code must be turned into machine instructions to be run on the device.
What this quote is saying is that as the assembly file is processed, the label in jmp
instructions is replaced with the address of the instruction following the label. The address itself is discovered during the compilation process as the instructions are collated.
If you want to look at actual machine code that is produced, you can view the intel .hex
file produced by avr-gcc. There is a wikipedia entry to help you interpret the intel hex file format.
Edit in response to OP's comment:
For rjmp
, the number supplied is calculated by the compiler by taking the address of the instruction following the label and subtracting the address of the rjmp
instruction. Note that rjmp
only works if the jump is small enough, while jmp
can go to any address.
If you want an actual document describing that process, you need to research the compiler you are using. There may not be such a document explaining exactly what you want, but the source code is available for avr-gcc.
Note that most assembly code is actually produced by compilers themselves when processing high-level language code such as C. The choice of jmp
versus rjmp
and how to calculate the number to give to the instruction can even depend on compiler switches. Compiling with -O3
or -Os
will give different resulting assembly code.
Upvotes: 0
Reputation: 39621
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)
Upvotes: 1