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