Subroutines Flashcards
What are the characteristics of an open(inline) subroutine?
- Code is inserted inline wherever the subroutine is invoked
2.Arguments are passed in/out using registers - Efficient, since branching and returning is avoided
- Suitable only for short subroutines
How are open(inline) subroutines invoked and defined?
By use of m4 Macros
ie. define(cube, `mul $2 $1 $1, mul $2, $1, $2’)
ie. invoked as cube(x19, x20)
What are the characteristics of a closed subroutine?
- Machine code for the routine appears only once in RAM
- When invoked, control “jumps” to the first instruction of the routine
- When finished, control returns to the next instruction in the calling code
- Arguments are placed in registers or the stack
- Slower than open routines, because of calling/returning
(T/F)Arguments to subroutines are considered as local variables
True
The subroutine may change their value.
What are the 3 parameters to pass parameters to subroutines?
By-value, By-reference, Using the stack
What is the process of invoking a closed subroutine?
- branch to the subroutine using ‘bl subroutine’
- Return address is stored in the Link Register(x30)
What does the stp instruction do?
Creates a frame record in each function’s stack frame
What is a pointer argument?
The address of the variable is passed to the subroutine. The called subroutine dereferences the address, to manipulate the variable being pointed to.
What registers are argc and argv[] passed into?
argc is passed into w0/x0, and the address for argv[] is passed into x1
How is a structure returned?
A struct’s values are stored in a memory location specified by x8, which is provided before the function call