System Calls and I/O (SPIM Simulator)
系统调用 与 输入/输出(主要针对SPIM模拟器)
(本人使用的是Mars 4.4,也通用--!)
- 通过系统调用实现终端的输入输出,以及声明程序结束
- 学会使用 syscall
- 参数所使用的寄存器:$v0, $a0, $a1
- 返回值使用: $v0
下表给出了系统调用中对应功能,代码,参数机返回值
Service | Code 对应功能的调用码 | Arguments 所需参数 | Results 返回值 |
---|---|---|---|
print_int 打印一个整型 |
$v0 = 1
| $a0 = integer to be printed 将要打印的整型赋值给 $a0 | |
print_float 打印一个浮点 |
$v0 = 2
| $f12 = float to be printed 将要打印的浮点赋值给 $f12 | |
print_double 打印双精度 |
$v0 = 3
| $f12 = double to be printed 将要打印的双精度赋值给 $f12 | |
print_string |
$v0 = 4
| $a0 = address of string in memory 将要打印的字符串的地址赋值给 $a0 | |
read_int |
$v0 = 5
| integer returned in $v0 将读取的整型赋值给 $v0 | |
read_float 读取浮点 |
$v0 = 6
| float returned in $v0 将读取的浮点赋值给 $v0 | |
read_double 读取双精度 |
$v0 = 7
| double returned in $v0 将读取的双精度赋值给 $v0 | |
read_string 读取字符串 |
$v0 = 8
| $a0 = memory address of string input buffer 将读取的字符串地址赋值给 $a0 将读取的字符串长度赋值给 $a1 | |
sbrk 应该同C中的sbrk()函数 动态分配内存 |
$v0 = 9
| $a0 = amount 需要分配的空间大小(单位目测是字节 bytes) | address in $v0 将分配好的空间首地址给 $v0 |
exit 退出 |
$v0 =10
| 你懂得![]() |
- 大概意思是要打印的字符串应该有一个终止符,估计类似C中的'\0', 在这里我们只要声明字符串为 .asciiz 类型即可。下面给个我用Mars4.4的提示:
- .ascii 与 .asciiz唯一区别就是 后者会在字符串最后自动加上一个终止符, 仅此而已
- The read_int, read_float and read_double services read an entire line of input up to and including the newline character.
- 对于读取整型, 浮点型,双精度的数据操作, 系统会读取一整行,(也就是说以换行符为标志 '\n')
- The read_string service has the same semantices as the UNIX library routine fgets.
- It reads up to n-1 characters into a buffer and terminates the string with a null character.
- If fewer than n-1 characters are in the current line, it reads up to and including the newline and terminates the string with a null character.
- 这个不多说了,反正就是输入过长就截取,过短就这样,最后都要加一个终止符。
- The sbrk service returns the address to a block of memory containing n additional bytes. This would be used for dynamic memory allocation.
- 上边的表里已经说得很清楚了。
- The exit service stops a program from running.
- 你懂得。。。
- 大概意思是要打印的字符串应该有一个终止符,估计类似C中的'\0', 在这里我们只要声明字符串为 .asciiz 类型即可。下面给个我用Mars4.4的提示:
e.g. Print out integer value contained in register $t2 栗子: 打印一个存储在寄存器 $2 里的整型 li $v0, 1 # load appropriate system call code into register $v0; 声明需要调用的操作代码为 1 (print_int) 并赋值给 $v0 # code for printing integer is 1 move $a0, $t2 # move integer to be printed into $a0: $a0 = $t2 将要打印的整型赋值给 $a0 syscall # call operating system to perform operation e.g. Read integer value, store in RAM location with label int_value (presumably declared in data section) 栗子: 读取一个数,并且存储到内存中的 int_value 变量中 li $v0, 5 # load appropriate system call code into register $v0; # code for reading integer is 5 声明需要调用的操作代码为 5 (read_int) 并赋值给 $v0 syscall # call operating system to perform operation、 经过读取操作后, $v0 的值已经变成了 输入的 5 sw $v0, int_value # value read from keyboard returned in register $v0; # store this in desired location 通过写入(store_word)指令 将 $v0的值(5) 存入 内存中 e.g. Print out string (useful for prompts) 栗子: 打印一个字符串(这是完整的,其实上面栗子都可以直接替换main: 部分,都能直接运行) .data string1 .asciiz "Print this.\n" # declaration for string variable, # .asciiz directive makes string null terminated .text main: li $v0, 4 # load appropriate system call code into register $v0; # code for printing string is 4 打印字符串, 赋值对应的操作代码 $v0 = 4 la $a0, string1 # load address of string to be printed into $a0 将要打印的字符串地址赋值 $a0 = address(string1) syscall # call operating system to perform print operation e.g. To indicate end of program, use exit system call; thus last lines of program should be: 执行到这里, 程序结束, 立马走人, 管他后边洪水滔天~~ li $v0, 10 # system call code for exit = 10 syscall # call operating sys
转载地址:http://www.cnblogs.com/thoupin/p/4018455.html?utm_source=tuicool