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