Skip to content

CS 315-02 Lecture/Lab Meeting Summary — Fall 2025

  • Date: September 10, 2025
  • Time: 06:31 PM Pacific Time (US and Canada)
  • Meeting ID: 868 6589 0521

Quick Recap

The session covered fundamentals of function calls and register management in assembly language, including how the Program Counter (PC) and Return Address (RA) work during calls and returns. Greg reviewed stack memory allocation, register preservation conventions, and the role of the Stack Pointer (SP) in function management. The meeting concluded with practical demonstrations, including a “Hello, World” example and exercises involving function calls and register handling. Upcoming sessions will cover calling conventions and debugging techniques.

Next Steps

  • Implement the findMax_fc function, which calls another function inside a loop.
  • Review the lecture summary notes posted on CampusWire.
  • Work on Project 2 (more involved than Lab 3).
  • Practice implementing complex functions that call other functions while following register preservation conventions.
  • Allocate stack space in multiples of 16 bytes.
  • Preserve all required caller-saved registers when making function calls.
  • Restore the stack pointer after function calls by adding back exactly what was subtracted.

Summary

Function Call Mechanisms

  • Greg explained the transition from simple to more complex functions, focusing on the mechanics of function calls and the roles of key registers:
  • The call instruction saves the return address in RA (the address of the next instruction) and sets the PC to the target function.
  • This foundation supports the FindMaxFC problem, where one function calls another.
  • Lecture notes and recordings from the previous day are available.

Virtual Memory and Instruction Limits

  • While a 64-bit PC can theoretically address a vast instruction space, practical limits come from physical memory.
  • Modern systems use virtual memory: the PC points to a virtual address that the OS maps to a physical address, enabling multiple programs to run concurrently.
  • In practice, program size is more often limited by data than by code; large applications (e.g., video editors) typically consume memory for data processing.

Return Address: Concepts and Risks

  • A return updates the PC to the RA set by the caller.
  • If a function overwrites RA and fails to restore it, execution may return to the wrong location, potentially causing infinite loops or crashes.
  • Preserving and restoring the original RA is essential.

Stack Memory and Register Management

  • The stack is managed via the Stack Pointer (SP):
  • Allocate and deallocate stack space using SP, in multiples of 16 bytes.
  • Follow conventions for saving and restoring registers across function calls.
  • Greg noted that these rules would be applied to a targeted problem related to function calls and RA.

Register Conventions Overview

  • Key registers:
  • x0: always 0
  • x1: RA (return address)
  • x2: SP (stack pointer)
  • Global and thread pointers: not used in this class (focus is on sequential programs)
  • A, T, and S registers:
  • A (argument) registers: hold function arguments and return values.
  • T (temporary) registers: caller-saved.
  • S (saved) registers: callee-saved.
  • Distinction:
  • Caller-saved: the caller must preserve them before making a call.
  • Callee-saved: the callee must save and restore them if modified.

Stack Operations in Assembly

  • Demonstrated:
  • Allocating space on the stack.
  • Storing and retrieving values.
  • Restoring the stack to its original position after a function call.
  • Emphasis on preserving registers and maintaining SP correctly.
  • Introduced shorthand notation for addressing with offsets from SP.

Function Call Demonstration (“Hello, World”)

  • Showed how call/return work at the hardware level:
  • RA is saved (often on the stack) and PC is updated during calls.
  • Identified and fixed a bug by adding a stack adjustment to ensure correct return behavior.

Stack Management Best Practices

  • Properly manage stack space and preserve caller-saved registers, even for simple calls.
  • Demonstrated translating a C function that adds four numbers into assembly:
  • Allocate/deallocate stack space.
  • Preserve necessary registers.
  • Set up arguments and manage temporaries on the stack.

Compiler, Debugging, and Exercises

  • The C compiler handles much of the bookkeeping, but understanding conventions is crucial.
  • Recommended using GDB for debugging.
  • Next session will review calling conventions, rules, and sample code, including a recursive function.
  • Introduced a new exercise involving a Max function:
  • Transform a loop with a conditional into a function call while preserving necessary registers.
  • Encourage reasoning about which registers must be saved (rather than saving all caller-saved registers).
  • Demonstrated using ChatGPT to verify register preservation rules.

Key Conventions to Remember

  • Allocate stack space in 16-byte increments.
  • Preserve caller-saved registers before function calls.
  • Restore SP precisely after calls (add back exactly what was subtracted).
  • Ensure RA is preserved and restored correctly to avoid incorrect returns.