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

合集下载

MIPS架构及其汇编语言【英】

MIPS架构及其汇编语言【英】

MIPS架构及其汇编语⾔【英】说明:MIPS指令包含硬件及MIPS汇编器所提供的指令,其中汇编器指令⼀般是将⼏个硬件指令合并为⼀个指令,以⽅便使⽤。

MIPS指令类型有以下⼏类:1. 数据传送类2. 算术、逻辑运算类3. 控制类4. 输⼊输出类还可详细划分为以下这些种:算术运算指令:各种数据的加减乘除逻辑运算指令:与或⾮、移位常数操作指令:⽴即数操作⽐较指令:⼤⼩等与不等分⽀指令:条件控制跳转指令:直接跳转、跳转并链接陷阱指令:各条件下的陷阱中断取数指令:按字节、半字、字从RAM取数保存指令:取数的逆操作数据传送指令:乘除寄存器、协处理器间的数据存取浮点运算指令:单精度、双精度浮点数的加减乘除、⽐较、传送、整型转换、舍⼊、平⽅根异常和中断指令:异常返回、系统调⽤、跳出、空操作⽽本⽂只介绍了⼀部分类型的指令,不过模式⼤同⼩异,应⽤时还需举⼀反三。

⽬录:MIPS汇编语⾔的数据类型MIPS处理器中的寄存器及分类MIPS汇编程序结构MIPS汇编语⾔变量声明⽴即寻址、间接寻址与基址寻址MIPS汇编语⾔中的各种指令算术指令RAM存取指令分⽀&跳转指令系统调⽤与I/O以下为原⽂内容:,可以参考这篇⽂章Data Types and LiteralsData types:Instructions are all 32 bitsbyte(8 bits), halfword (2 bytes), word (4 bytes)a character requires 1 byte of storagean integer requires 1 word (4 bytes) of storageLiterals:numbers entered as is. e.g. 4characters enclosed in single quotes. e.g. 'b'strings enclosed in double quotes. e.g. "A string"Registers32 general-purpose registersregister preceded by $ in assembly language instructiontwo formats for addressing:using register number e.g. 0through31using equivalent names e.g. t1,spspecial registers Lo and Hi used to store result of multiplication and divisionnot directly addressable; contents accessed with special instruction mfhi ("move from Hi") and mflo ("move from Lo") stack grows from high memory to low memoryThis is from Figure 9.9 in the Goodman&Miller textRegister Number AlternativeNameDescription0zero the value 01$at(assembler temporary) reserved by the assembler2-3v0−v1(values) from expression evaluation and function results4-7a0−a3(arguments) First four parameters for subroutine. Not preserved across procedure calls8-15t0−t7(temporaries) Caller saved if needed. Subroutines can use w/out saving.Not preserved across procedure calls16-23s0−s7(saved values) - Callee saved.A subroutine using one of these must save original and restore it before exiting.Preserved across procedure calls24-25t8−t9(temporaries) Caller saved if needed. Subroutines can use w/out saving.These are in addition to t0−t7 above.Not preserved across procedure calls.26-27k0−k1reserved for use by the interrupt/trap handler28$gp global pointer.Points to the middle of the 64K block of memory in the static data segment.29$sp stack pointerPoints to last location on the stack.30s8/fp saved value / frame pointer Preserved across procedure calls31$ra return addressSee also Britton section 1.9, Sweetman section 2.21, Larus Appendix section A.6Program Structurejust plain text file with data declarations, program code (name of file should end in suffix .s to be used with SPIM simulator) data declaration section followed by program code sectionData Declarationsplaced in section of program identified with assembler directive .datadeclares variable names used in program; storage allocated in main memory (RAM)Codeplaced in section of text identified with assembler directive .textcontains program code (instructions)starting point for code e.g.ecution given label main:ending point of main code should use exit system call (see below under System Calls)Commentsanything following # on a line# This stuff would be considered a commentTemplate for a MIPS assembly language program:# 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# ....text # instructions follow this linemain: # indicates start of code (first instruction to execute)# ...# End of program, leave a blank line afterwards to make SPIM happyData Declarationsformat for declarations:name: storage_type value(s)create storage for variable of specified type with given name and specified valuevalue(s) usually gives initial value(s); for storage type .space, gives number of spaces to be allocated Note: labels always followed by colon ( : )examplevar1: .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 array, or a# 10-element integer array; a comment should indicate which!Load / Store InstructionsRAM access only allowed with load and store instructionsall other instructions use register operandsload:lw register_destination, RAM_source#copy word (4 bytes) at source RAM location to destination register.lb 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 bytesstore word: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 destinationload immediate:li register_destination, value#load immediate value into destination registerexample:.datavar1: .word 23 # declare storage for var1; initial value is 23.text__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 = $t1doneIndirect and Based AddressingUsed only with load and store instructionsload address:la $t0, var1copy RAM address of var1 (presumably a label defined in the program) into register $t0 indirect addressing:lw $t2, ($t0)load word at RAM address contained in t0into t2sw $t2, ($t0)store word in register t2intoRAMataddresscontainedin t0based or indexed addressing:lw $t2, 4($t0)load word at RAM address (t0+4)intoregister t2"4" gives offset from address in register $t0sw $t2, -12($t0)store word in register t2intoRAMataddress(t0 - 12)negative offsets are fineNote: based addressing is especially useful for:arrays; access elements as offset from base addressstacks; easy to access elements at offset from stack pointer or frame pointerexample.dataarray1: .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 element set to 13li $t1, -7 # $t1 = -7sw $t1, 8($t0) # third array element set to -7doneArithmetic Instructionsmost use 3 operandsall operands are registers; no RAM or indirect addressingoperand size is word (4 bytes)add $t0,$t1,$t2 # $t0 = $t1 + $t2; add as signed (2's complement) integerssub $t2,$t3,$t4 # $t2 = $t3 Ð $t4addi $t2,$t3, 5 # $t2 = $t3 + 5; "add immediate" (no sub immediate)addu $t1,$t6,$t7 # $t1 = $t6 + $t7; add as unsigned integerssubu $t1,$t6,$t7 # $t1 = $t6 + $t7; subtract as unsigned integersmult $t3,$t4 # multiply 32-bit quantities in $t3 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# used to get at result of product or quotientmove $t2,$t3 # $t2 = $t3Control StructuresBranchescomparison for conditional branches is built into instructionb 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 $t0,$t1,target # branch to target if $t0 <= $t1bgt $t0,$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 <> $t1Jumpsj target # unconditional jump to program label targetjr $t3 # jump to address contained in $t3 ("jump register")Subroutine Callssubroutine call: "jump and link" instructionjal sub_label # "jump and link"copy program counter (return address) to register $ra (return address register)jump to program statement at sub_labelsubroutine return: "jump register" instructionjr $ra # "jump register"jump to return address in $ra (stored by jal instruction)Note: return address stored in register ra;ifsubroutinewillcallothersubroutines,orisrecursive,returnaddressshouldbecopiedfrom ra onto stack to preserve it, since jal always places return address in this register and hence will overwrite previous value System Calls and I/O (SPIM Simulator)used to read or print values or strings from input/output window, and indicate program enduse syscall operating system routine callfirst supply appropriate values in registers v0and a0-$a1result value (if any) returned in register $v0The following table lists the possible syscall services.Service Codein $v0Arguments Resultsprint_int1$a0 = integer to be printedprint_float2$f12 = float to be printedprint_double3$f12 = double to be printedprint_string4$a0 = address of string in memoryread_int5integer returned in $v0read_float6float returned in $v0read_double7double returned in $v0read_string8a0=memoryaddressofstringinputbuffer a1 = length of string buffer (n)sbrk9$a0 = amount address in $v0exit10The print_string service expects the address to start a null-terminated character string. The directive .asciiz creates a null-terminated character string.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 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 anull character.The sbrk service returns the address to a block of memory containing n additional bytes. This would be used for dynamicmemory allocation.The exit service stops a program from running.e.g. Print out integer value contained in register $t2li $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 operatione.g. Read integer value, store in RAM location with label int_value (presumably declared in data section)li $v0, 5 # load appropriate system call code into register $v0;# code for reading integer is 5syscall # call operating system to perform operationsw $v0, int_value # value read from keyboard returned in register $v0;# store this in desired locatione.g. Print out string (useful for prompts).datastring1 .asciiz "Print this.\n" # declaration for string variable,# .asciiz directive makes string null terminated.textmain: li $v0, 4 # load appropriate system call code into register $v0;# code for printing string is 4la $a0, string1 # load address of string to be printed into $a0syscall # call operating system to perform print operatione.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 = 10syscall # call operating sysProcessing math: 100%。

MIPS汇编及KDB入门最全版

MIPS汇编及KDB入门最全版

幸运
14
Mips汇编中的隐式规则

计算结构体数组元素偏移时,数组元素的大小总是采用左移 结合加减法完成的,因为乘除效率太低 下面是函数eth_mode_init()计算port_inf结构体大小的汇编 代码:v0 = portid; sll s0,v0,0x3 /*s0 = v0 << 3 = 8v0*/ addu s0,s0,v0 /*s0 = s0+v0 = 9v0*/ sll s0,s0,0x3 /*s0 = 8s0 = 72v0*/ subu s0,s0,v0 /*s0 = s0 –v0 = 71v0*/ sll s0,s0,0x4 /*s0 = 16s0 = 1136v0*/ subu s0,s0,v0 /* s0 = s0 –v0 = 1135v0 */ sll s0,s0,0x3 /*s0 = 8s0 = 9080v0*/ 从上面可以看出,如果反汇编代码对一个变量有一连串的左 移与加减运算就可猜测是在存取数组元素,有助于快速找到 幸运 对应的C代码 15

幸运
13
Mips汇编中的隐式规则
子函数的返回值约定存入v0, 赋值语句在子函数中 jalr v0 /*调用子函数*/ move a1,s1 /*参数赋值*/ beqz v0,0xdfc2ce04 /*这时v0的值已经自动改为子函数的返回值*/ 跳转进子函数时,a0-a4 是前四个参数的值,在调用函数中已经赋好, ra自动改为返回地址(隐式的),没有显式赋值语句 调用函数: 子函数: lui v0,0xcfb5 addiu sp,sp, -40 move a0,a1 sw ra,36(sp) /*ra = A,先保存*/ addiu v0,v0,24820 lui v0,0x802c jalr v0 addiu v0,v0,18544 move a1,a2 jalr v0 /*ra = B*/ A: move v0,s2 addiu a2,a2,-12 B: move a0,s0 lw ra,36(sp) jr ra说明

MIPS 指令系统和汇编语言

MIPS 指令系统和汇编语言

其中 A1 为目的操作数地址,A2 为源操作数地址。 指令的含义:(A1)OP(A2)→A1。 (3)一地址指令 一地址指令顾名思义只有一个显地址,它的指令格式为: OP A1
一地址指令只有一个地址, 那么另一个操作数来自何方呢?指令中虽未明显给出,但按事 先约定,这个隐含的操作数就放在一个专门的寄存器中。因为这个寄存器在连续性运算时,保 存着多条指令连续操作的累计结果,故称为累加寄存器(AC) 。 指令的含义:(AC)OP(A1)→AC (4)零地址指令 零地址指令格式中只有操作码字段,没有地址码字段,其格式为: OP 零地址的运算类指令仅用在堆栈计算机中的。 堆栈计算机没有一般计算机中必备的通用寄 存器,因此堆栈就成为提供操作数和保存运算结果的唯一场所。通常,参加运算的两个操作数 隐含地从堆栈顶部(栈顶和次栈顶单元)弹出,送到运算器中进行运算,运算的结果再隐含地 压入堆栈中。对于同一个问题,用三地址指令编写的程序最短,但指令长度(程序存储量)最 长;而用二、一、零地址指令来编写程序,程序的长度一个比一个长,但指令的长度一个比一 个短。
作码结构等,是一个很复杂的问题,它与计算机系统结构、数据表示方法、指令功能设计等都 密切相关。
指令的基本格式
一条指令就是机器语言的一个语句, 它是一组有意义的二进制代码, 指令的基本格式如下: 操作码字段 地址码字段
其中操作码指明了指令的操作性质及功能,地址码则给出了操作数的地址。 指令的长度是指一条指令中所包含的二进制代码的位数, 指令长度与机器字长没有固定的 关系,它可以等于机器字长,也可以大于或小于机器字长。通常,把指令长度等于机器字长的 指令称为单字长指令; 指令长度等于半个机器字长的指令称为半字长指令;指令长度等于两个 机器字长的指令称为双字长指令。 在一个指令系统中,若所有指令的长度都是相等的,称为定长指令字结构。定长结构指令 系统控制简单,但不够灵活。若各种指令的长度随指令功能而异,就称为变长指令字结构。现 代计算机广泛采用变长指令字结构,变长结构指令系统灵活,但指令的控制较复杂。 计算机执行一条指令所需要的全部信息都必须包含在指令中。 对于一般的双操作数运算类 指令来说,除去操作码之外,地址码字段中应包含以下信息: 第一操作数地址。 第二操作数地址。 操作结果存放地址。 这些信息可以在指令中明显的给出,称为显地址;也可以依照某种事先的约定,用隐含的 方式给出,称为隐地址。所以,从地址结构的角度可以分为三地址指令、二地址指令、一地址 指令和零地址指令。 (1)三地址指令 三地址指令格式为: OP A1 A2 A3

MIPS汇编学习

MIPS汇编学习

MIPS汇编学习MIPS汇编学习 mips汇编不同于x86汇编,属于精简指令集,常见于路由器等⼀些嵌⼊式设备中。

mips汇编没有对堆栈的直接操作,也就是没有push和pop指令,mips汇编中保留了32个通⽤寄存器,但是不同于x86汇编,mips汇编中没有ebp/rbp寄存器。

mips每条指令都⽤固定的长度,每条指令都是四个字节,所以内存数据的访问必须以32位严格对齐,这⼀点也不同于x86汇编。

通过⼀个demo,⽤mips-linux-gnu-gcc编译,通过IDA远程调试,来理解mips汇编中的⼀些概念。

#include<stdio.h>int sum(int a,int b){return a+b;}int main(){int a=1,b=2,c;c=sum(a,b);printf("%d\n",c);return 0;}32个通⽤寄存器的功能和使⽤约定定义如下:mips汇编中重要的寄存器: 1.堆栈指针$sp,也就是$29指向堆栈的栈顶,类似于x86中的ebp和rbp指针; 2.$0寄存器的值始终为常数0; 3.PC寄存器保留程序执⾏的下⼀条指令,相当于x86架构中的eip寄存器; 4.参数传递的时候,$a0-$a3寄存器保存函数的前四个参数,其他的参数保存在栈中; 5.$ra寄存器,保存着函数的返回地址,这⼀点也不同于x86汇编中将返回地址保存在栈中。

在函数A执⾏到调⽤函数B的指令时,函数调⽤指令复制当前的$PC寄存器的值到$RA寄存器,然后跳转到B函数去执⾏,即当前$RA寄存器的值就是函数执⾏结束时的返回地址。

如上图所⽰,调⽤sum函数之前,$ra寄存器的值是0x7f62eca8。

进⼊分⽀延迟槽之后,$ra寄存器的值被赋值为$pc寄存器的下⼀条指令地址。

在结束sun函数调⽤之后,通过:jr $ra指令跳转回main函数继续执⾏。

5.mips架构下,对静态数据段的访问,通过$gp寄存器配合基址寻址来实现; 7.$30寄存器表⽰帧指针,指向正在被调⽤的栈桢,mips和x86由于堆栈结构的区别,调⽤栈时会出现⼀些不同。

mips基础指令

mips基础指令

mips基础指令MIPS(Microprocessor without Interlocked Pipeline Stages)是一种高性能的RISC(Reduced Instruction Set Computing)指令集架构,其指令集被广泛应用于工业控制、嵌入式系统、服务器等领域。

MIPS指令集的特点是指令精简、寄存器丰富、执行效率高。

本文将主要介绍MIPS基础指令,帮助初学者快速入门。

一、MIPS基础指令结构MIPS指令的格式为“操作码r1,r2,r3”,其中操作码指示了该指令的功能,而r1、r2、r3分别指定了指令所需的操作数,通常为寄存器或常数。

以下是MIPS指令的分类:1.算术指令算术指令主要包括加、减、乘、除等指令,其中最基本的指令为ADD指令,格式为“ADD r1,r2,r3”,表示将r2和r3的值相加,存放到r1中。

其他的算术指令根据功能的不同,指定相应的操作码。

2.逻辑指令逻辑指令主要包括与、或、非、异或等指令,同样地,最基本的指令为AND指令,格式为“AND r1,r2,r3”,表示将r2和r3的值进行与运算,存放到r1中。

其他的逻辑指令根据功能的不同,指定相应的操作码。

3.移位指令移位指令主要包括逻辑左移、逻辑右移以及算术右移等指令,其中最基本的指令为SLL指令,格式为“SLL r1,r2,n”,表示将r2的值向左移动n位,空出的低位用0填充,存放到r1中。

其他的移位指令根据功能的不同,指定相应的操作码。

4.存取指令存取指令主要包括读、写、加载等指令,其中最基本的指令为LOAD指令,格式为“LOAD r1,addr”,表示将存储在addr地址中的数据加载到r1中。

其他的存取指令根据功能的不同,指定相应的操作码。

5.分支指令分支指令主要包括BZ(分支为零)、BC(分支为给定条件)、J(无条件跳转)等指令,其中最基本的指令为JUMP指令,格式为“JUMP addr”,表示将程序的控制跳转到addr地址处。

mips快速入门-入门指南-简单高效

mips快速入门-入门指南-简单高效

Load / Store Instructions
• RAM access only allowed with load and store instructions • all other instructions use register operands
load: lw register_destination, RAM_source
• store word: sw register_source, RAM_destination
• #store word in source register into RAM destination
sb register_source, RAM_destination
• #store byte (low-order) in source register into RAM destination
Mips快速入门
Program Structure • just plain text file with data declarations, program code (name of file should end in suffix .s to be used with SPIM simulator) 1. data declaration section 2. program code section

.text start: lw $t0, var1
# load contents of RAM location into register $t0: $t0 = var1
li $t1, 5 # $t1 = 5 ("load immediate") sw $t1, var1
# store contents of register $t1 into RAM: var1 = $t1

实验三MIPS指令系统和MIPS体系结构

实验三MIPS指令系统和MIPS体系结构

实验3 MIPS指令系统和MIPS体系结构一.实验目的(1)了解和熟悉指令级模拟器(2)熟悉掌握MIPSsim模拟器的操作和使用方法(3)熟悉MIPS指令系统及其特点,加深对MIPS指令操作语义的理解(4)熟悉MIPS体系结构二. 实验内容和步骤首先要阅读MIPSsim模拟器的使用方法,然后了解MIPSsim的指令系统。

(1)、启动MIPSsim。

(2)、选择“配置”->“流水方式”选项,使模拟器工作在非流水方式。

(3)、参照使用说明,熟悉MIPSsim模拟器的操作和使用方法。

(4)、选择“文件”->“载入程序”选项,加载样例程序 alltest.asm,然后查看“代码”窗口,查看程序所在的位置。

(5)、查看“寄存器”窗口PC寄存器的值:[PC]=0X00000000 。

(6)、执行load和store指令,步骤如下:1)单步执行一条指令(F7)。

2)下一条指令地址为OX00000004 ,是一条有(有,无)符号载入 (字节,半字,字)指令。

3)单步执行一条指令(F7)。

4)查看R1的值,[R1]= 0xFFFFFFFFFFFFFF80 。

5)下一条指令地址为 OX000000008 ,是一条有 (有,无)符号载入(字节,半字,字)指令。

6)单步执行1条指令。

7)查看R1的值,[R1]=0x0。

8)下一条指令地址为 0x0000000c ,是一条无(有,无)符号载入(字节,半字,字)指令。

9)单步执行1条指令。

10)查看R1的值,[R1]=0x0 。

11)单步执行1条指令。

12)下一条指令地址为0x00000014 ,是一条保存字节(字节,半字,字)指令。

13)单步执行一条指令。

14)查看内存BUFFER处字的值,值为0x00000080。

(7)、执行算术运算类指令。

步骤如下:1)双击“寄存器”窗口中的R1,将其值修改为2。

2)双击“寄存器”窗口中的R2,将其值修改为3。

3)单步执行一条指令。

MIPS指令系统和汇编语言

MIPS指令系统和汇编语言

MIPS指令系统和汇编语言MIPS(Microprocessor without Interlocked Pipeline Stages)指令系统,是一种以RISC(Reduced Instruction Set Computer,精简指令集计算机)为基础的处理器架构。

作为一种广泛应用于嵌入式系统和计算机组成的指令集架构,MIPS指令系统以其简洁高效的特性而受到广泛关注和应用。

一、MIPS指令系统概述MIPS指令系统的设计目标之一是提高处理器的性能,并降低设计的复杂性。

它采用了统一的指令格式,包括操作码、源操作数以及目的操作数等字段,使得指令的译码和执行过程更加高效。

此外,MIPS的指令集还支持延迟槽、流水线和分支延迟等特性,以进一步提升指令执行的效率。

二、MIPS指令格式MIPS指令格式遵循统一的规则,包括三种基本类型的指令格式:R 型、I型和J型指令。

R型指令主要用于寄存器之间的操作,包括算术运算、逻辑运算等;I型指令用于立即数和寄存器之间的操作,涵盖了数据传输、分支跳转等功能;J型指令主要用于无条件跳转。

三、MIPS指令编码和寻址方式MIPS指令采用固定长度的指令编码格式,使得指令的解析和处理更加高效。

在寻址方面,MIPS支持多种寻址方式,包括立即寻址、寄存器寻址和间接寻址等。

这些灵活的寻址方式使得MIPS指令更加适用于不同的计算需求。

四、MIPS汇编语言MIPS汇编语言是一种用于编写MIPS指令的低级语言。

它是一种基于文本的表示形式,使用助记符来表示不同的指令和操作。

MIPS汇编语言具有简单易学的特性,更加接近底层硬件的工作原理,使得程序员可以更加精准地控制和优化程序的执行过程。

五、MIPS指令系统的应用由于MIPS指令系统的优越性能和灵活性,它被广泛应用于各种领域。

在嵌入式系统中,MIPS处理器可以实现高性能和低功耗的设计,广泛应用于智能手机、路由器、电视机等设备中。

在计算机组成和操作系统领域,MIPS指令系统被用于讲解和研究计算机的工作原理和底层机制。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

MIPS 体系结构和汇编语言快速入门
译者:Sonic Fu, Northeastern University, Boston, MA, USA
译者按:有修改,无删减,初学必读。

学习笔记,抛砖引玉!网上有一个老版本,不如此版全面。

英文原版:/366/notes/mips%20quick%20tutorial.htm#IOSystemCalls
本文分3部分:1、寄存器2、程序结构框架3、编写汇编程序
概要:数据类型和文法
数据类型:字节,byte占用( 8bit ), halfword占2 byte= 16bit), 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.)
堆栈(Stack)的增长方向是: 从内存的高地址方向, 向低地址方向;
汇编程序结构框架
汇编源程序代码本质上是文本文件。

由数据声明、代码段两部分组成。

汇编程序文件应该以.s为后缀,以在Spim软件中进行模拟。

(实际上ASM也行。


数据声明部分
在源代码中,数据声明部分以.data开始。

声明了在代码中使用的变量的名字。

同时,也在主存(RAM)中创建了对应的空间。

程序代码部分
在源代码中,程序代码部分以.text开始。

这部分包含了由指令构成的程序功能代码。

代码以main: 函数开始。

main的结束点应该调用exit system call,参见后文有关system call 的介绍。

程序的注释部分
使用#符号进行注释。

每行以#引导的部分都被视作注释。

一个MIPS汇编程序框架:
编写MIPS汇编程序:
Content:
PartI: 数据的声明
Part II:数据的装载和保存(Load/Store 指令)
Part III:寻址
Part IV:算术运算指令:Arithmetic Instructions
Part V 程序控制指令:Control Instructions
Part VI: 系统调用和I/O操作(SPIM仿真)
PartI:数据的声明
创建一个以name为变量名称,values通常为初始值,storage_type代表存储类型。

Part II:数据的装载和保存(Load/Store 指令)
主存(RAM)的存取access只能用load / store 指令来完成。

所有其他的指令都使用的是寄存器作为操作数。

举个例子:
MIPS系统结构只能用load/store 相关指令来实现寻址操作,包含3中寻址方式:装载地址:load address,相当于直接寻址,把数据地址直接载入寄存器。

把var1在主存(RAM)中的地址拷贝到寄存器t0中。

var1也可以是程序中定义的一个
主存中有一个字的地址存在t0中,按这个地址找到那个字,把字拷贝到寄存器t2中。

把t2中的字存入t0中的地址指向的主存位置。

址的偏移量。

把t2中的内容存入t0中的地址-12所得的地址所对应的主存中,存入一个字,占用4
字节,消耗4个内存号,可见,地址偏移量可以是负值。

注意:基线寻址在以下场合特别有用:
1、数组:从基址出发,通过使用偏移量,存取数组元素。

2、堆栈:利用从堆栈指针或者框架指针的偏移量来存取元素。

Part IV 算术运算指令:Arithmetic Instructions
算数运算指令的所有操作数都是寄存器,不能直接使用RAM地址或间接寻址。

操作数的大小都为Word (4-Byte)
Part V 程序控制指令:Control Instructions
1. 分支指令(Branches)
3. 子程序调用指令
子程序调用指令的实质是跳转并链接(Jump and Link), 它把当前程序计数器的值保留到$ra中,以备跳回):
sub_label为子程序的标签,如LOOP,SUB_ROUTINE
注意,返回地址存放在$ra寄存器中。

如果子程序调用了下一级子程序,或者是递归调用,此时需要将返回地址保存在堆栈中,因为每执行一次jal指令就会覆盖$ra中的返回地址。

Part VI: 系统调用和I/O操作(SPIM仿真)
系统调用是指调用操作系统的特定子程序。

系统调用用来在仿真器的窗口中打印或者读入字符串string, 并可显示程序是否结束。

用syscall指令进行对系统子程序的调用。

本操作首先支持$v0 and $a0-$a1中的相对值
调用以后的返回值(如果存在)会保存在$v0中。

The print_string service expects the address to start a null-terminated character string. The directive .asciiz creates a null-terminated character string.
打印字符串的功能认为起始地址为一个空终止符串。

声明字符串使用的.asciiz指示符会建立一个空终止符串。

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 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.
读入字符串的功能和UNIX库中fgets函数的语法相同。

他会读入n-1个字符到缓存,然后以空字符结尾。

如果少于n-1的字符,它会读到结尾并包含换行符,并以空字符结尾。

The sbrk service returns the address to a 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
附:ASCII Code Table:。

相关文档
最新文档