CS2002 Flashcards
Fetch-Execute Cycle
- Load data from the IP 2. Set the ip to the next instruction 3. Control unit decodes the instruction 4. ALU executes the instruction 5. Some instr may change the pointer to something else
ISA
Set of instructions understood by a CPU -
- Their encoding into bits
- Their meaning
imulq $86, (%rax), %rcx
- imul: signed integer multiplication
- imulq: qword or quadword= 64 bit
- $86: Constant 86
- %rax, %rcx : registers
- anything inside brackets, is basically just derefencing
- Compiler
- Interpreter
- Assembler
- Linker
- Loader
- Compiler: converts high level instructions to low level instructions which the CPU can understand
- Interpreter: Program that reads high level programmes and carries it out
- Assembler: Converts assembly language to object code, think of it as compilers for lower level languages.
- Linker: Combines object code files to create an executable file that can run by including different library object code files and determining memory offsets for functions in different files.
- Loader: Loads all the libraries into memory and prepares them for execution
List the different kinds of registers
- GP registers: Stores one word and are used in integer operations, typically a small number, e.g. x86 which has 16 registers
- Floating point registers:
- Special registers: IP a.k.a program counters
Orthogonality of ISAs
When ISA doesn’t distinguish between different registers. What can be done with one register can be done with any other registers. Each instruction performs a unique task that doesn’t overlap with another instruction.
ARM is orthogonal
Uses of Conventions
They help structure writing assembly code
- Not writing values immediately to registers
ISA Design decisions for where arguments can be loaded and stored
- Register-memory: Atleast one argument can come straight from memory
- More expensive.
- Load store: All arguments are directly from the memory, register - register
- Memory is accessed only to load data into the registers
- Separation of memory and arithmetic operations make design easier.
Word Addressed
One address for one word and there is extra info to refer to the bytes inside the word
Byte Addressed
One address per octet ( 8 bits ) and the lowest address in a word represents the address of the word.
How does ISA allow memory access or what are the different memory addressing modes?
- Immediate mode
- Direct mode
- Indirect mode
- Index mode
- Immediate Memory Access Modes
- Direct Mode
Immediate Mode: The value is specified in the instruction itself, used for constants.
Direct Mode: The value is stored at a fixed address. Used for global variables.
- Indirect Memory access Mode
- Index mode
Indirect mode: The value is stored in a fixed address which is specified inside a register. Extra power(check)
Index Mode: The value is at a fixed address specified by register but is offset by an index.
Register Naming

Stackframes
The stack frame is the collection of all data on the stack associated with the function call like the arguments to the function and the return address
The steps involved in a function call with respect to the stack frame and the different pointers
- Push the arguments onto the stack
- execute callq
- push the value of rbp onto the stack
- make rbp the current rsp
- decrement rsp to make space for local variables and stuff
What are the 2 special purpose registers?
- rip: Instruction pointer
- rflags: binary flags for comparison
Main addressing mode in x86 ( AT&T Syntax )
disp(%reg1, %reg2, scale)
- disp - displlacement
- reg1 - register that holds the base address
- reg2 - register that holds the index
- scale - value which can be only 1, 2, 4, 6
This refers to the address, disp + (reg1) + scale * reg2
What is load effective addressing?
Stores the effective address of the source into the destination register instead of storing the value pointed to by effective address ( this is what mov does )
Lea doesn’t read from the computed address whereas Mov does.
Command for compiling a C file ( sam.c ) to an assembly file ( sam.s)
clang -S sam.c -o sam.s
Which register is used for:
- Passing arguments to a function
- Returning values from a function
- rdi
- rax is used if the value fits in 64 bits, otherwise, the upper 64 bits is stored in the rdx register.
Calling conventions (1) - How arguments are passed into functions.
There are 2 ways in which arguments are passed onto a function
- if there are N arguments to a function and N <= 6, and all the arguments fit into the 64 bit registers then they are stored inside these registers
- rdi, rsi, rdx, rcx, r8, r9
- the return value from function should be stored in rax
- If there are more arguments or they are longer, then these go onto the stack
- If the callee wants to use any of the arguments then they should be restored back to the original value upon return
Call and exit in ISA
callq address: pushes the address of the following instruction ( this is the return address ) onto the stack and transfers the control to the function by setting the rip to the address of the function.
retq: pops the address from the stack by incrementing the stackpointer and returns control back to the calling function by setting rip to the popped address
What are callee saved registers?
rbx, rbi, r12 - r15