http://blog.csdn.net/sdreamq/article/details/51044913
MIPS体系结构--指令集
标签: mips内核汇编
2016-04-02 18:36 1458人阅读 评论(0) 收藏 举报
分类: mips体系结构(5)
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)
mips体系结构下的汇编指令
指令集是存储在CPU内部,对CPU运算进行指导和优化的硬程序。拥有这些指令集,CPU就可以更高效地运行。
Mips汇编语言的风格
汇编语言指令格式
操作符
标签: (可选)
标记内存地址, 必须跟冒号
通常在数据和代码段出现
操作符
定义操作 (比如 add, sub, 等)
操作数
指明操作需要的数据
操作数可以是寄存器,内存变量或常数
大多数指令有3个操作数
1
2
3
# this is a comment
entrypoint: # that’s a label
add $1 , $2, $3 # (registers) $1 = $2 + $3
xxx:—— 定义代码的人口点和命名数据段的存储段
程序结构
汇编语言的程序结构,为数据声明+代码段+数据段(文件后缀为.s,或者.asm也行),数据声明在代码段之后(其实在其之前也没啥问题,也更符合高级程序设计的习惯)
数据声明
数据段以 .data为开始标志
声明变量后,即在主存(RAM)中分配空间。
1
2
3
4
5
6
7
Example:
var1:
.word 3 # 声明一个 word 类型的变量 var1, 同时给其赋值为 3
array1:
.byte 'a','b' # 声明一个存储2个字符的数组 array1,并赋值 'a', 'b'
array2:
.space 40 # 为变量 array2 分配 40字节(bytes)未使用的连续空间,当然,对于这个变量
代码段:
代码段以 .text为开始标志
其实就是各项指令操作
程序入口为main:标志(这个都一样啦)
程序结束标志(详见下文)
其他:
.data //数据段
.text //代码段
.globl //全局符号声明
.align 0 //关闭所有的自动对齐
.asciiz //字符串(带终止符)
寄存器
32个通用寄存器:
MIPS的寄存器约定,一种32个寄存器
0 zero: 总返回0
1 at: (汇编暂存寄存器)为汇编保留
2-3 v0、v1:存放子函数调用返回结果,还可用于表达式求值
4-7 a0 - a3:存放向子函数传递的参数
8-15 t0- t7:存放临时运算结果,在发生函数调用时不必保存它们的内容
24,25 t8-t9:
16-23 s0 - s7:存放局部变量,在发生函数调用时一般要保存它们的内容
26,27 k0, k1:为中断/陷入处理保留,你也可以改变
28 gp:全局指针
29 sp: 栈(stack)指针
30 s8/fp: 帧(frame)指针
31 ra: 返回地址(用于过程调用
数据类型
MIPS CPU的一次操作可加载或存储1到8个字节的数据。
MIPS名称
大小(字节)
汇编器助记符
dword 8 “d”代表ld
word 4 “w”代表lw
halfword 2 “h”代表lh
byte 1 “b”代表lb `
操作指令
加载和存储(load、store)
Load
在一个内存地址加载word/halfword/byte的数据到一个寄存器
lw r, a R<-a
lh r, a 无符号
lb r, a 无符号
lhu 有符号
lhbu 有符号
li r,c 加载立即数
Store
将寄存器中的数据存储到内存地址
Sw r, a R->a
Sh r, a Store low halfword
Sb r, a Store low byte
Move:
寄存器之间数据的直接交换
move r , s R<-s
逻辑运算
and r, s, t R <-s . t
or r, s, t R <-s + t
not r, s R <-s取反
xor r, s, t R <-s异或t
nor r, s, t R <-(s+t)再取反
算数运算
add r, s, t R->s + t
sub r, s, t R->s – t
mul r, s, t R->s*t
div r, s, t R->s/t
hi和lo乘法器相关的寄存器规模结果接口,不能用于乘和除之外的操作。对于以上二者,不存在直接寻址;必须要通过mfhi(“move from hi”)以及mflo(“move from lo”)两条指令分别来进行访问对应的内容。
乘法——将两个整数的相乘结果分成两部分存储的指定的寄存器里。
除法——lo寄存器存储结果(商),hi寄存器存储余数。
移位操作
sll r, s, c r ← shift s left c bits
srl r, s, c r ← shift s right c bits
分支操作
通过条件判断,使程序跳转到tag
b tag 跳转->tag
beq r, s, tag R=s ->tag
bne r, s, tag R != s->tag
bgt r, s, tag R > s->tag
bge r, s, tag R >=s->tag
blt r, s, tag R < s->tag
ble r, s, tag R<=s->tag
比较指令
slt r, s, t S < t
sle r, s, t S <= t
sgt r, s, t S >t
sge r, s, t S >= t
seq r, s, t S = t
sne r, s, t S != t
FALSE r <- 0
TRUE r <- 1
寻址方式
直接寻址:
MIPS只有一种寻址方式。任何加载或存储机器指令可以写成
lw $1, offset($2)
你可以使用任何寄存器来作为目标和源寄存器。offset偏移量是一个有符号的16位
的数字(因此可以是在-32768与32767之间的任何一值)。用来加载的程序地址是源寄
存器与偏移量的和所构成的地址。
($t0) —— 默认从0地址偏移$t0字节
访问连续的内存空间:
根据程序的实际需要可能进行字符串等数据的访问。
.ascii s ASCII encoded characters of string s
.asciiz s l ike .ascii, null-terminated
.word w1, w2,… 32-bit words w1, w2, . . .
.half h 1, h 2, . . . 16-bit halfwords h 1, h 2, . . .
.byte b1, b2, . . . 8-bit bytes b1, b2, . . .
.float f1, f2, . . . 32-bit single precision floating point numbers f1, f2, . . .
.double d1, d2, . . . 64-bit double precision floating point numbers d1, d2, . . .
.space n n zero bytes
例如:
1
2
.data
str: .asciiz "hello word"
系统调用:syscall
MIPS 提供一条特殊的 syscall 指令,从操作系统获取服务
使用 syscall 系统服务
从 $v0寄存器中读取服务数
从 $a0, $a1, 等寄存器中读取参数值(如果有)
发送 syscall 指令
从结果寄存器中取回返回值(如果有)
$v0 中包含调用号(共12个):
Service $v0 Arguments / Result
Print Integer 1 $a0 = integer value to print
Print Float 2 $f12 = float value to print
Print Double 3 $f12 = double vlaue to print
print String 4 $a0 = address of null-terminated string
Read Integer 5 $v0 = integer read
Read Float 6 $f0 = float read
Read Double 7 $f0 = double read
Read String 8 $a0 = address of input buffer$a1 = maximum number of characters to read
Exit Program 10
Print Char 11 $a0 = character to print
Read Char 12 $a0 = character read
程序调用
jal a (jump and link)将程序跳转到地址a并将下一条指令的地址存储在$ra寄存器中
j $ra 返回到程序跳转时的位置,无条件跳转
Last edited by zzz19760225 on 2017-6-15 at 23:33 ]
http://blog.csdn.net/sdreamq/article/details/51044913
MIPS Architecture -- Instruction Set
Tags: MIPS core assembly
2016-04-02 18:36 1458 views Comments(0) Favorite Report
Classification: MIPS Architecture (5)
Copyright Notice: This article is an original article by the blogger. Reproduction is not permitted without the blogger's permission.
Table of Contents(?)
Assembly Instructions under the MIPS Architecture
An instruction set is a hard program stored inside the CPU that guides and optimizes CPU operations. With these instruction sets, the CPU can run more efficiently.
Style of MIPS Assembly Language
Format of Assembly Language Instructions
Operator
Label: (optional)
Marks the memory address, must be followed by a colon
Usually appears in data and code segments
Operator
Defines the operation (such as add, sub, etc.)
Operand
Indicates the data required for the operation
Operands can be registers, memory variables, or constants
Most instructions have 3 operands
1
2
3
# this is a comment
entrypoint: # that’s a label
add $1 , $2, $3 # (registers) $1 = $2 + $3
xxx: —— Defines the entry point of the code and the storage segment of the named data segment
Program Structure
The program structure of assembly language is data declaration + code segment + data segment (file suffix is .s or .asm), and data declaration comes after the code segment (actually, it's okay before it, and it's more in line with the habits of high-level programming)
Data Declaration
The data segment starts with .data
After declaring a variable, space is allocated in the main memory (RAM).
1
2
3
4
5
6
7
Example:
var1:
.word 3 # Declare a word-type variable var1 and assign it the value 3
array1:
.byte 'a','b' # Declare an array array1 that stores 2 characters and assign 'a', 'b'
array2:
.space 40 # Allocate 40 bytes (bytes) of unused continuous space for variable array2. Of course, for this variable
Code Segment:
The code segment starts with .text
Actually, it's various instruction operations
The program entry is marked by main: (it's all the same)
Program end mark (see below for details)
Others:
.data //Data segment
.text //Code segment
.globl //Global symbol declaration
.align 0 //Turn off all automatic alignment
.asciiz //String (with terminator)
Registers
32 general-purpose registers:
MIPS register convention, a set of 32 registers
0 zero: always returns 0
1 at: (assembly temporary register) reserved for assembly
2-3 v0, v1: store sub-function call return results, can also be used for expression evaluation
4-7 a0 - a3: store parameters passed to sub-functions
8-15 t0- t7: store temporary operation results, their contents do not need to be saved when a function call occurs
24,25 t8-t9:
16-23 s0 - s7: store local variables, generally need to save their contents when a function call occurs
26,27 k0, k1: reserved for interrupt/trap handling, you can also change
28 gp: global pointer
29 sp: stack pointer
30 s8/fp: frame pointer
31 ra: return address (for procedure call)
Data Types
A single operation of the MIPS CPU can load or store 1 to 8 bytes of data.
MIPS Name
Size (bytes)
Assembler mnemonic
dword 8 “d” stands for ld
word 4 “w” stands for lw
halfword 2 “h” stands for lh
byte 1 “b” stands for lb `
Instruction Operations
Load and Store (load, store)
Load
Load word/halfword/byte data from a memory address into a register
lw r, a R<-a
lh r, a Unsigned
lb r, a Unsigned
lhu Signed
lhbu Signed
li r,c Load immediate
Store
Store data in a register to a memory address
Sw r, a R->a
Sh r, a Store low halfword
Sb r, a Store low byte
Move:
Direct exchange of data between registers
move r , s R<-s
Logical Operations
and r, s, t R <-s . t
or r, s, t R <-s + t
not r, s R <-s inverted
xor r, s, t R <-s XOR t
nor r, s, t R <-(s+t) then inverted
Arithmetic Operations
add r, s, t R->s + t
sub r, s, t R->s – t
mul r, s, t R->s*t
div r, s, t R->s/t
hi and lo are the result interfaces related to the multiplier register, and cannot be used for operations other than multiplication and division. For the above two, there is no direct addressing; you must access the corresponding content through the mfhi ("move from hi") and mflo ("move from lo") instructions respectively.
Multiplication - stores the result of multiplying two integers into the specified registers in two parts.
Division - the lo register stores the result (quotient), and the hi register stores the remainder.
Shift Operations
sll r, s, c r ← shift s left c bits
srl r, s, c r ← shift s right c bits
Branch Operations
Jump to tag through conditional judgment
b tag Jump->tag
beq r, s, tag R=s ->tag
bne r, s, tag R != s->tag
bgt r, s, tag R > s->tag
bge r, s, tag R >=s->tag
blt r, s, tag R < s->tag
ble r, s, tag R<=s->tag
Comparison Instructions
slt r, s, t S < t
sle r, s, t S <= t
sgt r, s, t S >t
sge r, s, t S >= t
seq r, s, t S = t
sne r, s, t S != t
FALSE r <- 0
TRUE r <- 1
Addressing Modes
Immediate Addressing:
MIPS has only one addressing mode. Any load or store machine instruction can be written as
lw $1, offset($2)
You can use any register as the target and source register. The offset is a signed 16-bit number (so it can be any value between -32768 and 32767). The program address for loading is the address formed by the sum of the source register and the offset.
($t0) —— Default to offset $t0 bytes from address 0
Accessing Continuous Memory Space:
Access to data such as strings may be required according to the actual needs of the program.
.ascii s ASCII encoded characters of string s
.asciiz s l ike .ascii, null-terminated
.word w1, w2,… 32-bit words w1, w2, . . .
.half h 1, h 2, . . . 16-bit halfwords h 1, h 2, . . .
.byte b1, b2, . . . 8-bit bytes b1, b2, . . .
.float f1, f2, . . . 32-bit single precision floating point numbers f1, f2, . . .
.double d1, d2, . . . 64-bit double precision floating point numbers d1, d2, . . .
.space n n zero bytes
For example:
1
2
.data
str: .asciiz "hello word"
System Call: syscall
MIPS provides a special syscall instruction to get services from the operating system
Use syscall system service
Read the service number from the $v0 register
Read parameter values from registers such as $a0, $a1, etc. (if any)
Send the syscall instruction
Retrieve the return value from the result register (if any)
The $v0 contains the call number (total 12):
Service $v0 Arguments / Result
Print Integer 1 $a0 = integer value to print
Print Float 2 $f12 = float value to print
Print Double 3 $f12 = double vlaue to print
print String 4 $a0 = address of null-terminated string
Read Integer 5 $v0 = integer read
Read Float 6 $f0 = float read
Read Double 7 $f0 = double read
Read String 8 $a0 = address of input buffer$a1 = maximum number of characters to read
Exit Program 10
Print Char 11 $a0 = character to print
Read Char 12 $a0 = character read
Procedure Call
jal a (jump and link)Jump to address a and store the address of the next instruction in the $ra register
j $ra Return to the position when the program jumped, unconditional jump
Last edited by zzz19760225 on 2017-6-15 at 23:33 ]