Overview
This lecture reviewed core MIPS assembly concepts, expanded on loop control with break and continue, introduced memory/data management, and demonstrated working with variables, alignment, and pointers.
Announcements & Revision Sessions
- C revision session on June 12th, 10:00 a.m.–12:00 p.m. via Blackboard Collaborate.
- More details are available on the forum under Announcements.
Recap of Previous Lectures
- Covered register-based arithmetic and system calls for input/output.
- Explored simplifying loops and conditionals into assembly (using jumps and branches).
- Explained register naming and basic assembly instructions like
li (load immediate), la (load address), and move.
Loops: break and continue
break exits a loop immediately, often equivalent to a goto to the loop end label in assembly.
continue skips to the next iteration, acting like a goto to the loop step label.
- Use of
break is discouraged for program clarity, similar to goto.
Data and Memory in MIPS
- RAM stores program data and is close to the CPU for fast access.
- Memory has sections: text (code), data, stack, and kernel sections.
- Text section stores executable code; data section stores global variables.
C Data Types and MIPS Equivalents
char: 1 byte, int: 4 bytes, pointer: 4 bytes (on 32-bit MIPS).
- Arrays are contiguous memory sequences of the same type; structs can mix types.
- Local variables are best stored in registers or on the stack; globals are in the data segment.
Initializing Data in Assembly
.word allocates 4 bytes (an integer), .half for 2 bytes, .byte for 1 byte, .asciiz for strings (zero-terminated).
.space n allocates n bytes without initialization.
- Labels are used to reference memory locations rather than hardcoded addresses.
Loading and Storing Data
- CPU arithmetic works only with register values; load from memory to registers before operating.
- Use
lb/sb (load/store byte), lh/sh (halfword), lw/sw (word) for memory access.
- Addressing is often little-endian: lower addresses store the least significant bytes.
- Prefer label-based access for maintainability and assembler assistance.
Memory Alignment
- Data types must be stored at addresses divisible by their size (e.g., 4-byte ints at addresses divisible by 4).
- Misaligned memory access causes exceptions.
- Use
.align to ensure correct alignment and avoid manual calculation when changing data layout.
Working with Pointers
- Use labels to get addresses; no need for explicit
& as in C.
- Load/store via registers holding addresses; can read or modify memory through pointer dereferencing in assembly.
Instruction Encoding (Bonus)
- If assembler lacks an instruction,
.word can directly inject machine code instruction.
- This technique is non-standard but can be used for unsupported instruction set extensions.
Key Terms & Definitions
- Register — A small, fast storage location in the CPU.
- Immediate — A constant value embedded in an instruction.
- Label — Name for a memory address used in assembly.
- Alignment — Placing data at an address divisible by its size for correct access.
- Pointer — A variable that stores a memory address.
- Data Segment — Memory area used for global/static variables.
Action Items / Next Steps
- Attend the June 12th C revision session.
- Review use of
.align, .space, and label-based memory access in MIPS.
- Prepare for next lecture on 1D and 2D arrays and structs.
- Post questions or discussion points on the course forum.