Topic B Flashcards

1
Q

What is a subroutine?

A

a section of code that can be called repeatedly as a program executes

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

What are other names for subroutines?

A

Procedures, functions, methods

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

Advantages of subroutines?

A
  • save effort in programming
  • reduce size of programs
  • Share code/workload between programmers
  • encapsulate/package a specific activity
  • provide east access to tried and tested code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What do subroutines need to have and why?

A
  • every subroutine needs a return address
  • this is because there can be different calls of the same subroutine to different places
  • so we need to know where to return to after the subroutine is finished
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does RET stand for and what are they used for?

A

-RET retrieves the stored return address (which is the top of the stack) to the instruction pointer and pops it off the call stack.

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

What instruction is used to call a procedure?

A

CALL label

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

What does the call instruction do?

A
  • The call operation pushes the current value of the instruction pointer (the return address) to the top of the call stack
  • puts the required subroutine address into IP (so the next instruction to be executed is the first instruction of the subroutine)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do we deal with nested subroutine calls?

A
  • we use a call stack to keep track of the return addresses of the subroutines called.
  • once a subroutine has finished, we pop it off the stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a stack?

A

A data structure that uses a LIFO structure

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

What is the name of the stack pointer register?

A

ESP - it holds the address of the item which is currently on top of the stack

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

What do the pop and push instructions do?

A
  • push decrements address in ESP so it points to free space and writes item to the memory location pointed to by the ESP
  • pop fetches the item pointed to by the ESP and increments the ESP by a chunk to remove the item from the stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is it important to remember that the stack grows down in memory?

A

Because this means that the top of the stack is the bottom free slot in memory, so to push something to the stack you decrement the stack pointer down a chunk

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

How do we pop off multiple items from the stack at the same time?

A

If we’re using a 32 bit system, to pop off one item would be 4 bytes so we just add 8 or 12 bytes to the ESP

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

What are the two main forms of parameters?

A

Value Parameters

Reference Parameters

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

What is the difference between value and reference parameters?

A

value parameters are numeric values where as reference parameters are the addresses of numeric values such as variables

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

What is the simplest method of passing parameters into a subroutine?

A

by using a register: cope a value into a register then call the subroutine

17
Q

What does the LEA assembly instruction do?

A
  • LEA loads the effective address of a variable
18
Q

How can we pass more parameters into a subroutine?

A
  • since we don’t have enough registers to store all the variables in a program we can use a stack.
19
Q

What is printf()

A
  • typing printf and then a string as a parameter will take whatever’s in the string and output it to the console
  • it expects its parameters to be on the stack
  • it doesn’t remove the parameter from the stack so you must do it
20
Q

what are the two external library routine calls we can make?

A

printf and scanf

21
Q

What is the ECX register used for?

A

-to count what loop iteration we’re on

22
Q

Why do we need to pop ECX off the stack at the end of our program?

A

Because ECX will be storing the incorrect loop value of which iteration we’re on, so we pop it and then add the new loop value

23
Q

what are the 4 common format specifiers and what type are they associated with?

A

%d - integer
%s - string
%c - character
f%f - float

24
Q

What is scanf()

A
calling scanf() allows you to read values from the keyboard
- it takes two parameters a string including a format specifier and the address to store whatever it reads in from the keyboard
25
Q

What is one of the ways a compiler can turn your high level code into machine code?

A

By creating stack frames and pushing it to the stack.

26
Q

What are the three things a stack frame holds

A
  • local variables
  • return address
  • parameters
27
Q

What happens when a HLL subroutine is called?

A

a new stack frame is created on the stack

28
Q

What is the EBP?

A
  • the base pointer is a register which points to the bottom of the current stack frame
  • The compiler uses EPB to store and manipulate local variables
29
Q

how does the compiler know what area of memory it can use to store its local variables?

A

Because this area is is somewhere between the base pointer and the stack pointer

30
Q

What happens when a subroutine returns (regarding frames)

A

-As each subroutine returns, the block of the subroutine’s stack frame is cleared from the stack, the old base pointer is restored and the old return address is restored

31
Q

What happens calling a subroutine (regarding frames)

A
  • params are pushed onto stack
  • return address is pushed onto stack
  • old EBP is pushed onto stack
  • current address of top of stack is put into EBP
  • local variabls are installed on the stack increasing EPS
32
Q

What is a recursive subroutine?

A

a subroutine that calls itself

33
Q

What is mutual recursion

A

where two or more subroutines recursively call each other

34
Q

What are recursive algorithms useful for coding?

A

Useful for coding data structures, if you want a tree or some other nested structure

35
Q

What does tail recursive mean?

A

the recursive subroutine does what it needs to do and it calls itself as the very final instruction in its code. So it’s tail recursive because the call happens at the end.

36
Q

What does recursion use to simplify the stack?

A

uses the stack (of nested calls and frames) to simplify the algorithm

37
Q

What do we use to find the length of a string in C? and what library do we need to include?

A

strlen

with the