Compilation etc part 2 Flashcards
Suroutines & Stacks
What is a subroutine?
A subroutine is a set of instructions designed to perform a frequently used operation, packaged as a unit. It can be called from different parts of a program to avoid code repetition.
What is a method in programming?
A method is a type of subroutine associated with an object or class in object-oriented programming. It defines a behavior for objects.
What does the call instruction do?
call operand jumps to a subroutine and stores the current program counter (PC) value (the return address) before jumping.
What does the ret instruction do?
‘ret’ retrieves the stored return address and loads it back into the program counter (PC), resuming execution after the subroutine.
How is ‘call’ different from a regular ‘jump’ instruction?
‘call’ saves the current location (return address) before jumping, while a regular ‘jump’ simply moves to a new location without saving.
Where is the return address typically stored during a ‘call’ operation?
The return address is usually stored in a register or on the stack to be retrieved later by the ‘ret’ instruction.
What is one idea for storing the return address in a subroutine call?
The first byte of the subroutine can be used to store the return address.
Why might storing the return address in the subroutine’s first byte be problematic?
It can overwrite important instructions or data at the start of the subroutine, making the subroutine unreliable.
What is the return address?
It is the memory location where the program should continue execution after a subroutine finishes.
In simple systems, why is using the first byte for the return address considered?
It simplifies design by avoiding the need for a dedicated return address stack or register.
What is a major disadvantage of storing the return address in the first byte of a subroutine?
Only one return address can be stored.
What happens if a subroutine that uses Idea #1 tries to call itself (recursion)?
It would need to store a second return address, which Idea #1 cannot handle.
Why can’t a subroutine call itself under Idea #1?
Because overwriting the single return address would lose the original return location, breaking program execution.
What programming feature becomes impossible if only one return address can be stored?
Recursion (a subroutine calling itself).
What does FORTRAN stand for, and what were its features?
FORTRAN stands for “FORmula TRANslation”; it used jumps (GOTO) and conditional jumps but banned recursion.
Why did FORTRAN ban recursion?
To support a simple return address storage method like storing it in the first byte (Idea #1).
What was Algol, and how was it different from FORTRAN?
Algol (“Algorithmic Language”) introduced structured programming (if..then..else, etc.) and allowed recursion.
Which early language’s ideas influenced modern languages like C and Java?
Algol’s ideas influenced the design of modern programming languages.
What is a stack in computer science?
A stack is a structure that can flexibly store a variable number of bytes, handling data dynamically.
What does LIFO mean in the context of stacks?
LIFO stands for “Last In, First Out” — the last item added to the stack is the first one removed.
What is another name for LIFO?
FILO, which stands for “First In, Last Out.”
Why are stacks useful for storing return addresses?
They can store multiple return addresses and retrieve them in the correct order, allowing recursion and nested subroutine calls.
What is the purpose of the sp (stack pointer) register?
The ‘sp’ register points to the top of the stack in memory.
Do we need to know where the bottom of the stack is?
No, as long as we are careful to only pop values that have been pushed.