Section Exercises

True or False:

The content of a callee-saved register does not remain the same after a subroutine is called.

False!

The value of a callee-saved registered is saved by the subroutine that is called. So if in a callee-saved register, we have a value x and then we call a subroutine foo, even though we use the callee saved register in the execution of foo, when the subroutine returns, the callee-saved register still has the value x.

The value of a caller-saved register remains the same after a subroutine is called.

False!

The value of a caller-saved register can be changed by a subroutine call, so, in order to reuse the initial value of such a register, you need to store it either on the stack or in a callee-saved register.

rbx, rsp, and rbp are callee-saved registers.

True!

rax is a callee-saved register.

False!

rax is a caller-saved register, so its value has to be stored somewhere else.

Hint: remember that the value returned by a subroutine will be found in rax. meaning that the content of rax is overwritten with every subroutine execution.

rdi, rsi, rdx, and rcx are caller-saved registers.

True!

Stack (mis)alignment was the most common cause of a subroutine call crash in Computer Organization 2024.

True!

Keep in mind that before calling a subroutine, your stack should be 16-byte aligned.

The term stack frame refers to the entire stack.

False!

The stack frame refers to the area of the stack that is used by a subroutine.

rsp is indicates the base of the current stack frame.

False!

rsp, the stack pointer, indicates the top of the current stack frame and it moves with every push or pop instruction. rbp is the base pointer which indicates the base of the current stack frame.

A prolog is used to set up the stack frame of a subroutine.

True!

The epilog refers to the action of restoring the previous stack frame before a subroutine returns.

True!

It is advised to store local variables in registers.

False!

Although, in theory, you could, it is not advisable because storing the local variables in registers has some drawbacks such as limiting the number of local variables (there is a limited number of registers, hence a limited number of local variables) and you risk to have their value changed if the register is used in a subroutine.

To make space for the local values, you can push them on the stack after the prologue.

True!

As an alternative, you can subtract the needed space from the stack pointer if the local variables are uninitialized.

Multiple Choice

Why is it or is it not the stack misaligned in the following cases?

main: 
     ...
     subq $16, %rsp 
     movq $10, %rdi 
     pushq %rdi 
     call foo
main:  
     ... 
     subq $8, %rsp 
     movq %rsp, %rdi 
     subq $8, %rsp 
     call foo
main: 
     ...
     pushq $4 
     subq $16, %rsp 
      movq $10, %rdi 
      pushq %rdi 
      call foo
main: 
     ...
     subq $16, %rsp
     pushq $4 
     movq $10, %rsi 
     pushq %rsi 
     popq %rdi 
     call foo

Last updated