ILD

mips 架构
作者:YUAN JIANPENG 邮箱:yuanjp@hust.edu.cn
发布时间:2018-11-10 站点:Inside Linux Development

Data Types and Literals

指令是32位

byte 8位,halfword 2个字节,word 4个字节


字面量

数字,如4

字符,如'b'

字符串,如"A string"


Registers

32个通用寄存器。

寄存器在汇编语言指令中用$开头,如$0

寄存器可以使用名字$0-$31,或别名,如$sp

特殊寄存器 Lo Hi 用来存储乘和除的运算结果,不能直接访问,使用特殊的指令mfhi和mflo拷贝到其它寄存器。

栈从高内存到低内存生长


寄存器

Register
Number
Alternative 
Name
Description

0

zero

the value 0

1

$at

(assembler temporary) reserved by the assembler

2-3

$v0 - $v1

(values) from expression evaluation and function results

4-7

$a0 - $a3

(arguments) First four parameters for subroutine.
Not preserved across procedure calls

8-15

$t0 - $t7

(temporaries) Caller saved if needed. Subroutines can use w/out saving.
Not preserved across procedure calls

16-23

$s0 - $s7

(saved values) - Callee saved. 
A subroutine using one of these must save original and restore it before exiting.
Preserved across procedure calls

24-25

$t8 - $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-27

$k0 - $k1

reserved for use by the interrupt/trap handler

28

$gp

global pointer. 
Points to the middle of the 64K block of memory in the static data segment.

29

$sp

stack pointer 
Points to last location on the stack.

30

$s8/$fp

saved value / frame pointer
Preserved across procedure calls

31

$ra

return address


Program Structure

数据段

.data


代码段

.text


注释

# 之后的内容


# commemt start

.data    # data segment

main:


Data Declarations

格式

name : storage type value(s)

如果类型是.space,则value表示多个字节


var1: .word 3

array1: .byte 'a', 'b'

array2: .space 40


Load / Store Instructions

内存访问只能通过 load /store指令

所有其它的指令只能操作寄存器


load:

lw register_destination, RAM_source  #拷贝4字节

lb register_destination, RAM_source  #拷贝1个字节


store:

sw

sb


拷贝立即数(硬编码在指令里)

li register_destination, value


example:
	.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 = var1
	li	$t1, 5		#  $t1 = 5   ("load immediate")
	sw	$t1, var1		# store contents of register $t1 into RAM:  var1 = $t1
	done


Indirect and Based Addressing

load address:

la $t0, var1

加载label var1的地址到$t0寄存器


indirect addressing

lw $t2 ($t0)

加载$t0寄存器表示的地址处的内容到寄存器$t2

sw $t2, ($t0)


based or indexed address

lw $t2, 4($t0)    

sw $t2, -12($t0)


example

		.data
array1:		.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 $t0
		li	$t1, 5		#  $t1 = 5   ("load immediate")
		sw $t1, ($t0)		#  first array element set to 5; indirect addressing
		li $t1, 13		#   $t1 = 13
		sw $t1, 4($t0)		#  second array element set to 13
		li $t1, -7		#   $t1 = -7
		sw $t1, 8($t0)		#  third array element set to -7
		done


Arithmetic Instructions

大多数使用3个指令

所有的操作数是寄存器

大小是word

add	$t0,$t1,$t2	#  $t0 = $t1 + $t2;   add as signed (2's complement) integers
sub	$t2,$t3,$t4	#  $t2 = $t3 Ð $t4
addi	$t2,$t3, 5	#  $t2 = $t3 + 5;   "add immediate" (no sub immediate)
addu	$t1,$t6,$t7	#  $t1 = $t6 + $t7;   add as unsigned integers
subu	$t1,$t6,$t7	#  $t1 = $t6 + $t7;   subtract as unsigned integers
mult	$t3,$t4		#  multiply 32-bit quantities in $t3 and $t4, and store 64-bit
			#  result in special registers Lo and Hi:  (Hi,Lo) = $t3 * $t4
div	$t5,$t6		#  Lo = $t5 / $t6   (integer quotient)
			#  Hi = $t5 mod $t6   (remainder)
mfhi	$t0		#  move quantity in special register Hi to $t0:   $t0 = Hi
mflo	$t1		#  move quantity in special register Lo to $t1:   $t1 = Lo
			#  used to get at result of product or quotient

move	$t2,$t3	        #  $t2 = $t3


Control Structures

branches 跳转

16位表示地址,跳转范围64K

		b	target		#  unconditional branch to program label target
		beq	$t0,$t1,target	#  branch to target if  $t0 = $t1
		blt	$t0,$t1,target	#  branch to target if  $t0 < $t1
		ble	$t0,$t1,target	#  branch to target if  $t0 <= $t1
		bgt	$t0,$t1,target	#  branch to target if  $t0 > $t1
		bge	$t0,$t1,target	#  branch to target if  $t0 >= $t1
		bne	$t0,$t1,target	#  branch to target if  $t0 <> $t1


Jumps

26位表示地址,跳转范围更大

		j	target	    #  unconditional jump to program label target
		jr	$t3	    #  jump to address contained in $t3 ("jump register")


Subroutine Calls

	jal	sub_label	#  "jump and link"

拷贝program counter (return address) 到  $ra (return address register)

跳转到sub_label处执行


subroutine return

	jr	$ra	#  "jump register"


如有函数嵌套调用,要把$ra保存到栈中。


System Calls

syscall指令,参数存储到通用寄存器


参考

MIPS Instruction Reference. http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html

MIPS32® Instruction Set Quick Reference. https://www2.cs.duke.edu/courses/fall13/compsci250/MIPS32_QRC.pdf

MIPS Architecture and Assembly Language Overview. http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm

https://en.wikipedia.org/wiki/MIPS_architecture

https://learnxinyminutes.com/docs/mips/



Copyright © insidelinuxdev.net 2017-2021. Some Rights Reserved.