table of Contents
MIPS
Register type
Arithmetic and addressing instructions
Arithmetic Instructions
! ! ! Arithmetic operation instructions cannot be used for addresses! ! !
Simple addition and subtraction are as follows:
For multiplication and division, there are special registers: Lo and Hi, which correspond to the low and high bits of the 64-bit storage after multiplication and division, respectively.
Load / Store
Only these two instructions can operate on the address.
example:
lw register_destination, RAM_source ;load a word
lb register_destination, RAM_source ;load a byte
sw register_source, RAM_destination
sb register_source, RAM_destination
load immediate:
li register_dedstination, 5
Indirect and Based Addressing
load address:
la $t0,val ;$t0 = address of val ----($t0)=val
lw $t0,4($t1) ; $t0 = address of ($t1+4)
Sample program:
.data
array1: .space 12
# declare 12 bytes of storage to hold array of 3 integers
# 定义一个 12字节 长度的数组 array1, 容纳 3个整型
.text
__start: la $t0, array1
# load base address of array into register $t0
# 让 $t0 = 数组首地址
li $t1, 5 # $t1 = 5 ("load immediate")
sw $t1, ($t0)
# first array element set to 5; indirect addressing
# 对于 数组第一个元素赋值 array[0] = $1 = 5
li $t1, 13 # $t1 = 13
sw $t1, 4($t0) # second array element set to 13# 对于 数组第二个元素赋值 array[1] = $1 = 13 # (该数组中每个元素地址相距长度就是自身数据类型长度,即4字节, 所以对于array+4就是array[1])
li $t1, -7 # $t1 = -7
sw $t1, 8($t0) # third array element set to -7
# 同上, array+8 = (address[array[0])+4)+ 4 = address(array[1]) + 4 = address(array[2])
done
Program structure
The basic template is as follows:
# Comment giving name of program and description of function
# 说明下程序的目的和作用(其实和高级语言都差不多了)
# Template.s
#Bare-bones outline of MIPS assembly language program
.data # variable declarations follow this line
# 数据变量声明
# ...
.text # instructions follow this line
# 代码段部分
main: # indicates start of code (first instruction to execute)
# 主程序
# ...
# End of program, leave a blank line afterwards to make SPIM happy
# 必须多给你一行,你才欢?
Data declaration
format for declarations
name storage_type value(s)
-->example: val .word 3
lables:
.word
.byte
.space 开辟数组空间
.asciiz 用于字符串
System call
Control Structures
Branch
comparison for conditional branches is built into instruction
b target # unconditional branch to program label target
beq $t0,$t1,target # branch to target if $t0 = $t1
bne $t0,$t1,target # branch to target if $t0 != $t1
slt $t0,$t1,target # branch to target if $t0 < $t1
还有其它的可以自己翻阅上传的文件。
Jump
j target
# unconditional jump to program label target 看到就跳, 不用考虑任何条件
jr $t3
# jump to address contained in $t3 ("jump register") 类似相对寻址,跳到该寄存器给出的地址处
Subroutine Calls
jal sub_label ;跳转到指定位置,且保存之前的pc于$ra寄存器
jr $ra ;用$ra寄存器恢复之前的pc值
This only applies to one jump. After all, there is only one $ra. If you want to recurse, you still need to use a stack frame.
MIPS instruction set experiment
MinMIPS64 emulator
The big guy about the platform made it very clear: MinMips64 simulator
manual
After writing the code in Notepad, etc., save it as a .s file, use the command line to execute the asm program, and use the code txt as the parameter.
Will prompt no errors and other information, you can run in winmips64.
Open winmips64 and import the txt file:
and the excute->run to program will be executed directly (unless there is a pause such as input).
Then this program has output. If you want to see the output cmd window, you have to select terminal in the windows above, and finally it displays:
IO memory mapping
After DATA is set, the word of CONTROL can be controlled to perform IO operation.
hello world demo
.data
str: .asciiz "Hello World"
CONTROL: .word32 0x10000
DATA: .word32 0x10008
.text
main:
lwu r31,CONTROL(r0) ;$r31 = address of CONTROL register
lwu r30,DATA(r0) ;$r30 = address of DATA register
daddi r29,r0,4 ;set control(4):output string
daddi r28,r0,str ;get address of str and store in r28
sd r28,(r30) ;write address of message to DATA
sd r29,(r31) ;output
halt
bubble sort
Code structure
main
first_show
sort
last_show
sort
loopi
loopj
looj
if array[j] > array[j+1] swap
swap
sd -> array[j]
sd -> array[j+1]
The complete code will be released later.