指令STMFD和LDMFD分析
根据ATPCS规则,我们一般使用FD(Full Descending)类型的数据栈(sp指向最后一个压入的值,数据栈由高地址向低地址生长)!所以经常使用的指令就有STMFD和LDMFD。
在STM、LDM指令中,寄存器列表与内存单元的对应关系:编号低的寄存器对应于内存中低地址单元;编号高的寄存器对应于内存中高地址单元。
通过ARM对于栈操作和批量Load/Store指令寻址方式,可以知道指令STMFD和LDMFD的地址计算方法:
STMFD指令的寻址方式为事前递减方式(DB)
而DB寻址方式实际内存地址为:
start_address = Rn - (Number_Of_Set_Bits_In(register_list)*4)
end_address = Rn - 4
STM指令操作的伪代码:
if ConditionPassed(cond) then
address = start_address
for i = 0 to 15
if register_list[i] == 1
Memory[address] = Ri
address = address + 4
有上面两个伪代码可以得出 STMFD SP!,{R0-R7,LR} 的伪代码如下:
SP = SP - 9×4;
address = SP;
for i = 0 to 7
Memory[address] = Ri;
address = address + 4;
Memory[address] = LR;
LDMFD指令的寻址方式为事后递增方式(IA)
IA内存的实际地址的伪代码
start_address = Rn
end_address = Rn + (Number_of_set_bits_in(register_list)*4) - 4
LDM指令操作的伪代码(未考虑PC寄存器):
if ConditionPassed(cond) then
address = start_address
for i = 0 to 15
if register_list[i] == 1
Ri = Memory[address,4]
address = address + 4
所以LDMFD SP!,{R0-R7,PC}^ (;恢复现场,异常处理返回)伪代码是:
address = SP;
for i = 0 to 7
Ri = Memory[address ,4]
address = address + 4;
SP = address;