Topic B Flashcards
(37 cards)
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