# Section Exercises

### True or False:

<details>

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

**False!**&#x20;

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.

</details>

<details>

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

**False!**&#x20;

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.

</details>

<details>

<summary><code>rbx</code>, <code>rsp</code>, and <code>rbp</code> are callee-saved registers.</summary>

**True!**

</details>

<details>

<summary><code>rax</code> is a callee-saved register.</summary>

**False!**&#x20;

`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_.

</details>

<details>

<summary><code>rdi</code>, <code>rsi</code>, <code>rdx</code>, and <code>rcx</code> are caller-saved registers.</summary>

**True!**

</details>

<details>

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

**True!**&#x20;

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

</details>

<details>

<summary>The term stack frame refers to the entire stack.</summary>

False!&#x20;

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

</details>

<details>

<summary><code>rsp</code> is indicates the base of the current stack frame.</summary>

False!&#x20;

`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.

</details>

<details>

<summary>A prolog is used to set up the stack frame of a subroutine.</summary>

**True!**

</details>

<details>

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

**True!**

</details>

<details>

<summary>It is advised to store local variables in registers.</summary>

**False!**&#x20;

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.

</details>

<details>

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

**True!**&#x20;

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

</details>



### Multiple Choice

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

{% tabs %}
{% tab title="Option 1" %}
```c
main: 
     ...
     subq $16, %rsp 
     movq $10, %rdi 
     pushq %rdi 
     call foo
```
{% endtab %}

{% tab title="Answer" %}
**The stack is misaligned.**

It can be seen in the code associated with this answer option that we first reserve 16 bytes of space on the stack for two variables by subtracting 16. Before calling foo, `rdi` is pushed on the stack, meaning that we add another 8 bytes on the stack. As a result, there were 24 bytes added to the stack instead of a number of bytes equal to a multiple of 16.
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Option 2" %}
```c
main:  
     ... 
     subq $8, %rsp 
     movq %rsp, %rdi 
     subq $8, %rsp 
     call foo
```
{% endtab %}

{% tab title="Answer" %}
**The stack is aligned.**

Although we only need space on the stack for a value, by subtracting only 8 bytes, the calling convention is broken. To make the stack 16 aligned, another 8 bytes are subtracted before foo is called.
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Option 3" %}
```c
main: 
     ...
     pushq $4 
     subq $16, %rsp 
      movq $10, %rdi 
      pushq %rdi 
      call foo
```
{% endtab %}

{% tab title="Answer" %}
**The stack is aligned.**

By pushing 4 on the stack (8 bytes) and then subtracting 16 and then pushing `rdi` (8 bytes), we reserve 32 bytes on the stack, which is a multiple of 16.
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Option 4" %}
```c
main: 
     ...
     subq $16, %rsp
     pushq $4 
     movq $10, %rsi 
     pushq %rsi 
     popq %rdi 
     call foo
```
{% endtab %}

{% tab title="Answer" %}
**The stack is misaligned.**

In the above code example, we observe that 16 bytes are allocated on the stack and that 4 and `rsi` are pushed on the stack, meaning that we reserve 32 bytes on the stack. By popping the top of the stack in `rdi`, we reduce the stack size by 8 bytes, resulting in only 24 bytes being reserved on the stack.
{% endtab %}
{% endtabs %}



