Mips指令集学习

Mips学习

一些废话:

  • MIPS技术公司则是一家设计制造高性能、高档次及嵌入式32位和64位处理器的厂商。
  • 在通用方面,MIPS R系列微处理器用于构建SGI的高性能工作站、服务器和超级计算机系统。
  • 在嵌入式方面,MIPS K系列微处理器是仅次于ARM的用得最多的处理器之一(1999年以前MIPS是世界上用得最多的处理器)
  • 其应用领域覆盖游戏机、路由器、激光打印机、掌上电脑等各个方面。

Start:

学习环境: mips 工具:mars

mips特点: 所有指令都是32位编码

语法规则: 不同于at&t和intel little一致

[操作码 操作数1 , 操作数2, 操作数3.....]


常用指令集:

li 将一个值存放至目标寄存器例如: li $a0,123(将123放入a0中)
move 将一个另一个寄存器中的值存放到目标寄存器例如: move $a0,a1(把a1的数据放到了a0中)
la 将一个地址存放至目标寄存器例如: la $a0,msg(msg为地址,存放在某个段内)
add 将2个数的相加结果放在目标寄存器中例如: add $a0 ,$a1 ,3 (将a1+3放入a0中)
sub 两个数相减,用法同add
bgt 比较如果第一个操作数大于第二个操作数(gt是大于的意思),将跳转到第三个操作数上.例如: bgt $a0,$a1,sub(sub为一个地址,a1可以为常量)
ble 比较如果第一个操作数小于第二个操作数(le是小于的意思),将跳转到第三个操作数上.例如: ble $a0,$a1,sub(sub为一个地址,a1可以为常量)
beq 比较两个操作数相等跳转
j 无条件跳转

寄存器:

寄存器名字 寄存器编号 寄存器功能
$zero $0 恒等于零
$at $1 被汇编器保留,用于处理大的常数
$v0 – $v1 $2-$3 存放函数返回值
$a0 – $a3 $4-$7 传递函数参数
$t0 – $t7 $8-$15 存放临时变量
$s0 – $s7 $16-$23 存放需要保存的临时值
$t8 – $t9 $24-$25 额外的存放临时变量
$k0 – $k1 $26-$27 用于操作系统内核
$gp $28 指向全局变量的指针
$sp $29 指向栈顶的指针
$fp $30 指向栈帧[^footnote2]的指针
$ra $31 返回地址,用于函数调用

区段名解释:

.text 代码段
.data 数据段
.ascii ascii码
.space 开辟一段内存空间
.byte 字节
.word 字=2*byte
.dword 双字= 4*byte
.qword 8*byte
.double 双精度浮点
.float 单精度浮点
.globl 全局data,存放在.bss区段中

系统调用表:

Service Code in $v0 Arguments Result
print integer 1 $a0 = integer to print
print float 2 $f12 = float to print
print double 3 $f12 = double to print
print string 4 $a0 = address of null-terminated string to print
read integer 5 $v0 contains integer read
read float 6 $f0 contains float read
read double 7 $f0 contains double read
read string 8 $a0 = address of input buffer$a1 = maximum number of characters to read See note below table
sbrk (allocate heap memory) 9 $a0 = number of bytes to allocate $v0 contains address of allocated memory
exit (terminate execution) 10
print character 11 $a0 = character to print See note below table
read character 12 $v0 contains character read
open file 13 $a0 = address of null-terminated string containing filename$a1 = flags$a2 = mode $v0 contains file descriptor (negative if error). See note below table
read from file 14 $a0 = file descriptor$a1 = address of input buffer$a2 = maximum number of characters to read $v0 contains number of characters read (0 if end-of-file, negative if error). See note below table
write to file 15 $a0 = file descriptor$a1 = address of output buffer$a2 = number of characters to write $v0 contains number of characters written (negative if error). See note below table
close file 16 $a0 = file descriptor
exit2 (terminate with value) 17 $a0 = termination result See note below table
Services 1 through 17 are compatible with the SPIM simulator, other than Open File (13) as described in the Notes below the table. Services 30 and higher are exclusive to MARS.
time (system time) 30 $a0 = low order 32 bits of system time$a1 = high order 32 bits of system time. See note below table
MIDI out 31 $a0 = pitch (0-127)$a1 = duration in milliseconds$a2 = instrument (0-127)$a3 = volume (0-127) Generate tone and return immediately. See note below table
sleep 32 $a0 = the length of time to sleep in milliseconds. Causes the MARS Java thread to sleep for (at least) the specified number of milliseconds. This timing will not be precise, as the Java implementation will add some overhead.
MIDI out synchronous 33 $a0 = pitch (0-127)$a1 = duration in milliseconds$a2 = instrument (0-127)$a3 = volume (0-127) Generate tone and return upon tone completion. See note below table
print integer in hexadecimal 34 $a0 = integer to print Displayed value is 8 hexadecimal digits, left-padding with zeroes if necessary.
print integer in binary 35 $a0 = integer to print Displayed value is 32 bits, left-padding with zeroes if necessary.
print integer as unsigned 36 $a0 = integer to print Displayed as unsigned decimal value.
(not used) 37-39
set seed 40 $a0 = i.d. of pseudorandom number generator (any int).$a1 = seed for corresponding pseudorandom number generator. No values are returned. Sets the seed of the corresponding underlying Java pseudorandom number generator (java.util.Random). See note below table
random int 41 $a0 = i.d. of pseudorandom number generator (any int). $a0 contains the next pseudorandom, uniformly distributed int value from this random number generator's sequence. See note below table
random int range 42 $a0 = i.d. of pseudorandom number generator (any int).$a1 = upper bound of range of returned values. $a0 contains pseudorandom, uniformly distributed int value in the range 0 = [int] [upper bound], drawn from this random number generator's sequence. See note below table
random float 43 $a0 = i.d. of pseudorandom number generator (any int). $f0 contains the next pseudorandom, uniformly distributed float value in the range 0.0 = f 1.0 from this random number generator's sequence. See note below table
random double 44 $a0 = i.d. of pseudorandom number generator (any int). $f0 contains the next pseudorandom, uniformly distributed double value in the range 0.0 = f 1.0 from this random number generator's sequence. See note below table
(not used) 45-49
ConfirmDialog 50 $a0 = address of null-terminated string that is the message to user $a0 contains value of user-chosen option0: Yes1: No2: Cancel
InputDialogInt 51 $a0 = address of null-terminated string that is the message to user $a0 contains int read$a1 contains status value0: OK status-1: input data cannot be correctly parsed-2: Cancel was chosen-3: OK was chosen but no data had been input into field
InputDialogFloat 52 $a0 = address of null-terminated string that is the message to user $f0 contains float read$a1 contains status value0: OK status-1: input data cannot be correctly parsed-2: Cancel was chosen-3: OK was chosen but no data had been input into field
InputDialogDouble 53 $a0 = address of null-terminated string that is the message to user $f0 contains double read$a1 contains status value0: OK status-1: input data cannot be correctly parsed-2: Cancel was chosen-3: OK was chosen but no data had been input into field
InputDialogString 54 $a0 = address of null-terminated string that is the message to user$a1 = address of input buffer$a2 = maximum number of characters to read See Service 8 note below table$a1 contains status value0: OK status. Buffer contains the input string.-2: Cancel was chosen. No change to buffer.-3: OK was chosen but no data had been input into field. No change to buffer.-4: length of the input string exceeded the specified maximum. Buffer contains the maximum allowable input string plus a terminating null.
MessageDialog 55 $a0 = address of null-terminated string that is the message to user$a1 = the type of message to be displayed:0: error message, indicated by Error icon1: information message, indicated by Information icon2: warning message, indicated by Warning icon3: question message, indicated by Question iconother: plain message (no icon displayed) N/A
MessageDialogInt 56 $a0 = address of null-terminated string that is an information-type message to user$a1 = int value to display in string form after the first string N/A
MessageDialogFloat 57 $a0 = address of null-terminated string that is an information-type message to user$f12 = float value to display in string form after the first string N/A
MessageDialogDouble 58 $a0 = address of null-terminated string that is an information-type message to user$f12 = double value to display in string form after the first string N/A
MessageDialogString 59 $a0 = address of null-terminated string that is an information-type message to user$a1 = address of null-terminated string to display after the first string N/A

练习: 《guess_number》


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
.data:
    wel: .ascii "Welcome!\nYou have 5 chance\n  Now ,try guess the Number\0"  #\0 string_ends
    msg1: .ascii "Plz Input Number:\0"
    success: .ascii "Success.\n\0"
    big: .ascii "Big!\n\0"
    small: .ascii "Small!\n\0"
    err: .ascii "no chance..\n\0"

.text:
    li $t3, 1   #loop_value
    la $a0, wel #welcome
    li $v0, 4
    syscall
    loop:
        la $a0, msg1    #puts(msg1 -> Plz Input Num:)
        li $v0, 4
        syscall

        li $a0, 0   #gets(t0)
        li $v0, 5
        syscall
        move $t0, $v0

        li $s0, 233 #judge -> success_value = 233
        beq $t0, $s0, win
        bgt $t0, $s0, gt
        ble $t0, $s0, le

    gt: #big
        la $a0, big
        li $v0, 4
        syscall
        add $t3, $t3, 1     #add loop_value
        ble $t3, 5, loop
        j lose

    le: #small
        la $a0, small
        li $v0, 4
        syscall
        add $t3, $t3, 1     #add loop_value
        ble $t3, 5, loop
        j lose

    win:    #success
        la $a0, success
        li $v0, 4
        syscall
        li $v0, 10      #sys_exit()
        syscall

    lose:   #lose
        la $a0, err
        li $v0, 4
        syscall
        li $v0, 10      #sys_exit()
        syscall

发表评论

电子邮件地址不会被公开。 必填项已用*标注