The push instruction of ARM
 

  • (Simplified) syntax of the push instruction:

      push {rN}   // Pushes the 32 bits value in reg rN on the System Stack 

    Note:

    • The push instruction can push multiple registers

      We will only use 1 register in the push instruction for simplicity

    Note:

    • The push instruction will always push 4 bytes on the System Stack

    • The push instruction will update the SP register (SP = SP - 4) !!

The push {rN} instruction of ARM

Suppose the register r0 contains the value X

Before excuting the push {r0} instruction:

The push {rN} instruction of ARM

Suppose the register r0 contains the value X

After excuting the push {r0} instruction:

The stack pointer (SP) is decremented by 4 !

Example of the push {rN} 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 first push {r0} instruction:

Example of the push {rN} 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:

Achieving the same effect of the push 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

The pop instruction of ARM
 

  • (Simplified) syntax of the pop instruction:

      pop {rN}   // Pops the 32 bits value from the top 
                 // of the System Stack into reg rN  

    Note:

    • The pop instruction can pop multiple values of the stack into different registers...

      We will only use 1 register in the pop instruction for simplicity

    Note:

    • The pop instruction will always pop 4 bytes off the System Stack

    • The pop instruction will update the SP register (SP = SP + 4) !!

The pop {rN} instruction of ARM

Suppose the program stack contains the value X at the top.

Before excuting the push {r0} instruction:

 

The pop {rN} instruction of ARM

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 !

Example of the pop {rN} 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

        pop     {r1}            // r1 = 9
        pop     {r2}            // r2 = 4 
 

Achieving the same effect of the pop instruction
 

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