Seyed Masoud Hosseini · Overview · Study log · Ideas · Transcript · RSS feed

Digital Design & Computer Architecture · Lecture 12 of 37 · 39:58

Lecture 9c: Assembly Programming

Digital Design and Computer Architecture - L9c:  Assembly Programming (Spring 2025) on YouTube

Study guide

What this lecture covers

This is a shorter, practice-focused companion lecture: instead of ISA theory, it shows how to actually write programs in LC-3 and MIPS assembly. It answers a concrete question: given conditional branches, load/store instructions and a handful of registers, how do you build loops, arrays, and function calls that behave correctly? It builds directly on the earlier LC-3 and MIPS instruction coverage in this course, and its stack and calling-convention material feeds into the labs, where students write MIPS programs by hand.

You'll see a full LC-3 program that sums an array of integers using a loop, a discussion of how to debug assembly by stepping through instructions and setting breakpoints, and MIPS examples of conditionals, loops, arrays, and function calls with the stack. After watching, you should be able to translate simple control structures into branch-based assembly and follow the caller/callee conventions used to pass arguments, return values and preserve registers across function calls.

Key ideas

  • Programming constructs map to branches: sequential, conditional and iterative constructs (if/else, while, for) are all built from conditional and unconditional branch instructions in both LC-3 and MIPS.
  • Sentinel values: a special value such as EOT (end of text) is used to mark the end of input, letting a loop test for termination rather than counting fixed iterations.
  • Trap instructions: LC-3's TRAP instruction is the operating system interface, used here to read a character from the keyboard, write to the monitor, and halt the program.
  • Interactive debugging: stepping one instruction at a time, examining registers and memory after each step, and setting breakpoints are the core techniques for finding bugs such as an incorrectly chosen branch condition.
  • Boundary conditions matter: the lecture's buggy multiply example shows how failing to check a zero or edge-case input can produce wrong results even when the main loop logic looks correct.
  • Calling convention: MIPS reserves a0-a3 for arguments, v0 for the return value, and ra for the return address set by jal; LC-3 uses R7 for the return address set by JSR/JSRR.
  • Register saving convention: temporary registers are "caller-saved" (not preserved across a call), while saved registers like s0 are "callee-saved," which avoids unnecessary save/restore work when everyone follows the convention.
  • The stack for spilling: a last-in-first-out memory region, addressed by a stack pointer, used to save registers (including ra, for nested calls) before a function overwrites them and restore them before returning.

Walkthrough

Building a first LC-3 loop program (0:04)

The lecture opens with a complete example: summing 12 integers stored at consecutive memory addresses. It shows the design process end to end, from a flowchart with explicit register assignments (address pointer, running sum, remaining count) to the corresponding LC-3 machine instructions, including LEA to load the base address, ADD in both register and immediate forms, and a conditional branch that tests whether the counter has reached zero before deciding to loop again or exit.

Programming constructs and a larger example program (5:05)

The lecture defines the three basic programming constructs, sequential, conditional and iterative, and explains how each corresponds to specific instruction patterns. It then sketches a program that counts occurrences of a character in a text file, using a sentinel end-of-text character to terminate the loop and TRAP instructions to read keyboard input and write output to the monitor.

Debugging assembly programs by hand (12:08)

The lecture covers interactive debugging operations, running to a breakpoint, stepping instruction by instruction, and examining registers and memory, then applies them to a buggy LC-3 multiplication routine. By annotating each instruction and tracking register values, it shows that a branch condition was checking the wrong flag (BRzp instead of BRp), causing one extra loop iteration, and separately notes the program also fails to handle a zero input correctly.

Conditionals and loops in MIPS (18:10)

Switching to MIPS, the lecture builds an if-statement using bne to skip an add, then extends it to if/else using both a conditional branch and an unconditional jump. It works through while and for loop translations, including a power-of-two calculation and a loop that uses slt (set less than) to compute a comparison result directly into a register instead of relying on condition codes.

Working with arrays in MIPS (25:16)

Because a 32-bit address cannot be loaded with a single immediate instruction, the lecture shows how lui and ori combine to construct a full base address, then how array elements are accessed by adding an offset equal to the index times the word size. Multiplying by four is done with a left shift rather than a multiply instruction, since shifting is simpler and faster in hardware.

Function calls and the stack (28:18)

The lecture defines caller and callee terminology, then shows MIPS jal/jr $ra and LC-3 JSR/RET for calling and returning. A worked example passes arguments in a0-a3 and returns a value in v0. When a function needs registers that might already be in use by its caller, or must preserve ra across a nested call, the lecture introduces the stack: pushing values to free up registers safely and popping them back before returning, following the caller-saved versus callee-saved convention.

Before you watch

  • Review LC-3's instruction set, addressing modes and condition codes, and MIPS's basic instruction formats, covered in this course's earlier ISA lectures.
  • Be familiar with the LC-3 datapath and instruction processing cycle referenced when tracing through the loop example.
  • Know how branches and jumps are encoded, since the whole lecture builds control structures out of them.

Check your understanding

  1. Why does an if/else statement in MIPS need both a conditional branch and an unconditional jump, while a simple if statement only needs the conditional branch?
  2. In the buggy LC-3 multiply program, what specific bug caused the loop to run one iteration too many, and how would you find it using instruction-by-instruction debugging?
  3. What is the difference between a caller-saved and a callee-saved register in the MIPS calling convention, and why does that distinction reduce unnecessary stack traffic?
  4. Why must a function save ra on the stack before making a nested function call?

From the YouTube description

Digital Design and Computer Architecture, ETH Zürich, Spring 2025 (https://safari.ethz.ch/ddca/spring2025/)

Lecture 9c: Assembly Programming
Lecturer: Prof. Onur Mutlu
Date: 21 March 2025

Lecture 9c Slides (pptx): https://safari.ethz.ch/ddca/spring2025/lib/exe/fetch.php?media=onur-ddca-2025-lecture9c-assembly-programming-beforelecture.pptx
Lecture 9c Slides (pdf): https://safari.ethz.ch/ddca/spring2025/lib/exe/fetch.php?media=onur-ddca-2025-lecture9c-assembly-programming-beforelecture.pdf

Recommended Reading:
====================
A Modern Primer on Processing in Memory
https://arxiv.org/pdf/2012.03112.pdf

Memory-Centric Computing: Solving Computing's Memory Problem
https://www.arxiv.org/pdf/2505.00458

Memory-Centric Computing: Recent Advances in Processing-in-DRAM
https://arxiv.org/pdf/2412.19275

Intelligent Architectures for Intelligent Computing Systems
https://people.inf.ethz.ch/omutlu/pub/intelligent-architectures-for-intelligent-computingsystems-invited_paper_DATE21.pdf

RowHammer: A Retrospective
https://people.inf.ethz.ch/omutlu/pub/RowHammer-Retrospective_ieee_tcad19.pdf

Fundamentally Understanding and Solving RowHammer
https://arxiv.org/pdf/2211.07613.pdf

Accelerating Genome Analysis via Algorithm-Architecture Co-Design
https://people.inf.ethz.ch/omutlu/pub/AcceleratingGenomeAnalysis_dac23.pdf

From Molecules to Genomic Variations: Accelerating Genome Analysis via Intelligent Algorithms and Architectures
https://people.inf.ethz.ch/omutlu/pub/IntelligentGenomeAnalysis_csbj22.pdf

RECOMMENDED LECTURE VIDEOS & PLAYLISTS:
========================================
Digital Design and Computer Architecture Spring 2025 Livestream Lectures Playlist:
https://www.youtube.com/watch?v=ubhxKNlOlRg&list=PL5Q2soXY2Zi9Eo29LMgKVcaydS7V1zZW3&index=3

Fundamentals of Computer Architecture Fall 2025 Livestream Lectures Playlist:
https://www.youtube.com/watch?v=uKgMFj1eQQc&list=PL5Q2soXY2Zi_ZMtqz1r-GHm-zzuE1QfIg&index=2

Seminar in Computer Architecture Spring 2025 Livestream Lectures Playlist:
https://www.youtube.com/watch?v=rqeKNZrLzng&list=PL5Q2soXY2Zi-oIW66TLOjtiqQxlDwNHng&index=2

Computer Architecture Fall 2024 Lectures Playlist:
https://www.youtube.com/watch?v=ziMRjDlLEwo&list=PL5Q2soXY2Zi-LfDdGgWyLcTSqzm6a26wD&index=2

Interview with Professor Onur Mutlu:
https://www.youtube.com/watch?v=8ffSEKZhmvo&list=PL5Q2soXY2Zi8VrmOTz44l2WupethSdh-M&index=9

TCuARCH meets Prof. Onur Mutlu
https://www.youtube.com/watch?v=6Hpn4SAX0dI

Arch. Mentoring Workshop @ISCA'21 - Doing Impactful Research
https://www.youtube.com/watch?v=83tlorht7Mc

The Story of RowHammer Lecture:
https://www.youtube.com/watch?v=sgd7PHQQ1AI&list=PL5Q2soXY2Zi8D_5MGV6EnXEJHnV2YFBJl&index=39

Accelerating Genome Analysis Lecture:
https://www.youtube.com/watch?v=r7sn41lH-4A&list=PL5Q2soXY2Zi8D_5MGV6EnXEJHnV2YFBJl&index=41

Memory-Centric Computing Systems Tutorial at IEDM 2021:
https://www.youtube.com/watch?v=H3sEaINPBOE&list=PL5Q2soXY2Zi8D_5MGV6EnXEJHnV2YFBJl&index=35

Intelligent Architectures for Intelligent Machines Lecture:
https://www.youtube.com/watch?v=GTieZPY4Wmc&list=PL5Q2soXY2Zi8D_5MGV6EnXEJHnV2YFBJl&index=38

Featured Lectures:
https://www.youtube.com/watch?v=jVYCchBGNVc&list=PL5Q2soXY2Zi8VrmOTz44l2WupethSdh-M&index=1

← Lecture 9: ISA and Microarchitecture · Lecture 10: Microarchitecture Fundamentals and Design II →