Jump Instructions & Call

<aside> đź’ˇ How do jump instructions manipulate the instruction pointer?

Jump instructions manipulate the instruction pointer by changing the instruction pointer to the address of the first instruction in a label. For example, jge skip will set the RIP to be the address of the instruction that comes after skip :

</aside>

Screen Shot 2022-02-14 at 9.28.13 PM.png

<aside> đź’ˇ **How does calling a function work? What does call do?

Call** will push the return address of the next instruction to be executed to the top of the stack. Then, whatever instructions are contained in that function will be executed (fetch/decode/execute). Ret will then pop the return address of the next instruction off the stack in order to “leave” the function and execute the next instruction.

</aside>

Calling Convention

Screen Shot 2022-02-14 at 9.21.09 PM.png

<aside> đź’ˇ Why is an agreed upon calling convention is necessary?

A calling convention is necessary to help distinguish between registers. For instance, if one complier wrote assembly code that put the first two arguments of a function in R8 and R9 while another complier wrote assembly code that put the first two augments of a function in R10 and R11, then there is nothing special to indicate where the arguments are and the 2 arguments would not be found since those 2 arguments are not in a common register/place.

</aside>

Return Addresses

<aside> đź’ˇ What is a return address? Why does it need to be stored when a function is called?

A return address is the address of the next instruction to be executed. A return address is stored when a function is called because a function needs to know where to go after it ends, and the return address indicates that next “destination”.

</aside>

<aside> đź’ˇ How does returning from a function works? What does ret do?

(See ***How does calling from a function work? What does call do?)***

</aside>

<aside> đź’ˇ Slide 10 Explanation

In main(), when bar() is called the return address to go back to main() is put on the stack. Then, in bar(), when foo() is called, the return address to go back to bar() is put on the stack. Once all the instructions in foo() have been executed, foo() should go back to bar(). Bar() should go back to foo()

</aside>

Screen Shot 2022-02-14 at 9.45.27 PM.png

The Stack

<aside> đź’ˇ **Basic Functionality:

RSP (%rsp)** - stack pointer (recall that the stack pointer will grow towards lower memory/lower addresses) push - decrements addresses by 8 bytes pop - increments addresses by 8 bytes. Lazy deletion is used to “remove” what’s at the top of the stack. The RSP is incremented when pop happens. lazy deletion - When something is “removed” from the stack, pop just overwrites the value of that element being “deleted”

</aside>