mips的系统调用

System Calls and I/O (SPIM Simulator)

  系统调用 与 输入/输出(主要针对SPIM模拟器)

 (本人使用的是Mars 4.4,也通用--!)

 

  • 通过系统调用实现终端的输入输出,以及声明程序结束
  • 学会使用 syscall
  • 参数所使用的寄存器:$v0, $a0,  $a1
  • 返回值使用: $v0

下表给出了系统调用中对应功能,代码,参数机返回值

Service

Code
in $v0

对应功能的调用码

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 = length of string buffer (n)

将读取的字符串长度赋值给 $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.
    • 你懂得。。。
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
  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中文名: MIPS汇编语言程序设计 原名: MIPS Assembly Language Programming 作者: Robert Britton 资源格式: PDF 出版社: Prentice Hall书号: 9780131420441发行时间: 2003年06月07日 地区: 美国 语言: 英文 简介: 这本书的用户将获得一个当代计算机体系结构的基本概念的理解,具有精简指令集计算机(RISC)的开始。一个计算机建筑的理解需要开始组织与现代计算机的基本知识。 MIPS架构体现了当代所有的RISC架构的基本设计原则。这本书提供了如何现代电脑的功能组件放在一起,以及如何在一台计算机的机器语言级作品的理解。 精心编写的,显然是有组织,这本书涵盖了MIPS架构的基础知识,包括算法开发,数字系统,函数调用,可重入函数,内存映射I / O,异常和中断,和浮点指令。 对于在系统系统开发,系统的分析和系统维护领域的员工。 Users of this book will gain an understanding of the fundamental concepts of contemporary computer architecture, starting with a Reduced Instruction Set Computer (RISC). An understanding of computer architecture needs to begin with the basics of modern computer organization. The MIPS architecture embodies the fundamental design principles of all contemporary RISC architectures. This book provides an understanding of how the functional components of modern computers are put together and how a computer works at the machine-language level. Well-written and clearly organized, this book covers the basics of MIPS architecture, including algorithm development, number systems, function calls, reentrant functions, memory-mapped I/O, exceptions and interrupts, and floating-point instructions. For employees in the field of systems, systems development, systems analysis, and systems maintenance. 目录: CHAPTER 1: The MIPS Architecture CHAPTER 2: Pseudocode CHAPTER 3: Number Systems CHAPTER 4: PCSpim The MIPS Simulator CHAPTER 5: Algorithm Development CHAPTER 6: Function Calls Using the Stack CHAPTER 7: Reentrant Functions CHAPTER 8: Exception Processing CHAPTER 9: A Pipelined Implementation CHAPTER 10: Embedded Processors APPENDIX A: Quick Reference APPENDIX B: ASCII Codes APPENDIX C: Integer Instruction Set APPENDIX D: Macro Instructions APPENDIX E: A Trap Handler
英国科学家 Dominic Sweetman(经典著作 See MIPS Run 的作者),称 MIPS 为“高效的 RISC 体系结构中最优雅的一种体系结构”。 它是一个双关语: 即是 Microcomputer without Interlocked Pipeline Stages 的缩写,同时又是 Millions of Instructions Per Second 的缩 写。 MIPS 的处理单元是一个五级流水线:Instruction Fetch, Register & Decoder, ALU, Memory 以及 Write back。一开始的 MIPS 是为 32 位系统设计的,实际上,后续的 64 位扩 展,也依然对 32 位的工作模式向下兼容着。一如其他的 RISC 处理器,MIPS 的每条指令长度 是固定的 32bit。(因此,最长的局部跳转指令只能跳转 2 的 26 次方 Byte,也就是 2 的 24 次 方,16777216 条指令) MIPS 有 32 个通用寄存器,编程者可以使用其中除$0 外的所有寄存器暂存数据。$0 寄存 器,在硬件上被设计为永远读出 0。我们可以用$0 寄存器的此特性,实现一些技巧性的编程, 譬如实现 NOP 操作。MIPS 本没有 NOP 指令,但由于对$0 寄存器的写入实际上无意义,可以 作为空操作使用。 事实上,编译器从高级语言(典型如 C/C++)转换为 MIPS 汇编指令时,一般总是遵守一 定的寄存器使用约定。某些寄存器用来从函数中传入和传出参数,存储临时数据,另一些则起特 殊作用,如保存调用函数时的指令地址,或作为堆栈指针等。如果你使用汇编开发,理论上可以 无视这点约定,但是,一般地,遵守这个约定,与人方便,也为己方便。关于寄存器使用的约定, 以后会展开论述。 一如所有的 RISC 处理器,MIPS 没有 CISC 那样复杂多变的寻址方式,统一为 Load/Store 寻址。任何载入和存储操作,都可写为如下形式: lw $1, offset ($2) 这条指令的操作符可以为 Load 或 Store,一次 Load/Store 操作的范围可以为字/半字/ 字节(对应 gcc 的 int, short 和 char)。偏移量是一个带符号的 16bit 整数。两个作为操作数 的寄存器可以是任何通用寄存器。(你可以向$0 写入,但无任何意义,等同于空操作)。对于 64 位模式下,也可以对 double 类型进行操作。注意 Load/Store 都必须对应一个对齐的地址,否 则会引发一个异常(Exception)。 MIPS 支持最多 4 个协处理器。协处理器 CP0 为 CPU 的控制协处理器,是体系结构中必须 实现的。CP1 为浮点处理器。CP2 保留,各生产厂商往往用来实现一些自己的特色功能,例如 RMI 的 Fast Message Ring 等。CP3 原本也是保留共扩展用,但很多 MIPS III 和 MIPS IV 时代的扩展指令集使用了它。对于 CP0,我们会有专门的一段用来讨论。 为适应处理器向多核时代的演进, MIPS 引入了多核操作必要的原子指令 (Atomic operation)、内存屏障(Barrier)等操作。在 SMP 或 AMP 等多核架构中,这些指令是并行计算 同步的保障。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值