Digital Design and Computer Architecture - Lecture 10b: Assembly Programming

This post is a derivative of Digital Design and Computer Architecture Lecture by Prof. Onur Mutlu, used under CC BY-NC-SA 4.0.

You can watch this lecture on Youtube and see pdf.

I write this summary for personal learning purposes.

Intro

What Will We Learn Today?

  • Assembly Programming
    • Programming constructs
    • Debugging
    • Conditional statements and loops in MIPS assembly
    • Arrays in MIPS assembly
    • Function calls
      • The stack

Example. A Program for Adding Intergers in LC-3

See pdf p.11

(Assembly) Programming

Programming Constructs

Programming requires dividing a task, i.e., a unit of work into smaller units of work. The goal is to replace the units of work with programming constructs that represent that part of the task. There are three basic programming constructs.

  1. Sequential construct
  2. Conditional construct
  3. Iterative construct

Debugging

Debugging is the process of removing errors in programs. It consists of tracing the program, i.e., keeping track of the sequence of instructions that have been executed and the results produced by each instruction. A useful technique is to partition the program into parts, often referred to as modules, and examine the results computed in each module.

  • High-level language (e.g., C programming language) debuggers: dbx, gdb, Visual Studio debugger.
  • Machine code debugging: Elementary interactive debugging operations.

Conditional statements and loops in MIPS assembly

  • If Statement
    • In MIPS, we create conditional constructs with conditional branches (e.g., beq, bne…)
  • If-Else Statement
    • We use the unconditional branch (i.e., j) to skip the “else” subtask if the “if” subtask is the correct one.
  • While Loop
    • As in LC-3, the conditional branch (i.e., beq) checks the condition and the unconditional branch (i.e., j) jumps to the beginning of the loop.
  • For Loop
    • The implementation of the ”for” loop is similar to the ”while” loop.
  • For Loop Using SLT
    • We use slt (i.e., set less than) for the ”less than” comparison.

Arrays in MIPS assembly

Accessing an array requires loading the base address into a register. In MIPS, this is something we cannot do with one single immediate operation. Load upper immediate + OR immediate (lui + ori)

Function Calls

Functions have arguments and return value.

  • Caller: calling function
    • passes arguments
    • jumps to callee
  • Callee: called function
    • performs the procedure
    • returns the result to caller
    • returns to the point of call
    • must not overwrite registers or memory needed by the caller

The Stack

The stack is a memory area used to save local variables. It is a Last-In-First-Out (LIFO) queue.

Lecture Summary

  • Assembly Programming

    • Programming constructs

    • Debugging
    • Conditional statements and loops in MIPS assembly
    • Arrays in MIPS assembly
    • Function call
      • The stack

Leave a comment