【发布时间】:2023-01-08 22:58:43
【问题描述】:
我写了一个 riscv64 模拟器,但我对分支指令(尤其是 bge)的偏移量计算有疑问。
对我来说,计算满足条件时添加到 pc 的偏移量的公式是:PC = PC + IMM; 立即是从指令中提取的: 有我的 C 代码:
void BNE(unsigned long instr) {
unsigned long IMM1 = instr >>7 & 0b11111;
unsigned long IMM2 = instr >> 25 & 0b1111111;
unsigned long first = IMM1 & 0b1; // 11 eme bits IMM\[11\]
unsigned long second = IMM1 >> 1 & 0b1111; // IMM\[4:1\]
unsigned long third = IMM2 & 0b111111; //imm\[10:5\]
unsigned long fourth = IMM2 >> 6 & 0b1; // IMM\[12\]
// after extract assemble :
unsigned long imm = second | third << 5 | first <<7 | fourth << 9; // \<- I think here is the problem
}
当程序得到这个简单的程序代码时:
// I have simplified the code :
00000000000100e8 \<init\>:
10130: fd843783 ld a5,-40(s0)
1018c: fae7d2e3 bge a5,a4,10130 \<init+0x48\> # note that the condition is always met on my code
我得到:0x7a2。
电脑地址是:0x1018c
当我将 0x1018c 添加到 0x7a2 时,我得到:0x1092E 有一个问题,但我不知道在哪里。我认为 imm 变量的摘录有一些问题。
我试图修改 imm 变量位移位和掩码。但我没有任何运气。
我试过这个:
unsigned long imm = second | third << 4 | first << 5 | fourth << 6; // output wrong data
unsigned long imm = second | third << 6 | first << 11 | fourth << 12; // output the same data as imm
unsigned long imm = second | third << 5 | first << 7 | fourth << 9; // actual version
unsigned long imm = second | third << 6 | first << 7 | fourth <<8; // output the same data as imm
【问题讨论】:
标签: assembly riscv instruction-set