Back to COMP1521 返回COMP1521

Week 2 — MIPS Programming Basics 第2周 — MIPS编程基础

Introduction to MIPS assembly, system calls, and basic I/O operations MIPS汇编入门、系统调用和基本输入输出操作

📋 Week Overview 📋 本周概览

  • Introduction to MIPS — Architecture basics and registers MIPS简介 — 架构基础和寄存器
  • System Calls — Input/output operations using syscall 系统调用 — 使用syscall进行输入输出
  • Control Instructions — Branching and jumping 控制指令 — 分支和跳转
  • Basic Programs — Grading, counting, sequence generation 基础程序 — 成绩计算、计数、序列生成

🎯 Learning Objectives 🎯 学习目标

  • ✓ Understand MIPS register usage ✓ 理解MIPS寄存器使用
  • ✓ Implement basic I/O operations ✓ 实现基本输入输出操作
  • ✓ Use control flow instructions ✓ 使用控制流指令
  • ✓ Translate simple C programs to MIPS ✓ 将简单C程序翻译为MIPS

📚 Key Concepts 📚 关键概念

  • • MIPS Architecture ($t, $s, $a, $v registers) • MIPS架构 ($t, $s, $a, $v寄存器)
  • • Syscall service codes ($v0 values) • 系统调用服务码 ($v0值)
  • • Branch instructions (beq, bne, blt, etc.) • 分支指令 (beq, bne, blt等)
  • • Memory addressing and data directives • 内存寻址和数据伪指令

MIPS Architecture Introduction MIPS架构介绍

Register Conventions 寄存器约定

Temporary Registers ($t0-$t9) 临时寄存器 ($t0-$t9)

Not preserved across function calls. Use for temporary calculations. 函数调用间不保留。用于临时计算。

Saved Registers ($s0-$s7) 保存寄存器 ($s0-$s7)

Preserved across function calls. Use for persistent values. 函数调用间保留。用于持久化值。

Argument Registers ($a0-$a3) 参数寄存器 ($a0-$a3)

Used for function arguments. First 4 parameters. 用于函数参数。前4个参数。

Value Registers ($v0-$v1) 返回值寄存器 ($v0-$v1)

For function return values and syscall service codes. 用于函数返回值和系统调用服务码。

System Calls Basics 系统调用基础

Common Syscall Service Codes 常用系统调用服务码

Service服务 $v0 Arguments参数 Result结果
Print integer打印整数 1 $a0 = integer -
Print string打印字符串 4 $a0 = address -
Read integer读取整数 5 - $v0 = integer
Read string读取字符串 8 $a0 = buffer, $a1 = length -
Exit program退出程序 10 - -
# Syscall template: print integer
li $v0, 1           # service code for print integer
move $a0, $t0       # integer to print
syscall             # execute system call

# Syscall template: read integer
li $v0, 5           # service code for read integer
syscall             # execute system call
move $t0, $v0       # save result to $t0

Control Instructions 控制指令

Branch Instructions 分支指令

beq rs, rt, label

Branch if equal: if rs == rt, jump to label 相等时分支:如果rs == rt,跳转到label

bne rs, rt, label

Branch if not equal: if rs != rt, jump to label 不等时分支:如果rs != rt,跳转到label

blt rs, rt, label

Branch if less than: if rs < rt, jump to label 小于时分支:如果rs < rt,跳转到label

bgt rs, rt, label

Branch if greater than: if rs > rt, jump to label 大于时分支:如果rs > rt,跳转到label

⚠️ Important: MIPS Pseudo-instructions ⚠️ 重要:MIPS伪指令

Instructions like blt and bgt are pseudo-instructions. They expand to real MIPS instructions using slt (set less than). bltbgt等指令是伪指令。它们会扩展为使用slt(小于时置位)的真实MIPS指令。

Example Programs 示例程序

Grading Program Example 成绩计算程序示例

.data
prompt:     .asciiz "Enter score (0-100): "
grade_a:    .asciiz "Grade: A\n"
grade_b:    .asciiz "Grade: B\n"
grade_c:    .asciiz "Grade: C\n"
grade_f:    .asciiz "Grade: F\n"

.text
main:
    # Print prompt
    li $v0, 4
    la $a0, prompt
    syscall

    # Read integer
    li $v0, 5
    syscall
    move $t0, $v0       # save score in $t0

    # Check grade boundaries
    blt $t0, 0, invalid_score
    bgt $t0, 100, invalid_score

    # Grade A: 85-100
    bge $t0, 85, print_a
    # Grade B: 70-84
    bge $t0, 70, print_b
    # Grade C: 50-69
    bge $t0, 50, print_c
    # Grade F: 0-49
    j print_f

print_a:
    li $v0, 4
    la $a0, grade_a
    syscall
    j exit

print_b:
    li $v0, 4
    la $a0, grade_b
    syscall
    j exit

print_c:
    li $v0, 4
    la $a0, grade_c
    syscall
    j exit

print_f:
    li $v0, 4
    la $a0, grade_f
    syscall
    j exit

invalid_score:
    li $v0, 4
    la $a0, error_msg
    syscall

exit:
    li $v0, 10
    syscall

Counting Program Example 计数程序示例

.data
prompt:     .asciiz "Enter count limit: "
result_msg: .asciiz "Count complete: "
newline:    .asciiz "\n"

.text
main:
    # Get user input for limit
    li $v0, 4
    la $a0, prompt
    syscall

    li $v0, 5
    syscall
    move $t0, $v0       # $t0 = limit

    # Initialize counter
    li $t1, 0           # $t1 = counter (starts at 0)

count_loop:
    # Check if counter >= limit
    bge $t1, $t0, done_counting

    # Increment counter
    addi $t1, $t1, 1

    # Continue loop
    j count_loop

done_counting:
    # Print result message
    li $v0, 4
    la $a0, result_msg
    syscall

    # Print final count
    li $v0, 1
    move $a0, $t1
    syscall

    # Print newline
    li $v0, 4
    la $a0, newline
    syscall

    # Exit program
    li $v0, 10
    syscall

Laboratory Exercises 实验练习

Week 2 Lab Tasks 第2周实验任务

Exercise 1: Grading Program 练习1:成绩计算程序

Write a MIPS program that reads a student's score (0-100) and prints their grade: 编写一个MIPS程序,读取学生成绩(0-100)并打印等级:

  • A: 85-100A: 85-100
  • B: 70-84B: 70-84
  • C: 50-69C: 50-69
  • F: 0-49F: 0-49

Exercise 2: Counting Program 练习2:计数程序

Write a program that reads a number N and counts from 0 to N, printing the final count. 编写一个程序,读取数字N,从0计数到N,打印最终计数值。

Exercise 3: Sequence Generation 练习3:序列生成

Generate and print the first N numbers of a sequence (e.g., even numbers, multiples of 3). 生成并打印序列的前N个数字(例如,偶数、3的倍数)。

💡 Challenge Exercise 💡 挑战练习

Create a program that implements a simple menu system with multiple options using branching. 创建一个使用分支实现简单菜单系统的程序,包含多个选项。

🔗 Additional Resources 🔗 额外资源

Official Lab Questions 官方实验题目

Week 2 Lab Exercises 第2周实验练习

MIPS Reference MIPS参考

Course Notes 课程笔记