Stack and Heap

Reserving space for the stack requires some arithmetic operations to set the symbol values used at runtime. The following listing is a sections-segment definition code fragment that shows this arithmetic.

Listing: Stack Setup Operations

_stack_address = __END_BSS;

_stack_address = _stack_address & ~7; /*align top of stack by 8*/

__SP_INIT = _stack_address + 0x4000;  /*set stack to 16KB*/

The heap requires a similar space reservation, which the following listing shows. Note that the bottom address of the stack is the top address of the heap.

Listing: Heap Setup Operations

___heap_addr = __SP_INIT; /* heap grows opposite stack */

___heap_size = 0x50000; /* heap size set to 500KB */