asm - 关于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:
不是很懂为什么need to increment %hi(addr) to compensate。。。
因为 %lo(addr)
是一个有符号16位整数,也就是说bit#15是符号位,因此 0x8000
是一个负数。
在做相加计算时,其符号位扩展到高16bit,也就变成 0xFFFF8000
, 要想得到原来的地址 0x10008000
,%hi(addr)
必须加1, 0x1001.0000 + 0xFFFF.8000 = 0x1000.8000
.
把 lw
、sw
指令的 offset
字段设为有符号整数的好处是可以寻址寄存器前后两个方向的地址,如果设计成无符号整数就只能寻址大于等于寄存器值的地址范围了。
这篇关于asm - 关于MIPS中%hi()与%lo()的一点疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!