MIPS

本文最后更新于 2024年10月6日 晚上

数据声明

1
2
3
4
5
6
.data 
pi:.double 3.145926
str:.asciiz "hello, world\n"
i:.word 3
array1:.byte 'a','b'#声明一个存储2个字符的数组
array2:.space 40#声明一个存放40字节的空间

基本运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sub	$t2,$t3,$t4	#  $t2 = $t3 - $t4
addi $t2,$t3, 5 # $t2 = $t3 + 5; "add immediate" (no sub immediate)
addu $t1,$t6,$t7 # $t1 = $t6 + $t7; add as unsigned integers
subu $t1,$t6,$t7 # $t1 = $t6 + $t7; subtract as unsigned integers

mult $t3,$t4 # multiply 32-bit quantities in $t3 and $t4, and store 64-bit
# result in special registers Lo and Hi: (Hi,Lo) = $t3 * $t4
                         # 运算结果存储在hi,lo(hi高位数据, lo地位数据)
div $t5,$t6 # Lo = $t5 / $t6 (integer quotient)
# Hi = $t5 mod $t6 (remainder)
                         # 商数存放在 lo, 余数存放在 hi
mfhi $t0 # move quantity in special register Hi to $t0: $t0 = Hi
# 不能直接获取 hi 或 lo中的值, 需要mfhi, mflo指令传值给寄存器
mflo $t1 # move quantity in special register Lo to $t1: $t1 = Lo
# used to get at result of product or quotient

move $t2,$t3 # $t2 = $t3

Syscall

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
.data
myFloat:.float 123.456
pi:.double 3.1415926
zeroDouble:.double 0
enter:.asciiz "\n"
i:.word 3
.macro ent
li $v0, 4
la $a0, enter
syscall
.end_macro

.macro done
li $v0, 10
syscall
.end_macro

.macro writeInt(%k)
move $a0, %k
li $v0, 1
syscall
.end_macro

.text
#print integer
li $v0, 1
li $a0, 12
syscall

ent
#print float
li $v0, 2
ldc1 $f12, myFloat
syscall

ent
#print double
li $v0,3
ldc1 $f2, pi
ldc1 $f0, zeroDouble
add.d $f12, $f2, $f0
syscall

ent
#use macro as a write function
li $t0, 4
writeInt($t0)

ent
#read and then print integer
li $v0, 5
syscall
add $a0, $0, $v0
li $v0, 1
syscall

ent
#read string
li $v0, 8
li $a1, 100
syscall
li $v0, 4
syscall

macro

堆栈

1
2
3
4
5
6
7
8
9
10
#放入堆栈
.macro push(%i)
addi $sp, $sp, -4
sw %i, 0($sp)
.end_macro
#从堆栈中取出
.macro pop(%i)
lw %i, 0($sp)
addi $sp, $sp, 4
.end_macro

for循环(从0到n)

C语言:

1
2
3
for (int i = 0; i < n; i++){
// body
}

MIPS

1
2
3
4
5
6
7
8
9
10
11
.macro for_begin(%startLabel, %endLabel, %n)
li $t7, 0
%startLabel:
bge $t7, %n, %endLabel
.end_macro

.macro for_end(%startLabel, %endLabel)
addi $t7, $t7, 1
j %startLabel
%endLabel:
.end_macro

for循环(从low到high)

C

1
2
3
for (int i = low; i < high; i++){
// body, 循环high-low次
}

MIPS

1
2
3
4
5
6
7
8
9
10
11
.macro for_begin(%startLabel, %endLabel, %low, %high)
move $t7, %low
%startLabel:
bge $t7, %high, %endLabel
.end_macro

.macro for_end(%startLabel, %endLabel)
addi $t7, $t7, 1
j %startLabel
%endLabel:
.end_macro

while循环(两数比较)

C

1
2
3
while (num1 < num2){
// body
}

MIPS

1
2
3
4
5
6
7
8
9
.macro while_begin(%startLabel, %endLabel, %num1, %num2)
%startLabel:
bge %num1, %num2, %endLabel
.end_macro

.macro while_end(%startLabel, %endLabel)
j %startLabel
%endLabel:
.end_macro

MIPS
https://meteor041.git.io/2024/09/23/MIPS/
作者
meteor041
发布于
2024年9月23日
许可协议