Subroutines Flashcards

1
Q

What are Subroutines used for and what are the two types?

A

Subroutines allow you to repeat a calculation using varying argument values

Open (inline)
Closed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does Open (inline) work?

A

Code is inserted inline wherever the subroutine is invoked (usually using a macro preprocessor)
Arguments are passed in/out using registers
It is efficient since overhead branching and returning is avoided
Suitable for fairly shorter routines

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does Closed work?

A

Machine code for the routine appears only cone in RAM which leads to more compact machine code than with open routines
When invoked, jumps to the first instruction of the routine (PC is loaded with the address of the first instruction)
When finished, control returns to the next instruction in the calling code (PC is loaded with the return address)
Arguments are placed in registers on the stack
Slower than open routines because of the call/return overhead

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What should Subroutines NOT do?

A

They should not change the state of the machine for the calling code

When invoked, they should save any registers it uses on the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are arguments to subroutines considered to be?

A

Local Variables - the subroutines may change their value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Check Notes for examples of Open and Closed

A

.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How may a subroutine be invoked?

A

Using a branch and link instruction (bl)

Form: bl subroutine_label

Stores the return address into the link register: x30

Return address is PC + 4, which points to the instruction immediately following bl

Transfer control to the address specified by the label

Loads the PC register with the address of the subroutine’s first instruction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In calling code, the _______ of a variable is passed to the subroutine

What does it imply?

A

address

Implies that the variable must be in RAM, not in a register (since registers don’t have addresses)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The called subroutine ____________ the address, to manipulate the variable being pointed to

How is it done?

A

dereferences

Done with an ldr or str instruction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Example code for Pointer args in Notes

A

.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does a function return for x0 and w0?

A

long ints in x0
ints, short ints, and chars in w0
Example in Notes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In C, a function may return a struct by value. The struct is usually too big to return in x0 or w0. Which other mechanism is used instead?

A

The calling code provides memory on the stack to store the returned result

The address of this memory is put into x8 prior to the function call

The called subroutine writes to memory at this address using a pointer to it

Example in notes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

x8 is the _____________ register

A

indirect result location

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are leaf subroutines?

A

They do not call other subroutines

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Is a frame record pushed on the stack?

A

No, since the routine does not do a bl, LR won’t change and since the routine does not call a subroutine, FP won’t change. Thus, eliminate the usual stp/ldp instructions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If _________ registers are used, then a stack frame is not pushed at all (since they are not saved, no need)

A

x0-x7 and x9-x15

Example in notes

17
Q

Arguments beyond the __ are passed on to the stack

A

8th

The calling code allocates memory at the top of the stack, and writes the “spilled” argument values there

By convention, each argument is allocated 8 bytes

The callee reads this memory using the appropriate offset

Example in notes

18
Q

When in sum() the stack appears as

A

[Insert Diagram]