: lea -0x7(%rsi),%ebp
2: /x $rsp = 0x7fffffffdc98
3: /x $rbp = 0x7fffffffdd60
(gdb) x/1xw $rsp
0x7fffffffdc98: 0xffffdd60
```
---
## Why?
* More fine-grained debugging
* See how your computer works
* Help you review for the midterm
* Will need to know this for the next homework
---
## Review
* Let's review a couple of things before getting to example questions
---
## Names and Sizes
* Registers can be used at different sizes
* `rax` is 64-bits, `eax` is the lower 32-bits of the same register
* You cannot go directly from a 32-bit number to a 64-bit number
* e.g. `addq %rax, %ebx`
* The instruction even has the width in it
* The value in %ebx needs to be sign extended before using %rbx
---
## The Stack
* "the stack" is a conceptually special piece of memory
* It is accessed so frequently that it is very likely to be cached
* It is the workspace for temporary data that won't fit into a register
---
## Stack Registers
* `rsp` is the stack pointer
* Holds the value to the stop of the stack
* `rbp` is the base pointer (or sometimes frame pointer)
* Remembers where the "local" top of the stack is
---
## Stack Instructions
* `push` adds something to the 'top' of the stack
* `push src` is like doing `memory[--%rsp] = src`
* `pop` removes something from the stack
* `pup dst` is like doing `dst = memory[%rsp++]`
* The stack goes down from the top!
* Subtracting from rsp adds space on top of the stack
* The area of stack that is being used locally is called the frame
---
## Other special registers
* rip: the current instruction pointer
* rax: where a return value is stored
* rdi, rsi, rdx, rcx, r8, r9: registers for passing arguments to a function
---
## Addressing modes
* Immediate: `mov $0x10, dst`
* Register: `mov %rax, dst`
* Direct: `mov 0xffffdd60, dst
* Indirect: `mov (%rax), dst)`
---
## More Indirect
* Indirect addressing is like pointer access
* It also supports scaling and displacement (and both at the same time)
* Indirect with displacement: `mov 8(%rax), dst)`
* Like `dst = memory[rax + 8]`
* Indirect with scaling: `mov (%rax, %rbx, 4), dst`
* Like `dst = memory[rax + rbx*4]`
---
## Example Question
pushq %rbp
movq %rsp, %rbp
**Q** Which statement accurately describes the code above?
a. Before calling a function, the caller adds an argument onto the stack.
b. This allocates memory on the stack.
c. In a called function, the code saves the caller's base stack pointer before replacing it with the function's new base stack pointer.
d. This frees memory on the stack.
---
## Example Question
movq %rbp, %rsp
popq %rbp
**Q** Which statement **does not** accurately describe the code above?
a. In a called function, this restores the callers %rbp and entry %rsp, restoring the frame of the caller.
b. This code prevents a stack overflow when stack-using functions are called repeatedly.
c. This allocates memory on the stack.
d. This frees memory on the stack.
---
## Example Question
**Q** Which of these is **not** an advantage of floating point values over fixed point?
a. Floating point operations are supported directly in hardware.
b. Floating point values can express much greater ranges given the same number of bits.
c. Floating point formats are standardized, making them easily shared between systems.
d. Floating point values can represent fractional values.
---
## Example Question
**Q** Two consecutive addq instructions use the same destination operand. What is **true** about this situation?
a. In a non-pipelined CPU, there is no hazard.
b. In a pipelined CPU, there is a data hazard.
c. Consecutive instructions of that type are not allowed.
d. The instructions will need to be reordered.
---
## Example Question
**Q** The instruction '`movq (%rax), %rax`' is issued during the instruction fetch stage on clock cycle 0. In the five stage pipeline discussed in class, which is the **earliest stage** after which the new value of %rax will be **available for forwarding** to a subsequent instruction?
a. Move instructions cannot be forwarded.
b. Clock cycle 2.
c. Clock cycle 4.
d. None of the above.
---
## Example Question
**Q** Consider the five stage pipeline shown. A designer tells you that they plan to split the execution phase into two steps, with each step taking half of the current 10ms clock period. All of the other stages run in less than 5ms. However, the two new execution stages will require a pipeline buffer with a delay of 0.1ms.
Which of the following is true?
a. The execution stage cannot be split because it would make forwarding too complicated.
b. A new clock rate can be used that is twice the current clock rate.
c. A new clock rate can be used that is slightly less than twice the current clock rate.
d. A new clock rate can be used that is slightly faster than twice the current clock rate.
---
## Example Question
**Q** Consider a superscalar pipeline two instructions wide and 10 instructions deep. Through prefetching, the pipeline is always filled with instructions. If a data hazard causes an unavoidable bubble of two instructions before the next instruction can run, but does not affect any other instructions, what is the lowest that CPU utilization will drop to because of the bubble?
a. 20%
b. 50%
c. 80%
d. 90%
---
## Example Question
**Q** Consider this code:
float mathFloats(float a, float b) {
return a + b - a;
}
Which statement about this code is **true**?
a. `return b;` would return the same value as the function.
b. `return a - a + b;` would return the same value as the function.
c. This code will not compile.
d. None of the above are true.
---
## Example Question
Write a function that returns nothing and accepts two arguments: a pointer to an int array and the number of elements of that array.
The function should set every element in the array with the index of that element squared (e.g. $0^2, 1^2, ...$)
---
## Example Question
This code will push a value onto the stack:
long long value;
asm("push %0" : : "r" (value) :);
This code will retrieve a value from the stack:
long long out;
asm("pop %0" : "=r" (out) : :);
Write a function that returns nothing and accepts two arguments: a pointer to a long long array and the number of elements of that array.
Use the stack to reverse the array. Use no variables other than loop counters and the stack.
---
## Exam Hints
* Study the quiz
* Study the example questions
* Study the homeworks
* Be careful if you have some LLM summarize the slides
* I repeat the things I believe are important
* The LLM will happily condense things into a single bullet, removing the emphasis
* Also, you're smarter than an LLM. Believe in yourself.