关于MIPS中%hi()与%lo()的一点疑问

在看《See MIPS Run(2nd Edition)》的Chapter 9 Reading MIPS Assembly Language中的9.4 Addressing Modes中,碰见这样一段话:

The constructs %hi() and %lo() represent the high and low 16 bits of the address. This is not quite the straightforward division into low and high halfwords that it looks, because the 16-bit offset field of an lw is interpreted as signed. So if the addr value is such that bit 15 is a 1, then the %lo(addr) value will act as negative, and we need to increment %hi(addr) to compensate: 图片描述

图片版的原文在此:

图片描述

文中说:

because the 16-bit offset field of an lw is interpreted as signed. So if the addr value is such that bit 15 is a 1, then the %lo(addr) valuwill act as negative, and we need to increment %hi(addr) to compensate:clipboard.png

不是很懂为什么“need to increment %hi(addr) to compensate”。。。

阅读 7.8k
1 个回答

因为 %lo(addr) 是一个有符号16位整数,也就是说bit#15是符号位,因此 0x8000 是一个负数。
在做相加计算时,其符号位扩展到高16bit,也就变成 0xFFFF8000, 要想得到原来的地址 0x10008000
%hi(addr) 必须加1, 0x1001.0000 + 0xFFFF.8000 = 0x1000.8000 .
lwsw 指令的 offset 字段设为有符号整数的好处是可以寻址寄存器前后两个方向的地址,如果设计成无符号整数就只能寻址大于等于寄存器值的地址范围了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进