2
\$\begingroup\$

I am disassembling AVR code from a bit of hardware. The processor is an Atmel-8.

On restart, after setting the stack pointer, the program calls a subroutine that starts with:

ldi     r17, 0x72
ldi     r18, 9
clr     r30
clr     r31
ldi     r20, 0x80
ldi     r19, 5
lpm     r3, Z+
lpm     r2, Z+

I am puzzled by the last two instructions. Since the subroutine is called right after power-up/reset, I don't know where the Z register is pointing to. I guess it contains whatever initial value the registers have after a power-up/reset.

Does anyone know what this could be doing? what address can be in the Z register upon power-up?

\$\endgroup\$

3 Answers 3

4
\$\begingroup\$

The Z register in an AVR is a 16-bit register pair of r30:r31. The assembly code clears both these registers to 0 clr r30; clr r31, then reads two bytes starting at this address (0x0) from the flash into r3 and r2.

Basically the code reads the first two bytes in the flash memory along with initialising four other registers to specific values.


In case you are curious, there are two other 16-bit register pairs, the Y register occupying r28:r29, and the X register in r26:r27.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you all who answered. Unfortunately I can only pick one right answer, and Tom was first. I did know about the X,Y and Z registers, but completely forgot about it... Now I need to figure out what the intention is behind this particular assignment. \$\endgroup\$
    – DrD
    Commented Jul 29, 2023 at 14:07
3
\$\begingroup\$

With AVR, X,Y,Z register pairs are registers (26,27) (28,29) (30,31) respectively. You can see in the code clr r30 and cli r31. So Z is 0. Edit:Tom got in first.

\$\endgroup\$
3
\$\begingroup\$

Z is synonymous with r30:r31. It has been set to zero.

The reset vector resides at 0x0000 in program memory. Evidently this is fetching the jump instruction (almost always 0x940c), which doesn't seem very helpful to me but perhaps it's being used for something else.

\$\endgroup\$

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.