Subroutines Flashcards

1
Q

What is the equivalent of a subroutine in java?

A

A method

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

What types of subroutines are there?

A

Open(inline) & closed

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

What does an open(inline) subroutine do

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does a closed subroutine do?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What should a subroutine not do?

A

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

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

What type of variable are arguments to the subroutine considered?

A

Local. The subroutine may change their value

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

What does $ mean

A

Argument

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

Which type of subroutine is usually implemented using macros?

A

Open

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

Which type of subroutine uses labels (the kind we used for the assignment)?

A

Closed

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

Which subroutine type is alloc used for and why?

A

Closed. It is the # of bytes that the subroutine uses and is negated to allocate for the subroutine’s stack frame

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

What is the minimum number of bytes a closed subroutine can use?

A

16

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

What pseudo-op must we use for a closed subroutine?

A

.balign 4

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

When we invoke a subroutine with bl, where is the return address stored?

A

In the link register. The return address is PC +4 which points to the instruction after bl

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

When we use ret after a subroutine, where is the control transferred to?

A

Calling code. The address stored in the lr x30

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

What do the stp instructions do for each subroutine?

A

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

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

What is a stack frame?

A

A section of memory allocated for a single function call.

17
Q

What is a frame record?

A

A data structure associated with a single stack frame about the context of the function’s execution.

18
Q

The __ and the stored __ ____ in the frame records form a ___ ___

A

FP, FP values, linked list

19
Q

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?

A

19, 28

callee-saved

20
Q

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?

A

9, 15

In between function calls

Caller-saved

21
Q

How many arguments can be passed into a function without doing extra work?

Can the subroutine overwrite these registers?

A

8 using registers x0-x7

These can be overwritten (contents not preserved over a function call)

22
Q

Which types of numbers use w registers?

A

Chars, Short Ints, Ints

23
Q

What types of numbers use x registers?

A

Long ints

24
Q

Which register numbers (x and w) is a subroutine free to overwrite? Register contents are not preserved over a function call. Caller-saved

A

x0-x18, w0-w18

25
Q

Pointer arguments: In calling code, the _____ of a variable is passed to the subroutine.

A

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)
26
Q

How does a function return a structure if it is too large to fit in a register?

A

The calling code provides memory on the stack to store it. The address of this memory is put in x8 before the function call

27
Q

What is x8?

A

The indirect result location register. A called subroutine writes to a memory at this address, using x8 as a pointer to it.

28
Q

What are leaf subroutines?

A

Subroutines that don’t call any other subroutines

29
Q

What are some things that happen/don’t happen in a leaf subroutine?

A

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

30
Q

What happens to the stack frame if a leaf subroutine only uses registers x0-x7 and x9-x15?

A

A stack frame is not pushed at all. No need to save/restore registers and no stack variables are used

31
Q

What happens to arguments 9+?

A

Not passed on the stack. The calling code allocates mem at the top of the stack and writes “spilled” arguments there

32
Q

How many bytes are allocated for each spilled argument?

A

8 bytes

33
Q

How does the callee read spilled arguments?

A

Using an offset