3

What is the difference beatwean X, Y and Z registers in AVR microcontrollers. What for each of them suitable in C compilers? And where do compilers store heap pointer, stack pointer, frame pointer? Do this registers have the same capabilities or providing addressing in different spaces(ex. EEPROM, RAM).

1 Answer 1

8

X Y and Z registers are actually pairs of r27:r26, r29:r28 and r31:r30 registers. Each of them can be used as indirect pointers to SRAM:

ld r16, X

with post-increment, or pre-decrement:

ld r16, -Y
st Z+, r16

But only Y and Z can be used with displacment

ldd r16, Y + 10
std Z + 5, r16

and only Z can be used to indirect read the flash memory, and no pre-decrement or displacement are available :

lpm r16, Z+
lpm r17, Z

So, there is no particular way how compilers should use them, and what information there should be stored. It is very depend on the compiler, considering all those limitations. For example, Z is good to be reserved to access the flash memory, while Y is good to store the stack frame, because it can be accessible with displacement. Also for example, the GCC use X and Z registers as "preserved by the caller", while Y register is used as "preserved by the called routine". This convention helps to minimize push-pop operations, allowing the caller routine to allocate Y as a pointer iterator, or stack frame, and also allows the called routine to freely use X and Z without spending time to push and pop them. But again, how to use those registers is very depend on the compiler. Nothing force it to use registers in one way or another.

The stack pointer is always stored in SPH:SPL I/O registers (0x3E, 0x3D) they are handled by the core itself, while performing calls, returns, pushes and pops. Compiler need not to store it somewhere else.

There is no such thing as a heap in AVR. So, if compiler implements the heap memory management somehow, it depend on the implementation where and how the heap is allocated. But speaking of such small MCUs as AVR, there is usually no point to store heap at all, because no dynamic allocation is needed.

6
  • Sorry, I meant data stack pointer not call stack. Where it stored? Feb 6, 2018 at 15:14
  • And why do you say that AVR MCU are too small for dynamic memory allocation. Some of MEGA and XMEGA have internal RAM from 4KBytes to 16KBytes and possibility of using external RAM Memory. Feb 6, 2018 at 15:19
  • 1
    There is only one stack. It used both for store return addresses (call/ret) and for storing the data (preserve registers push/pop and local variables). Yes it is possible to have an memory manager. But most task where those MCUs this is not required. Fast response and predictable memmory allocation thos those are better than unpredictable behavior of memory manager which may lead to heap fragmentation and stack overlap
    – AterLux
    Feb 6, 2018 at 15:44
  • 1
    All local variables allocated in either the stack or the registers. It depends on the compiler and even on the selected optimization level. Without an explicitly enabled memory manager to have a heap, and explicitly allocated memory (e.g. malloc) no variables will be stored there.
    – AterLux
    Feb 6, 2018 at 19:34
  • 1
    @AterLux Actually, since the processor stack on the AVR only supports push and pop of single registers there are compilers that only use this stack for return addresses and sacrifices one of the pointer registers to implement a separate stack for local data.
    – Johan
    Feb 7, 2018 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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