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

上传人:野鹰 文档编号:1128138 上传时间:2017-05-29 格式:PDF 页数:10 大小:721.93KB
返回 下载 相关 举报
MIPS 体系结构和汇编语言快速入门_第1页
第1页 / 共10页
MIPS 体系结构和汇编语言快速入门_第2页
第2页 / 共10页
MIPS 体系结构和汇编语言快速入门_第3页
第3页 / 共10页
MIPS 体系结构和汇编语言快速入门_第4页
第4页 / 共10页
MIPS 体系结构和汇编语言快速入门_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《MIPS 体系结构和汇编语言快速入门》由会员分享,可在线阅读,更多相关《MIPS 体系结构和汇编语言快速入门(10页珍藏版)》请在金锄头文库上搜索。

1、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= 16b

2、it), 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

3、, mflo = Move from Low.) 堆栈( Stack)的增长方向是 : 从内存的高地址方向 , 向低地址方向; 表格 1: 寄存器的编号名称及分类 编号 寄存器 名称 寄存器描述 0 Zero 第 0 号寄存器,其值始终为 0 1 $at (Assembler Temporary) 是 Assembler 保留的寄存器 2 3 $v0 $v1 (values)保存表达式或函数返回的结果 4-7 $a0 - $a3 (arguments) 作为函数的前四个入参。 在 子 函数调用的过程中不会被保 留。 8-15 $t0 - $t7 (temporaries) Caller sav

4、ed if needed. Subroutines can use without saving.供汇编程序使用的临时寄存器。 在 子 函数调用的过程中不会被保 留。 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 u

5、se without saving.供汇编程序使用的临时寄存器。在子函数调用的过程中不会被保留。这是对 $t0 - $t7 的补充。 26-27 $k0 - $k1 保留,仅供中断 (interrupt/trap)处理函数使用 . 28 $gp global pointer. 全局指针。 Points to the middle of the 64K block of memory in the static data segment.指向固态数据块内存的 64K 的块的中间。 29 $sp stack pointer 堆栈指针, 指向堆栈的栈顶。 30 $s8/$fp saved value

6、 / frame pointer 保存的值 /帧指针 其 中的 值在函数调用的过程中会被保留 31 $ra return address 返回地址 汇编程序结构 框架 汇编源程序代码本质上是文本文件。由 数据声明、代码段 两部分组成。 汇编 程序文件应该以 .s 为后缀 ,以在 Spim 软件中进行模拟。 (实际上 ASM 也行。) 数据声明 部分 在源代码中,数据声明部分以 .data 开始。 声明了在代码中使用的变量的名字。同时,也在 主 存 ( RAM) 中创 建了对应的空间。 程序代码部分 在源代码中 ,程序代码部分以 .text 开始。这部分包含了由指令构成的程序功能代码。 代码以

7、main: 函数开始。 main 的结束点应该调用 exit system call,参见后文有关 system call 的介绍。 程序的注释 部分 使用 #符号进行注释。每行以 #引导的部分都被视作注释。 一个 MIPS 汇编 程序 框架 : # Comment giving name of program and description of function # Template.s # Bare-bones outline of MIPS assembly language program .data # variable declarations follow this line

8、# . .text # instructions follow this line main: # indicates start of code (first instruction to execute) # . # End of program, leave a blank line afterwards to make SPIM happy 编写 MIPS 汇编程序: Content: PartI: 数据的声明 Part II: 数据的装载和保存( Load/Store 指令 ) Part III: 寻址 Part IV: 算术 运算 指令: Arithmetic Instructio

9、ns Part V 程序控制指令: Control Instructions Part VI: 系统调用和 I/O 操作( SPIM 仿真) PartI:数据的声明 格式: name: storage_type value(s) 创建 一个以 name 为 变量名称 , values 通常为初始值 , storage_type 代表 存储类型 。 注意:变量名后要跟一个 :冒号 example var1: .word 3 # create a single integer: #variable with initial value 3 array1: .byte a,b # create a

10、2-element character # array with elements initialized: # to a and b array2: .space 40 # allocate 40 consecutive bytes, # with storage uninitialized # could be used as a 40-element # character array, or a # 10-element integer array; # a comment should indicate it. string1 .asciiz Print this.n #declar

11、e a string Part II: 数据的装载和保存 ( Load/Store 指令 ) 主存 ( RAM) 的存取 access 只能用 load / store 指令来完成。 所有其他的指令都使用的是寄存器作为操作数。 i. load 指令 : lw register_destination, RAM_source # copy word (4 bytes) at # source_RAM location # to destination register. # load word - lw lb register_destination, RAM_source # copy byt

12、e 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 register #load immediate li ii. store 指令 sw register_source, RAM_destination #store word in so

13、urce register # into RAM destination sb register_source, RAM_destination #store byte (low-order) in #source register into RAM #destination 举个例子: .data var1: .word 23 # declare storage for var1; #initial value is 23 .text _start: lw $t0, var1 # load contents of RAM location # into register $t0: # $t0

14、 = var1 li $t1, 5 # $t1 = 5 (load immediate) sw $t1, var1 # store contents of register $t1 # into RAM: var1 = $t1 done done Part III:寻址 : MIPS 系统结构 只能用 load/store 相关指令来实现寻址操作, 包含 3 中寻址方式: 装载地址: load address,相当于直接寻址,把数据地址直接载入寄存器。 间接寻址: indirect addressing,间接寻址,把寄存器内容作为地址 基线寻址 /索引寻址: based or indexed

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

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 研究报告 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号