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
What is a stack frame?
A section of memory allocated for a single function call.
What is a frame record?
A data structure associated with a single stack frame about the context of the function’s execution.
The __ and the stored __ ____ in the frame records form a ___ ___
FP, FP values, linked list
If a called function uses any of the registers x__ to x__, it must save their data to the stack at the beginning of the function and then restore at the end. What are these registers called?
19, 28
callee-saved
The callee can also use registers x__ to x__ and these are not saved/restored by the callee. What is the only time it is safe to use these?
What are these registers called?
9, 15
In between function calls
Caller-saved
How many arguments can be passed into a function without doing extra work?
Can the subroutine overwrite these registers?
8 using registers x0-x7
These can be overwritten (contents not preserved over a function call)
Which types of numbers use w registers?
Chars, Short Ints, Ints
What types of numbers use x registers?
Long ints
Which register numbers (x and w) is a subroutine free to overwrite? Register contents are not preserved over a function call. Caller-saved
x0-x18, w0-w18