Classroom Public page

Week 9: VM II (Program Flow + Function Calls)

596 words

Finish the virtual machine. Add labels, conditional jumps, and the full function-call protocol. By end of week your VM can execute recursive factorial on the silicon you built; you can trace stack frames with gdb.


Reading

  • Chapter prose (primary). draft-chapters/ch8-vm-ii-prose.md
  • Petzold weave anchors. Ch 17 Automation pp. 234-235 ("It's better not to use actual numeric addresses... use labels"); Ch 22 The Operating System p. 330 (the third function of an OS; "a program running under CP/M makes use of a collection of subroutines..."); Ch 25 The Graphical Revolution pp. 371-373 (first Ch 25 citation; "A graphical operating system... has hundreds of API functions"; clicks-as-function-call-chains)
  • Cross-chapter handouts. VM segment cheat sheet, now extended with frame-pointer protocol

Lecture

lectures/ch8-vm-ii-lecture.md. 3 hours. Key arc:

  • Labels and goto. The VM-level abstraction of "branch to this point in the program"
  • Conditional jumps. if-goto label pops the stack; jumps if the popped value is non-zero
  • Function calls. The big one. call f n pushes a return address, saves caller state, jumps to f; return pops the saved state, jumps back. The protocol that lets functions compose
  • Frame pointers. The bookkeeping that makes local variables addressable across nested calls
  • Recursion. Once function calls work, recursion is free; recursive factorial is the canonical demo

Lab exercises

Five labs in worksheets/ch8/.

Plan for ~6 hours of lab. Lab 8.5 is a Tier-1 calibration that pairs naturally before Lab 8.2 (function-call translator); having the visualizer's frame-card layout in mind makes the translator's emit-the-saved-frame work concrete.

Independent practice

  • Read Petzold Ch 25 pp. 371-373 carefully. This is the first Ch 25 citation in the course. Ch 25 returns in Ch 9, 10, 11, and 12 (final paragraph of the book closes the curriculum). Notice the thesis: a click on a window is a chain of function calls, and you have just written the function-call infrastructure that makes such chains possible
  • Update your Toolchain Diary. Week 9 introduces: gdb (with set architecture riscv:rv32), gdb's bt (backtrace), gdb's info frame, the academy's vm-translator --emit-debug flag

Architecture comparison sidebar

The Virtus calling convention is a teaching simplification. Compare to ARM AAPCS (32-bit ARM Procedure Call Standard): callee-save and caller-save register sets; structured argument passing; the link register vs the stack-stored return address. Compare to x86_64 System V ABI: register-passed arguments for the first six integer args; xmm0-xmm7 for floats; the red zone optimization. Your Virtus calling convention is closer to AAPCS in spirit.

Reflection prompts

  1. Function calls compose: a function can call another function, which can call another, arbitrarily deep. What hardware support makes this possible? Could you do it without a stack?
  2. Recursion is free once function calls work. Why isn't every algorithm written recursively?
  3. Petzold introduces "API" in Ch 25 as a count: "hundreds of API functions" in a graphical OS. By Ch 12 you will have implemented ~34 API functions in your Virtus OS. What does "hundreds" tell you about the scale of a production OS API?

What's next

Week 10 starts the compiler. The Jack-equivalent high-level language gets its tokenizer and parser. You build the recursive-descent expression parser that is the heart of every compiler frontend ever written.