The stack pointer SP points at the top of the program stack
All memory cells that lies below the memory location pointed to by SP are used (= reserved/allocated)
(The ARM processor can only push the value in a whole register (= 32 bits) on the stack.
If you push a data that consists of less than 32 bits , then you (as an assembler programmer) need to manage that yourself .
push // Push the value in "regName" onto the program stack
main: mov r0, #4 push // Push value in r0 on stack mov r0, #9 push // Push value in r0 on stack
Result after the first push instruction (snapshot from EGTAPI 's stack area)
Notice that the stack pointer register SP (= 0x7affc) is pointing to the top of the program stack
Result after the second push instruction (snapshot from EGTAPI 's stack area)
Notice that the stack pointer register SP (= 0x7aff8) is updated and still points to the top of the program stack
How to run the program:
pop // Pop the int typed value from the top // of the program stack and store it in register "regName"
main: mov r0, #4 push mov r0, #9 push pop // r1 = 9 pop // r2 = 4
How to run the program:
Suppose the following is the state of the Program Stack after we have reserved some memory for the variable x :
If we want to de-allocate the memory space used for variable x and discard the value in the de-alocated variable , the resulting stack will look like this:
In this example, we assume that the value in the variable x is no longer needed nor used in future computation.
I.e.: we discard the variable stored in the variable x
Example: to de-allocate one int typed variable from the (top of the) program stack , we use:
add sp, sp, #4 // Move program stacktop down 4 bytes // This will remove the reservation of 4 bytes // that was used to stored the int typed variable
Example: to de-allocate two int typed variable from the (top of the) program stack , we use:
add sp, sp, #8 // Move program stacktop down 8 bytes // This will remove the reservation of 8 bytes // that was used to stored TWO int typed variable
+----------+ | | | | AVAILABLE . (not used) | | SP -->+----------+ | | ^ | | | USED . | | | | Program | | (= reserved . ) | stack | | | | | | | v +----------+