Search code examples
assemblyarduinostackmicrocontrolleravr

Assembly Stack Pointer AVR


Hello guys I have a quick question, but one I haven't been unable to answer yet. I would appreciate some assistance with this:

LDI R31, 0 

I'm not quite sure what this does, but before this I load the stack pointer HIGH equal to $08 and the low equal to $5F (I know this is the RAMEND definition for the end of the memory for my Arduino). Can someone please explain what this will do to the stack/ stack pointer? I know that R31 is used as the high Byte, but I'm not sure what this will do to my current stack of values.

This IS a very small part of my homework, but I do feel it is essential to understand this. Thanks!


Solution

  • R31 is part of the Z register, an alias for R31:R30. The X, Y, and Z registers are used by some assembly instructions for indirect addressing. See the AVR instruction Set. LDI R31, 0 sets register 31 to 0. It has no effect on the stack or stack pointer.

    It looks like you are looking at some assembly code, especially near the beginning of the program. Typically, this is where the C runtime initializes the stack pointer and RAM, among other things. The variables in your program that are initialized in the source code will have their initial values copied from the data section near the end of the program code into RAM, where the variables actually reside. The Z register is useful for this purpose, as it makes it easy to loop through the program data.