1/54
Comprehensive flashcard deck for CPE 2300 Midterm 2 covering subroutines, memory mapping, memory access instructions, stack frames, arrays, strings, and pointers.
What is a subroutine?
A reusable block of code that performs a task and returns control to its caller; similar to a C function.
Why are subroutines used in programming?
To reuse tested code, organize programs into smaller tasks, and create libraries.
What two RISC-V instructions are used to call subroutines?
JAL (jump and link) and JALR (jump and link register).
What does the JAL instruction do?
Jumps to a PC-relative target and saves the address of the next instruction (usually PC+4) in a destination register.
What does "link" mean in Jump-and-Link?
Saving the return address so execution can resume in the caller after the subroutine finishes.
How does JAL differ from a conditional branch instruction?
JAL jumps unconditionally and can save a return address; a conditional branch tests a condition and does not save a return address.
Why can JAL only reach nearby code?
Its PC-relative signed jump offset has limited bits, giving a range of about plus or minus 1MiB.
What does the JALR instruction do?
Jumps indirectly using an address held in a register plus a 12bit signed immediate; it can save a return address.
What can JALR do that JAL cannot?
Call a function through a register-held address, including distant targets or function pointers.
What is the syntax of the JALR instruction?
jalr rd, offset(rs1); rd receives the return address and the jump target is computed from rs1 plus offset.
What does the pseudo-instruction ret expand to and do?
Pseudo-instruction for jalr x0, 0(ra); resume execution at the address in ra.
Why does writing JAL's link to x0 matter?
x0 discards the saved return address, creating a jump rather than a call that can return using that link.
What are the two minimum requirements for a callable subroutine?
A starting address and a return location; together they provide linkage.
What is a side effect in the context of a subroutine?
An observable change beyond the return value, such as printing or changing memory or registers.
What are the designated argument and return value registers in RISC-V?
Argument registers are a0 through a7 (arguments 1 to 8); the primary return result is placed in a0, and a1 can hold a second return value under the standard convention.
Which registers are callee-saved in the standard procedure calling convention?
s0 through s11 and sp; the called function must restore their entry values before returning.
Which registers are caller-saved in the standard procedure calling convention?
a0 through a7, t0 through t6, and ra.
Why must ra be saved before making a nested call?
A new JAL overwrites ra, so a non-leaf function must preserve its original return address if it needs to return to its own caller.
What is the difference between a leaf function and a non-leaf function?
A leaf function calls no other function; a non-leaf function calls at least one other function.
What is the assembly code for int sum(int x, int y){return x+y;}?
sum: add a0, a0, a1; ret. Inputs are in a0/a1 and output is in a0.
What does the .globl sum directive do?
Makes the label sum visible to code in other source files.
What is the flat memory model?
A program sees memory as one addressable sequence of bytes, even if physical hardware is more complex.
What does the .text section contain?
Executable instructions or program code.
What does the .data section contain?
Static or global variables and embedded strings allocated for the program's duration.
What does the heap contain?
Dynamically allocated objects and data structures, such as linked-list nodes or trees.
Where do global C variables reside and what is their initial value if uninitialized?
In static storage (.data for initialized variables and normally .bss for zero-initialized or uninitialized globals); their initial value is zero because objects with static storage duration are zero-initialized before program execution.
What is the initial value of an uninitialized automatic local variable in C?
Indeterminate or garbage; it is not automatically initialized merely because stack memory was allocated.
What does the assembler directive .space 20 do?
Reserves 20 bytes; enough space for five 4byte integers if properly aligned.
What does .align 2 mean in the lecture slides?
Aligns the next data item to a 4byte boundary (22 bytes).
How is the 32-bit value 0x12345678 stored at address 0x100 in little-endian format?
The least significant byte is at the lowest address: bytes at 0x100 through 0x103 are 0x78, 0x56, 0x34, 0x12.
How many bytes are in an RV32 int and char?
An int is 4 bytes and a char is 1 byte; a short is commonly 2 bytes.
Which way does the stack grow in memory?
Toward lower memory addresses; allocating a stack frame subtracts from sp.
What is the difference between signed and unsigned byte/halfword loads?
lb and lh sign-extend; lbu and lhu zero-extend. Stores simply write the low 8 or 16 bits.
What is base-plus-offset addressing and what is the load/store immediate offset range?
Effective address = value in base register + signed immediate offset; the offset range is a signed 12bit immediate from −2048 through +2047 bytes.
What does the auipc instruction calculate?
Add Upper Immediate to PC calculates rd = PC + (immediate << 12); useful for PC-relative addresses.
What is the difference between la and lw?
la obtains an address; lw reads the 4byte value stored at an address.
What is memory-indirect addressing, and what is an example of it?
The address of the desired data is itself stored in memory; load that pointer, then load or store through it. Example: lw t0, 0(sp); lw t1, 0(t0).
What is an activation record or stack frame?
The memory reserved for one active function-call instance, containing its context and saved state.
What is a re-entrant subroutine?
A function that can be called again before an earlier call finishes without mixing up different calls' state.
What is a register spill?
A value temporarily placed in memory because there are not enough registers or its value must survive a call.
What is an example of a 16-byte callee frame setup and matching teardown?
Setup: addi sp, sp, -16; sw ra, 12(sp); sw s0, 8(sp). Teardown: lw s0, 8(sp); lw ra, 12(sp); addi sp, sp, 16; ret.
Where are excess arguments passed when a function has over eight integer arguments?
The caller passes the first eight in a0-a7 and places later ones on the stack; argument 9 is at 0(sp) on entry, and argument 10 is at 4(sp).
Where is argument 9 located on the stack after the callee executes addi sp, sp, -16?
At 16(sp); argument 10 is at 20(sp), because the new sp is 16 bytes lower.
What is the formula for the memory address of array element array[i]?
base address + i * sizeof element
(slli t2, t1, 2 # t2 = i * 4 (int = 4 bytes)
add a0, t0, t2 # a0 = address of array[i])
How do you access arr[i] in RISC-V assembly when i is stored in a register?
slli t1, iReg, 2; add t2, baseReg, t1; lw t3, 0(t2). Shift left 2 multiplies by 4.
Why can't lw directly use a variable index register in its offset field?
The offset inside lw/sw must be a constant immediate, so compute a variable element address in a register first.
How is a C string represented in memory?
As an array of character bytes ending with a null byte, '\0', whose numeric value is zero.
How much space does the string "cat" require in C?
Four bytes: 'c', 'a', 't', and the terminating '\0'.
What is the difference between p = &x and *p = 5?
p = &x changes which address p holds; *p = 5 writes 5 into the pointed-to integer.
What does p + 3 mean if p is declared as int *p?
An address 12 bytes beyond p on the class's 4byte-int model; it points three int elements ahead.
What is the difference between an array name and a pointer variable in C?
An array name identifies fixed allocated storage and cannot be reassigned; a pointer variable holds an address that can change.
What is the RISC-V assembly code for swapping the pointed-to values *a0 and *a1?
lw t0, 0(a0); lw t1, 0(a1); sw t1, 0(a0); sw t0, 0(a1); ret.
What is NULL and what happens if you dereference it?
A special null pointer value indicating that the pointer does not refer to a valid object; dereferencing it causes undefined behavior, often a crash.