CS2002 Flashcards

1
Q

Fetch-Execute Cycle

A
  1. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ISA

A

Set of instructions understood by a CPU -

  • Their encoding into bits
  • Their meaning
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

imulq $86, (%rax), %rcx

A
  • imul: signed integer multiplication
  • imulq: qword or quadword= 64 bit
  • $86: Constant 86
  • %rax, %rcx : registers
  • anything inside brackets, is basically just derefencing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Compiler
  2. Interpreter
  3. Assembler
  4. Linker
  5. Loader
A
  1. Compiler: converts high level instructions to low level instructions which the CPU can understand
  2. Interpreter: Program that reads high level programmes and carries it out
  3. Assembler: Converts assembly language to object code, think of it as compilers for lower level languages.
  4. 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.
  5. Loader: Loads all the libraries into memory and prepares them for execution
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List the different kinds of registers

A
  1. GP registers: Stores one word and are used in integer operations, typically a small number, e.g. x86 which has 16 registers
  2. Floating point registers:
  3. Special registers: IP a.k.a program counters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Orthogonality of ISAs

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Uses of Conventions

A

They help structure writing assembly code

  1. Not writing values immediately to registers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

ISA Design decisions for where arguments can be loaded and stored

A
  1. Register-memory: Atleast one argument can come straight from memory
    1. More expensive.
  2. Load store: All arguments are directly from the memory, register - register
    1. Memory is accessed only to load data into the registers
    2. Separation of memory and arithmetic operations make design easier.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Word Addressed

A

One address for one word and there is extra info to refer to the bytes inside the word

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Byte Addressed

A

One address per octet ( 8 bits ) and the lowest address in a word represents the address of the word.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does ISA allow memory access or what are the different memory addressing modes?

A
  1. Immediate mode
  2. Direct mode
  3. Indirect mode
  4. Index mode
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Immediate Memory Access Modes
  2. Direct Mode
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. Indirect Memory access Mode
  2. Index mode
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Register Naming

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Stackframes

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The steps involved in a function call with respect to the stack frame and the different pointers

A
  1. Push the arguments onto the stack
  2. execute callq
  3. push the value of rbp onto the stack
  4. make rbp the current rsp
  5. decrement rsp to make space for local variables and stuff
17
Q

What are the 2 special purpose registers?

A
  1. rip: Instruction pointer
  2. rflags: binary flags for comparison
18
Q

Main addressing mode in x86 ( AT&T Syntax )

A

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

19
Q

What is load effective addressing?

A

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.

20
Q

Command for compiling a C file ( sam.c ) to an assembly file ( sam.s)

A

clang -S sam.c -o sam.s

21
Q

Which register is used for:

  1. Passing arguments to a function
  2. Returning values from a function
A
  1. rdi
  2. rax is used if the value fits in 64 bits, otherwise, the upper 64 bits is stored in the rdx register.
22
Q

Calling conventions (1) - How arguments are passed into functions.

A

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
23
Q

Call and exit in ISA

A

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

24
Q

What are callee saved registers?

A

rbx, rbi, r12 - r15

25
Q

Calling conventions (2)

A

Stack is aligned at multiples of 16 bytes

There is a 128 byte red zone beneath the stack pointer, this is for the callee to store temporary data without creating a new stackframe

The last argument is pushed in first ( reverse order ) so from right to left.

Usually the base pointer is used with a positive offset in order to retrieve the arguments passed into the function

26
Q

What is relative addressing and what is the advantage of using it?

A

RIP rel ad is using the current value of an rip plus an offset to locate an operand rather than using the actual address of the operand. This leads to position independent code i.e. code that runs no matter where it is in the memory.

27
Q

Why would you not want to use the heap for stack frames?

A

This is because the heap may contain gaps due to freeing up memory whereas, the stack is contiguous.

28
Q

What are rflags?

A

These are 64 bit flags that are not available as general purpose flags but they contain the result of the execution of a previous instruction.

They are used for conditional jumps.

29
Q

mul vs imul

div vs idiv

A

mul (signed) and imul (unsigned )

  • atleast one operand should be in rax

div (signed ) idiv ( unsigned )

  • atleast one operand should be in rax
  • quotient goes to rax and remainder goes to rdx
30
Q

Shift and rotate

  • Logical shift
  • Arithemetic shift
  • rotate right/left
  • rotate with carry right/left
A
  • Logical shift: shr, shl -
  • Arithmetic shift: sar, sal - right shift duplicates sign bit
  • rotate right/left: ror, rol:
  • rotate with carry right/left: rcr, rcl - rotate with carry, involve carry bit in rotation

Artithmetic shift: preserves sign bit, can be used for multiplication and division

Logical shift: Does not preserve sign bit, used for unsigned interpretation.

31
Q

Flags

  1. ja, jae
  2. jb, jbe
  3. jz, jze
  4. js, jns
A
  1. Equal/not eq unsigned
  2. less than or less than or equal to unsigned
  3. zero or not zero
  4. if signed or unsigned
32
Q

Unconditional Control flow

A
  1. jmp: goto label
  2. call: pushes the rip onto the stack and jumps to that address
  3. ret: pops the stack and stores it in the rip
33
Q

What are returns the time stamp counter and what returns the clock cycles

A
  1. The RDTSC returns the time stamp counter
  2. TCS returns the count of the clock cycles
34
Q

How to compile a file foo.c:

  1. Such that it is only Preprocessed
  2. The preprocessed version is converted to assembly.
  3. Convert assembly code to object code
A

clang -E foo.c -0 foo-preprocessed.c

clang -S foo-preprocessed.c -o foo.s

clang -c foo.s -o foo

35
Q

What is big endian and little endian?

A

It is the order in which bytes are stored in memory.

In Big Endian, the most significant byte is stored at the ( first )lowest address and the least significant byte is stored at the highest address.

In Little Endian, the least significant byte is stored at the lowest address and the most significant address is stored at the highest address.

36
Q

Difference between CISC & RISC arch:

A

RISC:

  • executes smaller instructions
  • Since they are smaller, more number of instructions are needed
  • Each clock cycle executes one task
  • The instruction since they are simpler are easier to decode

CISC:

  • executes more complex instructions
  • There maybe dedicated hardware that performs some of these instructions
  • Complex instructions may take multiple cycles to executes.
  • lots of addressing modes for programmers
37
Q

How many general purpose registers are there in the modern CPU ( their size and count )

A

There are 16 64 bit general purpose registers in the CPU.

38
Q

How did the names of the general purpose registers change when they went from 8 to 16 in number and from 32 bits to 64 bits in size?

A

The new ones are numbered r8 to r15

Initially the registers were just named

AX, BX, CX, DX, SI, SD, SP, BP

When the 32 bit registers came, they became extended (E)

EAX, EBX, ECX, EDX, ESI, ESD, ESP, EBP

When the 64 bit versions came

RAX, RBX, RCX, RDX, RSI, RSD, RSP, RBP

39
Q

What should you be aware of in terms of the order of operands when comparing them in AT&T syntax.

A

The inversion of operands

cmp $1 %rax

This value will be 1 if rax > 1, there is an inversion of the operands.

subq $1 %rax

subtract 1 from rax and then store the result in rax (and not subtract rax from 1)