Topic B Flashcards
What is a subroutine?
a section of code that can be called repeatedly as a program executes
What are other names for subroutines?
Procedures, functions, methods
Advantages of subroutines?
- 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
What do subroutines need to have and why?
- 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
What does RET stand for and what are they used for?
-RET retrieves the stored return address (which is the top of the stack) to the instruction pointer and pops it off the call stack.
What instruction is used to call a procedure?
CALL label
What does the call instruction do?
- 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 do we deal with nested subroutine calls?
- 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
What is a stack?
A data structure that uses a LIFO structure
What is the name of the stack pointer register?
ESP - it holds the address of the item which is currently on top of the stack
What do the pop and push instructions do?
- 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
Why is it important to remember that the stack grows down in memory?
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 do we pop off multiple items from the stack at the same time?
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
What are the two main forms of parameters?
Value Parameters
Reference Parameters
What is the difference between value and reference parameters?
value parameters are numeric values where as reference parameters are the addresses of numeric values such as variables
What is the simplest method of passing parameters into a subroutine?
by using a register: cope a value into a register then call the subroutine
What does the LEA assembly instruction do?
- LEA loads the effective address of a variable
How can we pass more parameters into a subroutine?
- since we don’t have enough registers to store all the variables in a program we can use a stack.
What is printf()
- 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
what are the two external library routine calls we can make?
printf and scanf
What is the ECX register used for?
-to count what loop iteration we’re on
Why do we need to pop ECX off the stack at the end of our program?
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
what are the 4 common format specifiers and what type are they associated with?
%d - integer
%s - string
%c - character
f%f - float
What is scanf()
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
What is one of the ways a compiler can turn your high level code into machine code?
By creating stack frames and pushing it to the stack.
What are the three things a stack frame holds
- local variables
- return address
- parameters
What happens when a HLL subroutine is called?
a new stack frame is created on the stack
What is the EBP?
- 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
how does the compiler know what area of memory it can use to store its local variables?
Because this area is is somewhere between the base pointer and the stack pointer
What happens when a subroutine returns (regarding frames)
-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
What happens calling a subroutine (regarding frames)
- 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
What is a recursive subroutine?
a subroutine that calls itself
What is mutual recursion
where two or more subroutines recursively call each other
What are recursive algorithms useful for coding?
Useful for coding data structures, if you want a tree or some other nested structure
What does tail recursive mean?
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.
What does recursion use to simplify the stack?
uses the stack (of nested calls and frames) to simplify the algorithm
What do we use to find the length of a string in C? and what library do we need to include?
strlen
with the