Introduction to MIPS assembly, system calls, and basic I/O operations MIPS汇编入门、系统调用和基本输入输出操作
Not preserved across function calls. Use for temporary calculations. 函数调用间不保留。用于临时计算。
Preserved across function calls. Use for persistent values. 函数调用间保留。用于持久化值。
Used for function arguments. First 4 parameters. 用于函数参数。前4个参数。
For function return values and 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
beq rs, rt, labelBranch if equal: if rs == rt, jump to label 相等时分支:如果rs == rt,跳转到label
bne rs, rt, labelBranch if not equal: if rs != rt, jump to label 不等时分支:如果rs != rt,跳转到label
blt rs, rt, labelBranch if less than: if rs < rt, jump to label 小于时分支:如果rs < rt,跳转到label
bgt rs, rt, labelBranch if greater than: if rs > rt, jump to label 大于时分支:如果rs > rt,跳转到label
Instructions like blt and bgt are pseudo-instructions. They expand to real MIPS instructions using slt (set less than).
blt和bgt等指令是伪指令。它们会扩展为使用slt(小于时置位)的真实MIPS指令。
.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
.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
Write a MIPS program that reads a student's score (0-100) and prints their grade: 编写一个MIPS程序,读取学生成绩(0-100)并打印等级:
Write a program that reads a number N and counts from 0 to N, printing the final count. 编写一个程序,读取数字N,从0计数到N,打印最终计数值。
Generate and print the first N numbers of a sequence (e.g., even numbers, multiples of 3). 生成并打印序列的前N个数字(例如,偶数、3的倍数)。
Create a program that implements a simple menu system with multiple options using branching. 创建一个使用分支实现简单菜单系统的程序,包含多个选项。