CPE 2300 Midterm 2 Review: Subroutines, Memory, Stack, Arrays, Strings, and Pointers

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/54

flashcard set

Earn XP

Description and Tags

Comprehensive flashcard deck for CPE 2300 Midterm 2 covering subroutines, memory mapping, memory access instructions, stack frames, arrays, strings, and pointers.

55 Terms

1
New cards

What is a subroutine?

A reusable block of code that performs a task and returns control to its caller; similar to a C function.

2
New cards

Why are subroutines used in programming?

To reuse tested code, organize programs into smaller tasks, and create libraries.

3
New cards

What two RISC-V instructions are used to call subroutines?

JAL (jump and link) and JALR (jump and link register).

4
New cards

What does the JAL instruction do?

Jumps to a PC-relative target and saves the address of the next instruction (usually PC+4\text{PC} + 4) in a destination register.

5
New cards

What does "link" mean in Jump-and-Link?

Saving the return address so execution can resume in the caller after the subroutine finishes.

6
New cards

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.

7
New cards

Why can JAL only reach nearby code?

Its PC-relative signed jump offset has limited bits, giving a range of about plus or minus 1 MiB1\,\text{MiB}.

8
New cards

What does the JALR instruction do?

Jumps indirectly using an address held in a register plus a 12 bit12\,\text{bit} signed immediate; it can save a return address.

9
New cards

What can JALR do that JAL cannot?

Call a function through a register-held address, including distant targets or function pointers.

10
New cards

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.

11
New cards

What does the pseudo-instruction ret expand to and do?

Pseudo-instruction for jalr x0, 0(ra); resume execution at the address in ra.

12
New cards

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.

13
New cards

What are the two minimum requirements for a callable subroutine?

A starting address and a return location; together they provide linkage.

14
New cards

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.

15
New cards

What are the designated argument and return value registers in RISC-V?

Argument registers are a0 through a7 (arguments 11 to 88); the primary return result is placed in a0, and a1 can hold a second return value under the standard convention.

16
New cards

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.

17
New cards

Which registers are caller-saved in the standard procedure calling convention?

a0 through a7, t0 through t6, and ra.

18
New cards

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.

19
New cards

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.

20
New cards

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.

21
New cards

What does the .globl sum directive do?

Makes the label sum visible to code in other source files.

22
New cards

What is the flat memory model?

A program sees memory as one addressable sequence of bytes, even if physical hardware is more complex.

23
New cards

What does the .text section contain?

Executable instructions or program code.

24
New cards

What does the .data section contain?

Static or global variables and embedded strings allocated for the program's duration.

25
New cards

What does the heap contain?

Dynamically allocated objects and data structures, such as linked-list nodes or trees.

26
New cards

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.

27
New cards

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.

28
New cards

What does the assembler directive .space 20 do?

Reserves 2020 bytes; enough space for five 4 byte4\,\text{byte} integers if properly aligned.

29
New cards

What does .align 2 mean in the lecture slides?

Aligns the next data item to a 4 byte4\,\text{byte} boundary (222^2 bytes).

30
New cards

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.

31
New cards

How many bytes are in an RV32 int and char?

An int is 44 bytes and a char is 11 byte; a short is commonly 22 bytes.

32
New cards

Which way does the stack grow in memory?

Toward lower memory addresses; allocating a stack frame subtracts from sp.

33
New cards

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 88 or 1616 bits.

34
New cards

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 12 bit12\,\text{bit} immediate from −2048-2048 through +2047+2047 bytes.

35
New cards



36
New cards

What does the auipc instruction calculate?

Add Upper Immediate to PC calculates rd = PC + (immediate << 12); useful for PC-relative addresses.

37
New cards

What is the difference between la and lw?

la obtains an address; lw reads the 4 byte4\,\text{byte} value stored at an address.

38
New cards

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).

39
New cards

What is an activation record or stack frame?

The memory reserved for one active function-call instance, containing its context and saved state.

40
New cards

What is a re-entrant subroutine?

A function that can be called again before an earlier call finishes without mixing up different calls' state.

41
New cards

What is a register spill?

A value temporarily placed in memory because there are not enough registers or its value must survive a call.

42
New cards

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.

43
New cards

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 99 is at 0(sp) on entry, and argument 1010 is at 4(sp).

44
New cards

Where is argument 9 located on the stack after the callee executes addi sp, sp, -16?

At 16(sp); argument 1010 is at 20(sp), because the new sp is 1616 bytes lower.

45
New cards

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])

46
New cards

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 22 multiplies by 44.

47
New cards

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.

48
New cards

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.

49
New cards

How much space does the string "cat" require in C?

Four bytes: 'c', 'a', 't', and the terminating '\0'.

50
New cards

What is the difference between p = &x and *p = 5?

p = &x changes which address p holds; *p = 5 writes 55 into the pointed-to integer.

51
New cards

What does p + 3 mean if p is declared as int *p?

An address 1212 bytes beyond p on the class's 4 byte4\,\text{byte}-int model; it points three int elements ahead.

52
New cards

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.

53
New cards

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.

54
New cards



55
New cards

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.