📚

[Lecture 9b] Understanding LC3 and MIPS Assembly

Apr 9, 2025

Lecture Notes: LC3 and MIPS Assembly

Introduction

  • Focus on LC3 and MIPS assembly.
  • Discuss differences in instructions and similarities.
  • Cover programming constructs, debugging principles, conditional statements, loops, arrays, and function calls.
  • Context: One model Oney machine and instruction cycle.

Building LC3 Programs

  • Example: Simple program with 12 integers stored from addresses hex 3100 to hex 31OB.
  • Problem-solving approach: Translate problem to an algorithm using flowcharts.
  • Setup:
    • Address of first integer in 3100.
    • Counter in R3, number of integers in R2.
  • Process:
    • Initialize registers.
    • Check if R2 (number of integers left) is zero.
    • If not, load integer, add, increment address, decrement counter.
    • Loop until done; output result.

LC3 Assembly Instructions

  • Use of conditional branches to create loops.
  • Instructions like load effective address for managing addresses.
  • Importance of correctly managing branch targets.

Debugging LC3 Programs

  • Debugging focuses on tracing program execution and verifying results.
  • Useful technique: Partition program into parts/modules.
  • Interactive debugging operations:
    • Put values into memory and registers.
    • Execute instruction sequences.
    • Set breakpoints to stop execution.

Programming Constructs in MIPS

  • Importance of divide and conquer in programming.
  • Three basic constructs:
    • Sequential: Execute tasks in sequence.
    • Conditional: Use if/else constructs to execute subtasks based on conditions.
    • Iterative: Use loops to repeat tasks based on conditions.

MIPS Programming

  • Conditional constructs with conditional branches.
  • Usage of Branch if Equal.
  • Implementation of loops using conditional and unconditional branches.
    • Example: While loop to calculate power of two.
    • Example: For loop to add integers.

Arrays in MIPS

  • Accessing arrays involves loading base address into a register.
  • Load upper immediate and or immediate for loading addresses.

Function Calls and Conventions

  • Need for functions to enhance code reuse and readability.
  • Function call conventions:
    • Arguments in registers a0 to a3.
    • Return value in v0.
    • Jump and Link (jal) for calling functions.
    • Jump Register (jr) for returning from functions.

Stack Usage for Function Calls

  • Stack: To save local variables and registers during function calls.
  • Stack Pointer: Points to top of the stack.
    • Usage: Save registers before function call and restore after.
    • Conventions:
      • Temporary registers can be overwritten.
      • Preserved registers (s0, s1, ...) must be saved and restored.

Conclusion

  • Overview of LC3 and MIPS assembly programming.
  • Emphasized debugging, conditional branches, loops, arrays, and function call conventions.
  • Key takeaway: Understanding and following conventions is crucial for effective assembly programming.