Stack and Functions Flashcards

(40 cards)

1
Q

type of data structure where items are added and then removed in reverse order

A

stack

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

stack is often referred to as ___

A

LIFO (Last In, First Out)

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

stack is heavily used for _____

A

the storage of information during procedure or function calls

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

adding an item to a stack is referred to as a ______

A

push operation

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

removing an item from a stack is referred to as a _______

A

pop operation

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

pop and push syntax

A
pop <operand64>
push <operand64>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how is stack implemented in memory?

A

it is implemented growing downward

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

register used to point to the current top of stack in memory

A

RSP (stack pointer register)

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

what is being adjusted when performing push and pop operations?

A

the stack pointer register (RSP)

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

what happens in a push operation?

A
  1. rsp is decreased by 8
  2. operand is copied to the stack at [rsp]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what happens in a pop operation?

A
  1. current top-of-stack [rsp], is copied into the operand
  2. rsp is increased by 8
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

general format for declaring/defining a function

A
global <funcName>
 - is typically placed before any of the sections

<funcName>:
      ; function body
      ret (return statement)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

a function may be defined how many times?

A

only once

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

true/false

there is a specific order required for how functions are defined

A

false, there is no specific order

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

two (2) main actions that function(s) calls

A
  1. linkage
  2. argument transmission
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

function must be able to return to the correct place in which it was originally called

A

linkage

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

function must be able to access parameters to operate on or return results

A

argument transmission

18
Q

two (2) instructions that handle the linkage

A
  1. call <funcName>
  2. ret
19
Q

how does

call <funcName>
handle linkage?
A

it transfers control to the stated function

ex. from the _start label to the function specified in the function call

20
Q

how does ‘ret’ handle linkage?

A

from the current function, it returns control back to the calling routine (from where it was called)

21
Q

how does

call <funcName>
work?
A

saves the address of where to return by placing the contents of the RIP on the stack

22
Q

where does rip point to?

A

the next instruction

note: rip = instruction pointer register

23
Q

how does ‘ret’ work?

A

ret pops the current top of stack (rsp) into the rip which points the control back to the return address initially saved into the stack when the function was first called

24
Q

refers to sending information to a function and obtaining a result as appropriate for the specific function

A

argument transmission

25
transmitting values to a function is referred to as _____
call-by-value
26
transmitting addresses to a function is referred to as _____
call-by-reference
27
what is/are used to pass parameters to and/or from a function
a combination of registers and the stack
28
which arguments are passed in the registers?
the first six (6) integer arguments are passed in registers
29
which arguments are passed on the stack
the seventh and any additional arguments are passed on the stack
30
which registers are used to pass arguments?
1st arg: rdi 2nd arg: rsi 3rd arg: rdx 4th arg: rcx 5th arg: r8 6th arg: r9
31
what register is used to store the return value of a function?
rax
32
which registers can be used as a temporary storage (usually for swapping two values)?
r10 and r11
33
convert high-level code to assembly stats1(&arr, len, &sum, &ave); *assume all are of quadword size
``` mov rdi, arr mov rsi, qword[len] mov rdx, sum mov rcx, ave call stats1 ```
34
what is responsible for clearing the arguments from the stack when the function is completed?
calling routine
35
items on the stack as part of a function call
call frame
36
call frame is also referred to as _____
activation record or stack frame
37
possible items in the call frame (4)
1. return address (required): RIP 2. preserved registers (if any): RBP 3. passed arguments (if any) 4. stack dynamic local variables (if any)
38
local variables that are allocated on the stack
stack-based local variables
39
additional memory on the stack for local variables is allocated by adjusting the _____
rsp
40
what happens to the allocated memory for stack-based local variables when the function is completed
the allocated memory is released