知识点

寄存器地址:

伪指令

  • **.data:**用于预先存储数据的伪指令的开始标志。
  • **.text:**程序代码指令开始的标志。
  • **.word:**以字为单位存储数据。
  • **.asciiz:**以字节为单位存储字符串。
  • **.space:**申请若干个字节的未初始化的内存空间。

V0 的不同值

常用:

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 option 0: Yes 1: No 2: Cancel
InputDialogInt 51 $a0 = address of null-terminated string that is the message to user $a0 contains int read $a1 contains status value 0: 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 value 0: 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 value 0: 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 value 0: 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 icon 1: information message, indicated by Information icon 2: warning message, indicated by Warning icon 3: question message, indicated by Question icon other: 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

注释:服务编号为30及以上的服务不包含在SPIM服务8中。它遵循UNIX 'fgets’的语义。对于指定长度n,字符串的长度不能超过n-1。如果长度小于n-1,则在末尾添加换行符。在任何情况下,都会用空字符填充。如果n等于1,则忽略输入并将空字符放置在缓冲区地址上。如果n大于1,则忽略输入并且不会将任何内容写入缓冲区。

服务11 - 打印低字节所对应的ASCII字符。

服务13 - MARS实现了三个标志值:0表示只读,1表示可写且可创建,9表示可写且可创建和追加。它忽略模式。如果操作失败,则返回的文件描述符将为负数。底层文件I/O实现使用java.io.FileInputStream.read()读取和java.io.FileOutputStream.write()写入。MARS内部维护文件描述符,并从3开始分配它们。文件描述符0、1、2始终用于:从标准输入读取、向标准输出写入和向标准错误写入(自版本4.3起新增)。

服务13、14、15 - 在MARS 3.7中,结果寄存器已更改为与SPIM兼容。之前在《计算机组织与设计》的附录B中错误地印成了$a0,现在是$a1。

服务17 - 如果MIPS程序在MARS图形界面(GUI)的控制下运行,则忽略$a1中的退出代码。服务30 - 系统时间来自java.util.Date.getTime(),表示自1970年1月1日以来的毫秒数。

服务31、33 - 通过声卡模拟MIDI输出。详情见下文。

服务40至44使用Java底层的伪随机数生成器,由java.util.Random类提供。每个流(由$a0的内容标识)都由不同的Random对象模拟。没有默认的种子值,因此如果需要重复的随机序列,请使用Set Seed服务(40)。

常用指令

str: .asciiz “The numbers are:\n”

# 这里使用了宏,%i 为存储当前行数的寄存器,%j 为存储当前列数的寄存器 # 把 (%i * 8 + %j) * 4 存入 %ans 寄存器中

.macro getindex(%ans, %i, %j)
sll %ans, %i, 3 #%ans = %i * 8
add %ans, %ans, %j #%ans = %ans + %j
sll %ans, %ans, 2 # %ans = %ans * 4 .end_macro

在汇编程序中,还有一种和C语言中 #define 类似的宏定义,一般用于常量的定义上,那就是 .eqv.eqv 用法如下:

.eqv EQV_NAME string

li $t0, 0 # $t0=0

la $a0, str 地址

beq $t0, $s0, loop_in_end # $t0 == $s0 的时候跳出循环

bne 不相等时

array: .space 40 # 存储这些数需要用到数组,数组需要使用 10 * 4 = 40 字节 # 一个 int 整数需要占用 4 个字节,需要存储 10 个 int 整数 # 因此,array[0] 的地址为 0x00,array[1] 的地址为 0x04 # array[2] 的地址为 0x08,以此类推。

sll $t1, $t0, 2 # $t1 = $t0 << 2,即 $t1 = $t0 * 4

sw $v0, array($t1) # 把输入的数v0存入地址为 array + $t1 的内存中

lw $a0, array($t1) # 把内存中地址为 array + $t1 的数取出到 $a0 中

addi $t0, $t0, 1 # $t0 = $t0 + 1

add $t0, $t0, $a0 # $t0 = $t0 + $a0

jal f #跳转到函数f,并且将$ra变为下一句地址

jr $ra 跳转到寄存器$ra