|
Suppose the register r0 contains the value X
Before excuting the push {r0} instruction:
Suppose the register r0 contains the value X
After excuting the push {r0} instruction:
The stack pointer (SP) is decremented by 4 !
DEMO: /home/cs255001/demo/asm/8-sub/push.s
main: mov r0, #4 push {r0} // Push value in r0 on stack mov r0, #9 push {r0} // Push value in r0 on stack |
Stack content in EGTAPI after the first push {r0} instruction:
DEMO: /home/cs255001/demo/asm/8-sub/push.s
main: mov r0, #4 push {r0} // Push value in r0 on stack mov r0, #9 push {r0} // Push value in r0 on stack |
Stack content in EGTAPI after the second push {r0} instruction:
We can achieve the same effect of the push {r0} instruction using these 2 instructions:
main: mov r0, #4 sub sp, sp, #4 // Decrement stack pointer by 4 str r0, [sp] // Push value in r0 on stack mov r0, #9 sub sp, sp, #4 // Decrement stack pointer by 4 str r0, [sp] // Push value in r0 on stack |
In fact, that is how the push instruction is executed internally inside the CPU
|
Suppose the program stack contains the value X at the top.
Before excuting the push {r0} instruction:
Suppose the program stack contains the value X at the top.
Aftyer excuting the push {r0} instruction:
The
register r0 will
contain the
value
X
The
stack pointer (SP) will be
incremented by
4 !
DEMO: /home/cs255001/demo/asm/8-sub/push.s
main: mov r0, #4 push {r0} // Push value in r0 on stack mov r0, #9 push {r0} // Push value in r0 on stack pop {r1} // r1 = 9 pop {r2} // r2 = 4 |
We can achieve the same effect of the pop {r1} and pop {r2} instructions using these 2 instructions:
main: // pop {r1} ldr r1, [sp] // Pop top of stack into reg r1 add sp, sp, #4 // Increment stack pointer by 4 // pop {r2} ldr r2, [sp] // Pop top of stack into reg r2 add sp, sp, #4 // Increment stack pointer by 4 |
In fact, that is how the pop instruction is executed internally inside the CPU