ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:49.55KB ,
资源ID:7161544      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7161544.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(MIPS 体系结构和汇编语言快速入门.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

MIPS 体系结构和汇编语言快速入门.docx

1、MIPS 体系结构和汇编语言快速入门MIPS 体系结构和汇编语言快速入门译者:Sonic Fu, Northeastern University, Boston, MA, USA译者按:有修改,无删减,初学必读。学习笔记,抛砖引玉!网上有一个老版本,不如此版全面。英文原版: http:/logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm#IOSystemCalls本文分3部分: 1、寄存器 2、程序结构框架 3、编写汇编程序概要:数据类型和文法 数据类型: 字节,byte占用( 8bit ), halfword占2 byte= 16bi

2、t), word占用(4byte = 32bit) 一个字符需要一个Byte的空间; 一个整数需要1个Word(4 Byte)的空间; MIPS结构的每条指令长度都是32bit寄存器 MIPS体系架构有32个通用寄存器。在汇编程序中,可以用编号 $0 到 $31来表示; 也可以用寄存器的名字来进行表示, 例如: $sp, $t1, $ra. 有两个特殊的寄存器 Lo, Hi, 用来保存乘法/除法的运算结果;此2寄存器不能直接寻址,只能用特殊的指令:mfhi和mflo来aceess其中的内容。(含义:mfhi = move from Hi, mflo = Move from Low.) 堆栈(S

3、tack)的增长方向是: 从内存的高地址方向, 向低地址方向;表格1:寄存器的编号名称及分类编号寄存器名称寄存器描述0Zero第0号寄存器,其值始终为01$at(Assembler Temporary) 是Assembler保留的寄存器2 3$v0 $v1(values)保存表达式或函数返回的结果4-7$a0 - $a3(arguments) 作为函数的前四个入参。在子函数调用的过程中不会被保留。8-15$t0 - $t7(temporaries) Caller saved if needed. Subroutines can use without saving.供汇编程序使用的临时寄存器。

4、在子函数调用的过程中不会被保留。16-23$s0 - $s7(saved values) - Callee saved.A subroutine using one of these must save original and restore it before exiting. 在子函数调用的过程中会被保留。24-25$t8 - $t9(temporaries) Caller saved if needed. Subroutines can use without saving.供汇编程序使用的临时寄存器。在子函数调用的过程中不会被保留。这是对 $t0 - $t7的补充。26-27$k0

5、- $k1保留,仅供中断(interrupt/trap)处理函数使用.28$gpglobalpointer.全局指针。Points to the middle of the 64K block of memory in the static data segment.指向固态数据块内存的64K的块的中间。29$spstackpointer堆栈指针, 指向堆栈的栈顶。30$s8/$fpsaved value /framepointer保存的值/帧指针其中的值在函数调用的过程中会被保留31$rareturnaddress返回地址汇编程序结构框架汇编源程序代码本质上是文本文件。由 数据声明、代码段

6、两部分组成。汇编程序文件应该以.s为后缀,以在Spim软件中进行模拟。(实际上ASM也行。) 数据声明部分在源代码中,数据声明部分以 .data开始。声明了在代码中使用的变量的名字。同时,也在主存(RAM)中创建了对应的空间。 程序代码部分在源代码中,程序代码部分以 .text开始。这部分包含了由指令构成的程序功能代码。代码以main: 函数开始。main的结束点应该调用exit system call,参见后文有关system call 的介绍。程序的注释部分使用#符号进行注释。每行以#引导的部分都被视作注释。一个MIPS汇编程序框架:# Comment giving name of pro

7、gram 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

8、to make SPIM happy编写MIPS汇编程序:Content:PartI: 数据的声明Part II: 数据的装载和保存(Load/Store 指令)Part III: 寻址Part IV: 算术运算指令:Arithmetic InstructionsPart V 程序控制指令:Control InstructionsPart VI: 系统调用和I/O操作(SPIM仿真)PartI:数据的声明 格式:name: storage_type value(s) 创建一个以name为变量名称,values通常为初始值,storage_type代表存储类型。 注意:变量名后要跟一个:冒号ex

9、ample var1: .word 3 # create a single integer: #variable with initial value 3array1: .byte a,b # create a 2-element character # array with elements initialized: # to a and barray2: .space 40 # allocate 40 consecutive bytes, # with storage uninitialized # could be used as a 40-element # character arr

10、ay, or a # 10-element integer array; # a comment should indicate it.string1 .asciiz Print this.n #declare a stringPart II:数据的装载和保存(Load/Store 指令) 主存(RAM)的存取access只能用load / store 指令来完成。 所有其他的指令都使用的是寄存器作为操作数。i. load指令:lw register_destination, RAM_source # copy word (4 bytes) at # source_RAM location #

11、 to destination register. # load word - lwlb register_destination, RAM_source # copy byte at source RAM # location to low-order byte of # destination register, # and sign -e.g. tend to # higher-order bytes # load byte - lb li register_destination, value #load immediate value into #destination regist

12、er #load immediate - li ii. store指令sw register_source, RAM_destination #store word in source register # into RAM destinationsb register_source, RAM_destination #store byte (low-order) in #source register into RAM #destination举个例子:.datavar1: .word 23 # declare storage for var1; #initial value is 23.t

13、ext_start:lw $t0, var1 # load contents of RAM location # into register $t0: # $t0 = var1li $t1, 5 # $t1 = 5 (load immediate)sw $t1, var1 # store contents of register $t1 # into RAM: var1 = $t1 donedonePart III:寻址 :MIPS系统结构只能用load/store 相关指令来实现寻址操作,包含3中寻址方式: 装载地址:load address,相当于直接寻址,把数据地址直接载入寄存器。 间接

14、寻址:indirect addressing,间接寻址,把寄存器内容作为地址基线寻址/索引寻址:based or indexed addressing,相对寻址,利用补偿值(offset)寻址。 直接寻址/装载地址:load address:la $t0, var1把var1在主存(RAM)中的地址拷贝到寄存器t0中。var1也可以是程序中定义的一个子程序标签的地址。 间接寻址:indirect addressing:lw $t2, ($t0) 主存中有一个字的地址存在t0中,按这个地址找到那个字,把字拷贝到寄存器t2中。sw $t2, ($t0)把t2中的字存入t0中的地址指向的主存位置。基

15、线寻址/索引寻址:based or indexed addressing:lw $t2, 4($t0)把t0中地址+4所得的地址所对应的主存中的字载入寄存器t2中,4为包含在t0中的地址的偏移量。sw $t2, -12($t0) # offset can be negative把t2中的内容存入t0中的地址-12所得的地址所对应的主存中,存入一个字,占用4字节,消耗4个内存号,可见,地址偏移量可以是负值。注意:基线寻址在以下场合特别有用:1、数组:从基址出发,通过使用偏移量,存取数组元素。2、堆栈:利用从堆栈指针或者框架指针的偏移量来存取元素。举个例子:#example.dataarray1:

16、 .space 12 # declare 12 bytes of storage # to hold array of 3 integers.text_start: la $t0, array1 # load base address of array # into register $t0li $t1, 5 # $t1 = 5 (load immediate)sw $t1, ($t0) # first array element set to 5; # indirect addressingli $t1, 13 # $t1 = 13sw $t1, 4($t0) # second array

17、element set to 13li $t1, -7 # $t1 = -7sw $t1, 8($t0) # third array element set to -7donePart IV 算术运算指令:Arithmetic Instructions 算数运算指令的所有操作数都是寄存器,不能直接使用RAM地址或间接寻址。 操作数的大小都为 Word (4-Byte)add $t0,$t1,$t2 # $t0 = $t1 + $t2; add as signed # (2s complement) integerssub $t2,$t3,$t4 # $t2 = $t3 $t4addi $t2,

18、$t3, 5 # $t2 = $t3 + 5; add immediate # (no sub immediate)addu $t1,$t6,$t7 # $t1 = $t6 + $t7; addu $t1,$t6,5 # $t1 = $t6 + 5; # add as unsigned integerssubu $t1,$t6,$t7 # $t1 = $t6 - $t7; subu $t1,$t6,5 # $t1 = $t6 - 5 # subtract as unsigned integersmult $t3,$t4 # multiply 32-bit quantities in $t3 #

19、 and $t4, and store 64-bit # result in special registers Lo # and Hi: (Hi,Lo) = $t3 * $t4div $t5,$t6 # Lo = $t5 / $t6 (integer quotient) # Hi = $t5 mod $t6 (remainder)mfhi $t0 # move quantity in special register Hi # to $t0: $t0 = Himflo $t1 # move quantity in special register Lo # to $t1: $t1 = Lo,

20、 used to get at # result of product or quotientmove $t2,$t3 # $t2 = $t3Part V 程序控制指令:Control Instructions 1. 分支指令(Branches) 条件分支的比较机制已经内建在指令中b target # unconditional branch to program label targetbeq $t0,$t1,target # branch to target if $t0 = $t1blt $t0,$t1,target # branch to target if $t0 $t1ble $t

21、0,$t1,target # branch to target if $t0 $t1bge $t0,$t1,target # branch to target if $t0 = $t1bne $t0,$t1,target # branch to target if $t0 $t1beqz $t0, lab # Branch to lab if $t0 = 0.bnez $t0, lab # Branch to lab if $t0 != 0.bgez $t0, lab # Branch to lab if $t0 = 0.bgtz $t0, lab # Branch to lab if $t0

22、 0.blez $t0, lab # Branch to lab if $t0 = 0.bltz $t0, lab # Branch to lab if $t0 = 0, then put the address of the next #instruction into $ra and branch to lab.bgtzal $t0, lab #If $t0 0, then put the address of the next #instruction into $ra and branch to lab.bltzal $t0, lab #If $t0 0, then put the a

23、ddress of the next #instruction into $ra and branch to lab. 2. 跳转指令(Jumps)j target # unconditional jump to program label targetjr $t3 #jump to address contained in $t3 (jump register) 3. 子程序调用指令 子程序调用指令的实质是跳转并链接(Jump and Link), 它把当前程序计数器的值保留到$ra中,以备跳回):跳转到子程序:jal sub_label # jump and link, preserve

24、pc to $rasub_label为子程序的标签,如 LOOP, SUB_ROUTINE从子程序返回:jr $ra # jump register jump as the value of $ra返回到$ra中储存的的返回地址对应的位置, $ra中的返回地址由jal指令保存。注意,返回地址存放在$ra寄存器中。如果子程序调用了下一级子程序,或者是递归调用,此时需要将返回地址保存在堆栈中,因为每执行一次jal指令就会覆盖$ra中的返回地址。Part VI: 系统调用和I/O操作(SPIM仿真)系统调用是指调用操作系统的特定子程序。系统调用用来在仿真器的窗口中打印或者读入字符串string, 并

25、可显示程序是否结束。用syscall指令进行对系统子程序的调用。本操作首先支持$v0 and $a0-$a1中的相对值调用以后的返回值(如果存在)会保存在$v0中。表二:系统调用的功能:ServiceCode in $v0ArgumentsResultsprint_int1print_float2print_double3print_string4read_int5integer returned in $v0read_float6float returned in $v0read_double7double returned in $v0read_string8$a0 = memory ad

26、dress of string input buffer$a1 = length of string buffer (n)sbrk9$a0 = amountaddress in $v0exit10The print_string service expects the address to start a null-terminated character string. The directive .asciiz creates a null-terminated character string.打印字符串的功能认为起始地址为一个空终止符串。声明字符串使用的.asciiz指示符会建立一个空

27、终止符串。The read_int, read_float and read_double services read an entire line of input up to and including the newline character.读入整形,读入浮点型和读入双精度的功能会读取一整行,包含换行符。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 t

28、he 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.读入字符串的功能和UNIX库中fgets函数的语法相同。他会读入n-1个字符到缓存,然后以空字符结尾。如果少于n-1的字符,它会读到结尾并包含换行符,并以空字符结尾。The sbrk service returns the address to a

29、block of memory containing n additional bytes. This would be used for dynamic memory allocation. sbrk功能返回一个包含有n个附加字节的存储区的地址,这回被用于动态内存分配。exit功能用于停止程序运行。e.g. Print out integer value contained in register $t2例:打印在$t2中的整数的值li $v0, 1 # load appropriate system call # code into register $v0; #code for printing integer is 1move $a0, $t2 # move integer to be printed # into $a0: $a0 = $t2syscall # call operating system to # perform operation#e.g. Read integer value, store in RAM location with label # int_value (presumably declared in data section)l

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1