- MIPS指令知识的学习:
- 环境配置
- 从fpt://ftp.cs.wisc.edu目录PUB/SPIM中下载SPIM
- 运行pcspim.exe解压缩文件
- 在解压缩后的文件夹中运行SETUP.EXE安装
- 转到安装目录下运行pcspim.exe
- 模拟器界面:
- 按照MIPS指令规则编写汇编代码,(后缀名使用asm,s)用PCSpim打开运行。
- 程序结构学习(从hello world开始)
1: # author : See-See
2: # text segment
3: .text
4: .globl main
5: main: # execution starts here
6: la $a0,str # put string address into a0
7: li $v0,4 # system call to print
8: syscall # out a string
9: li $v0,10
10: syscall # exit
11: #data segment
12: .data
13: str: .asciiz "hello world\n"
- 效果图
- 程序设计实例
1: # author : lijian
2: # date: 2012-01-04 21:00:30
3: # function: use mips output
4:
5: .data
6: # variable declarations here
7: msg: .asciiz "\nthis is a message to show!\n" #declared for string
8: inter: .word 168 #declared a interger
9: char: .byte 'a' #declared a character
10:
11:
12: .text
13: main: # indicates start of code
14:
15: # show interger
16: li $v0, 1 # $v0 <= 1
17: lw $a0, inter # $a0 <= inter
18: syscall
19: # show string
20: li $v0, 4 # $v0 <= 4
21: la $a0, msg # $a0 <= msg
22: syscall
23:
24: li $v0, 10 #system call code for exit = 10
25: syscall #call operating system to exit
- 效果图
- 输入实例
1: # author : lijian
2: # date: 2012-01-04 21:35:31
3: # function: use mips input
4:
5: .data
6: # variable declarations here
7: msg: .space 40 #allocate 40 consecutive bytes,
8: msg1: .asciiz "\ninput a integer: " # declared for string
9: msg2: .asciiz "\ninput a string: " # declared for string
10: msg3: .asciiz "\nyou input: " # declared for string
11: .text
12: main: # indicates start of code
13:
14: # input integer
15: li $v0, 4
16: la $a0, msg1
17: syscall
18: li $v0, 5 #read integer
19: syscall
20: move $a1, $v0
21: li $v0, 4
22: la $a0, msg3
23: syscall
24: li $v0, 1
25: move $a0, $a1
26: syscall
27:
28: li $v0, 4
29: la $a0, msg2
30: syscall
31: li $v0, 8
32: la $a0, msg
33: li $a1, 40
34: syscall
35:
36: li $v0, 4
37: la $a0, msg
38: syscall
39:
40: li $v0, 10 #system call code for exit = 10
41: syscall #call operating system to exit
1: # author : lijian
2: # date: 2012-01-04 22:09:55
3: # fuction: use mips to count
4:
5: .data
6: newline: .asciiz "\n"
7: line: .asciiz "****************************\n"
8: head: .asciiz " count numbers \n"
9: author: .asciiz " author : lijian \n"
10: version: .asciiz " version: 0.1 \n"
11: first: .asciiz "input the first num: "
12: second: .asciiz "input the second num: "
13: addString: .asciiz " + "
14: subString: .asciiz " - "
15: mulString: .asciiz " * "
16: divString: .asciiz " / "
17: equalString: .asciiz " = "
18:
19: .text
20: main:
21: # show menu
22: li $v0, 4
23: la $a0, line
24: syscall
25: li $v0, 4
26: la $a0, head
27: syscall
28: li $v0, 4
29: la $a0, newline
30: syscall
31: li $v0, 4
32: la $a0, author
33: syscall
34: li $v0, 4
35: la $a0, version
36: syscall
37: li $v0, 4
38: la $a0, line
39: # input first num
40: syscall
41: li $v0, 4
42: la $a0, first
43: syscall
44: li $v0, 5
45: syscall
46: move $s0, $v0
47: # input second num
48: li $v0, 4
49: la $a0, second
50: syscall
51: li $v0, 5
52: syscall
53: move $s1, $v0
54: # show add result
55: li $v0, 1
56: move $a0, $s0
57: syscall
58: li $v0, 4
59: la $a0, addString
60: syscall
61: li $v0, 1
62: move $a0, $s1
63: syscall
64: li $v0, 4
65: la $a0, equalString
66: syscall
67:
68: add $a0, $s0, $s1
69: li $v0, 1
70: syscall
71: li $v0, 4
72: la $a0, newline
73: syscall
74:
75: # show sub result
76: li $v0, 1
77: move $a0, $s0
78: syscall
79: li $v0, 4
80: la $a0, subString
81: syscall
82: li $v0, 1
83: move $a0, $s1
84: syscall
85: li $v0, 4
86: la $a0, equalString
87: syscall
88:
89: sub $a0, $s0, $s1
90: li $v0, 1
91: syscall
92: li $v0, 4
93: la $a0, newline
94: syscall
95:
96: # show mul result
97: li $v0, 1
98: move $a0, $s0
99: syscall
100: li $v0, 4
101: la $a0, mulString
102: syscall
103: li $v0, 1
104: move $a0, $s1
105: syscall
106: li $v0, 4
107: la $a0, equalString
108: syscall
109:
110: mul $a0, $s0, $s1
111: li $v0, 1
112: syscall
113: li $v0, 4
114: la $a0, newline
115: syscall
116:
117: # show div result
118: li $v0, 1
119: move $a0, $s0
120: syscall
121: li $v0, 4
122: la $a0, divString
123: syscall
124: li $v0, 1
125: move $a0, $s1
126: syscall
127: li $v0, 4
128: la $a0, equalString
129: syscall
130:
131: div $a0, $s0, $s1
132: li $v0, 1
133: syscall
134: li $v0, 4
135: la $a0, newline
136: syscall
137:
138: li $v0, 10 #system call code for exit = 10
139: syscall #call operating system to exit
1: # author : lijian
2: # data: 2012-01-04 22:38:01
3: # function: find the max num and count the total
4:
5: .data
6: array: .space 12 #allocate 12 consecutive bytes
7: msg1: .asciiz "input the 3 integers(each num end of [enter]): \n"
8: msg2: .asciiz "the max num is: "
9: msg3: .asciiz "the tatal is: "
10: newline: .asciiz "\n"
11:
12: .text
13: main:
14:
15: la $t0, array
16: li $v0, 4
17: la $a0, msg1
18: syscall
19: li $v0, 5
20: syscall
21: sw $v0,($t0) # store the first num
22: li $v0, 5
23: syscall
24: sw $v0,4($t0) # stroe the second num
25: li $v0, 5
26: syscall
27: sw $v0,8($t0) # store the third num
28:
29: lw $s0, ($t0) # get the first num
30: lw $s1, 4($t0) # get the second num
31: lw $s2, 8($t0) # get the third num
32:
33: add $s3, $s0, $s1
34: add $s4, $s2, $s3 # total stored in $s4
35: li $v0, 4
36: la $a0, msg3
37: syscall
38: li $v0, 1
39: move $a0, $s4
40: syscall
41: li $v0, 4
42: la $a0, newline
43: syscall
44:
45: blt $s0, $s1,num2
46: move $s3, $s0
47: j num3
48: num2:
49: move $s3, $s1
50: num3:
51: bge $s3, $s2, num4
52: move $s3, $s2
53: num4:
54: li $v0, 4
55: la $a0, msg2
56: syscall
57: li $v0, 1
58: move $a0, $s3
59: syscall
60: li $v0, 4
61: la $a0, newline
62: syscall
63:
64: li $v0, 10 #system call code for exit = 10
65: syscall #call operating system to exit