转:终于知道mips中gp寄存器的用法了

 

http://www.imhan.com/archives/19/

费了好大的劲,终于知道gp寄存器的用法了。

首先说说gp寄存器出现的原因吧,由于RISC体系的CPU,每条指令都是32Bit,而地址总线也是32Bit,这样,就不可能通过一条指令来实现Memory的访问。

MIPS中,访问Memory的指令格式为:

lw r1, offset(r2)

其中,offset为16Bit,也就是说,最多能访问以r2为基地址,前后32K的空间。

于是这个时候,gp就闪亮登场了。

我们知道,GCC在编译过程,会将变量分别放在data,bss,common等段。而编译mips程序时,会将小变量放入sdata,sbss,scommon等段。这个大小由编译选项-G来决定。-G0则表示不使用小数据段。默认是-G8,表示小于或等于8 byte的数据将放入小变量区。

这样的话,在上电时给gp初始化一个值,那么,所有小变量区的变量就可以通过

lw r1, offset(gp)

来访问了。

那么,

 

from the man of gcc:

Code:
-G num
   Put global and static objects less than or equal to num bytes into the small
   data or bss sections instead of the normal data or bss sections.  The default
   value of num is 8.  The -msdata option must be set to one of sdata or use for
   this option to have any effect.

   All modules should be compiled with the same -G num value.  Compiling with dif‐
   ferent values of num may or may not work; if it doesn�t the linker will give an
   error message---incorrect code will not be generated.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值