Subroutines Flashcards
What is the equivalent of a subroutine in java?
A method
What types of subroutines are there?
Open(inline) & closed
What does an open(inline) subroutine do
- Code is inserted inline when subroutine is invoked
- done using a macro preprocessor instead of cutting & pasting
- arguments are passed in/out using registers
- efficient since overhead of branching & returning is avoided
- There are many many copies of your subroutine
What does a closed subroutine do?
- Only one copy in RAM (machine code appears only once in RAM) so more compact
- when invoked, control branches to the first instruction of routine (PC is loaded w/address of first instruction instead of being incremented each line like in regular code
- When finished, control returns to next instruction in calling code (PC is loaded w/return address)
- Arguments are placed in registers or on the stack
- Slower than open routines bc of the call/return overhead
What should a subroutine not do?
Change the state of the machine for the calling code
- when invoked, it should save any registers it uses on the stack
- When it returns, it should restore the og value of the registers
What type of variable are arguments to the subroutine considered?
Local. The subroutine may change their value
What does $ mean
Argument
Which type of subroutine is usually implemented using macros?
Open
Which type of subroutine uses labels (the kind we used for the assignment)?
Closed
Which subroutine type is alloc used for and why?
Closed. It is the # of bytes that the subroutine uses and is negated to allocate for the subroutine’s stack frame
What is the minimum number of bytes a closed subroutine can use?
16
What pseudo-op must we use for a closed subroutine?
.balign 4
When we invoke a subroutine with bl, where is the return address stored?
In the link register. The return address is PC +4 which points to the instruction after bl
When we use ret after a subroutine, where is the control transferred to?
Calling code. The address stored in the lr x30
What do the stp instructions do for each subroutine?
Create a frame record for each function’s stack frame. This safely stores the lr in case it’s altered by a bl in the body of the function