2
\$\begingroup\$

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?

\$\endgroup\$
  • 1
    \$\begingroup\$ Unless I'm missing something about the question, the location or the relative position of the location goes into the opcode. \$\endgroup\$ – Ignacio Vazquez-Abrams Nov 22 '14 at 18:53
2
\$\begingroup\$

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".

\$\endgroup\$
  • \$\begingroup\$ There was also "AVR Instruction Set Manual" which should be the best reference, but the link didn't work. \$\endgroup\$ – gbarry Nov 22 '14 at 19:54
  • \$\begingroup\$ "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." May I obtain the reference for the preceeding piece of information? \$\endgroup\$ – Utku Nov 22 '14 at 20:03
  • \$\begingroup\$ I am asking this question, because in the "8-bit AVR Instruction Set" document, under RJMP, the performed operation is stated as: "PC ← PC + k + 1" and the syntax for this operation is stated as: "RJMP k". Now if we think labels as: <someLabel>==<someConstant>, RJMP should always fail because in that case, the absolute address to jump is being supplied into RJMP, instead of relative address. \$\endgroup\$ – Utku Nov 22 '14 at 20:18
  • \$\begingroup\$ The assembler has to calculate the proper value for k that will make the instruction work when it performs "PC+k+1". It does this using the current PC and the label's value. The +1 is because of the manner in which the PC is automatically incremented during the fetching and execution of an instruction. \$\endgroup\$ – gbarry Nov 22 '14 at 20:25
  • \$\begingroup\$ Yes, that how it is supposed to be to make sense but why is this information is not present anywhere in official documents? Or maybe it is but I can't find it. \$\endgroup\$ – Utku Nov 22 '14 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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