mips寄存器介绍

一 概要

mips拥有32个通用寄存器,一个pc寄存器,一个HI和LO寄存器,另外协处理器也有自己的寄存器,如CP0有32个单独的寄存器(不同的厂商可以实现自己独特的寄存器,用MTC0 rt, rd, sel, sel进行区分),浮点协处理单元也有自己独立的寄存器。


二 32个通用寄存器


s0–s8: By convention, subroutines must guarantee that thevalues of

these registers on exit are the same as they were on entry, either by not

using them or by saving them on the stack and restoring them before

exit. This makes them eminently suitable for use as register variables or

for storing any value that must be preserved over a subroutine call.

 

k0, k1: Reserved for use by an OS trap/interrupt handlers,which will use

them and not restore their original value; they are of little use toanyone

else.

 

gp: This is used for two distinct purposes. In the kind ofpositionindependent

code (PIC) used by Linux applications, all out-of-module

code and data references go through a table of pointers known as the

GOT (for Global Offset Table). The gp register is maintained to point to

the GOT. See Chapter 16 for details.

In regular non-PIC code (as is used by simpler embedded systems) gp

is sometimes used to point to a link-time-determined location in the midstof your static data. This means that loads and stores to data lying

within 32 KB of either side of the gp value can be performed in a single

instruction using gp as the base register.

Without the global pointer, loading data from a static memory area takes

two instructions: one to load the most significant bits of the 32-bitconstant

address computed by the compiler and loader and one to do the

data load.

To create gp-relative references, a compiler must know at compile time

that a datum will end up linked within a 64-KB range of memory locations.

In practice it can’t know; it can only guess. The usual practice is

to put small global data items (eight bytes and less in size) in thegparea

and to get the linker to complain if it still gets too big.

 

sp: It takes explicit instructions to raise and lower thestack pointer, so

MIPS code usually adjusts the stack only on subroutine entry and exit;

it is the responsibility of the subroutine being called to do this.spis

normally adjusted, on entry, to the lowest point that the stack will need

to reach at any point in the subroutine.Nowthe compiler can access stack

variables by a constant offset from sp. Once again, see section 11.2.1 for

conventions about stack usage.

 

fp: Also known as s8, a frame pointer will be used by asubroutine

to keep track of the stack if for any reason the compiler or programmer

was unable or unwilling to compute offsets from the stack pointer.

That might happen, in particular, if the programdoes things that involve

extending the stack by an amount that is determined at run time.

Some languages may do this explicitly; assembly programmers are

always welcome to experiment; and C programs that use the alloca()

library routine will find themselves doing so.

If the stack bottom can’t be computed at compile time, you can’t access

stack variables from sp, so fp is initialized by the function prologue to

a constant position relative to the function’s stack frame. Cunning use

of register conventions means that this behavior is local to the function

and doesn’t affect either the calling code or any nested function calls.

 

ra: On entry to any subroutine, return address holds theaddress to

which control should be returned—so a subroutine typically ends with

the instruction jr ra. In theory, any register could be used, but some

sophisticated CPUs use optimization (branch prediction) tricks, which

work better if you use a jr ra.

Subroutines that themselves call subroutines must first save ra, usually

on the stack.

详细可以参考:see mips run.pdf


三 CP0寄存器




1. Registers used in memory management.
2. Registers used in exception processing.
3. Registers used in debug.

详细可以参考:4KcProgMan.pdf (MD00016)和 MIPS® Architecture For Programmers Vol. III: MIPS32®/microMIPS32™ Privileged Resource Architecture (MD00090)

### MIPS架构中的寄存器概述 MIPS架构是一种精简指令集计算机(RISC)架构,其寄存器设计简洁高效,适用于多种应用场景。以下是关于MIPS寄存器的相关功能和用途的详细介绍。 #### 1. **通用寄存器** MIPS架构提供了32个通用寄存器,编号从`$0`到`$31`,每个寄存器都有特定的名称和用途[^2]。这些寄存器主要用于存储数据、中间计算结果以及函数调用参数等。具体分配如下: - `$0 (zero)`:始终返回0,无论写入任何值都不会改变。 - `$1 (at)`:供汇编器使用,通常用于伪指令扩展。 - `$2-$3 (v0, v1)`:用于存放子程序的返回值(非浮点型)。 - `$4-$7 (a0-a3)`:用于传递子程序的前四个参数(非浮点型)。 - `$8-$15 (t0-t7)`:作为临时寄存器,用于子程序内的局部变量存储。 - `$16-$23 (s0-s7)`:保存寄存器,用于在子程序调用期间保留重要数据。 - `$24-$25 (t8, t9)`:额外的临时寄存器,其中`t9`常被用作跳转目标地址。 - `$26-$27 (k0, k1)`:专用于操作系统或异常处理。 - `$28 (gp)`:全局指针,指向只读数据区。 - `$29 (sp)`:堆栈指针,指示当前堆栈顶部位置。 - `$30 (fp/s8)`:帧指针,用于管理函数调用时的堆栈框架。 - `$31 (ra)`:子程序返回地址,记录调用者的位置以便执行完成后返回。 #### 2. **特殊寄存器** 除了上述通用寄存器外,MIPS还定义了一些特殊的寄存器来支持更复杂的操作[^3]: - **PC(Program Counter)**:程序计数器,虽然不是通用寄存器的一部分,但它跟踪即将执行的下一条指令地址。 - **HI/LO 寄存器**:两个专门用于整数乘法和除法操作的结果存储区域。例如,乘法结果可能跨越多个字宽,因此需要这两个寄存器共同表示完整的数值。 #### 3. **浮点寄存器** 为了支持浮点运算,MIPS架构另外配备了32个浮点寄存器(FPR)。它们同样按照单精度或多精度模式工作,并遵循IEEE标准规范。 #### 4. **调试工具支持** 当开发者利用GDB这样的调试环境时,可以通过命令 `info registers` 查看所有可用寄存器的状态及其当前值[^4]。这有助于分析运行时行为并诊断潜在错误。 ```bash (gdb) info registers ``` 此命令会显示包括但不限于以下字段的信息: - 所有的通用寄存器状态; - 特殊目的寄存器PC 和 HI/LO 的内容; - 浮点单元相关配置详情; --- ### 示例代码展示如何查看寄存器内容 下面是一个简单的C语言片段配合对应的汇编翻译示例,演示了几个常见寄存器的作用方式: 假设我们有一个简单加法函数: ```c int add(int a, int b){ return a+b; } ``` 经过GCC编译后的部分汇编代码可能是这样形式呈现出来: ```asm add: addu $v0,$a0,$a1 # 将$a0(a)+$a1(b),并将结果放入$v0(return value) jr $ra # 跳回至调用处($ra contains caller address) ``` 这里可以看到输入参数分别位于$a0与$a1之中,而最终相加之成果则储存在$v0准备反馈回去给呼叫方。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值