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
Pointer arguments: In calling code, the _____ of a variable is passed to the subroutine.
Address
- This assumes the variable must be in RAM, not in a register
- The called subroutine dereferences the address of the variable to manipulate the variable being pointed to (usually with a ldr or str instruction)
How does a function return a structure if it is too large to fit in a register?
The calling code provides memory on the stack to store it. The address of this memory is put in x8 before the function call
What is x8?
The indirect result location register. A called subroutine writes to a memory at this address, using x8 as a pointer to it.
What are leaf subroutines?
Subroutines that don’t call any other subroutines
What are some things that happen/don’t happen in a leaf subroutine?
A frame record is not pushed onto the stack
- The routine doesn’t do a bl so the LR won’t change and we don’t need to save
- The routine doesn’t call a subroutine so FP won’t change
- No need for stp/ldp
What happens to the stack frame if a leaf subroutine only uses registers x0-x7 and x9-x15?
A stack frame is not pushed at all. No need to save/restore registers and no stack variables are used
What happens to arguments 9+?
Not passed on the stack. The calling code allocates mem at the top of the stack and writes “spilled” arguments there
How many bytes are allocated for each spilled argument?
8 bytes
How does the callee read spilled arguments?
Using an offset