【问题标题】:Branch riscv instruction offset calculation on my emulator我的模拟器上的分支 riscv 指令偏移量计算
【发布时间】: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


    【解决方案1】:

    你很接近,但有几点:

    1. second 是 4:1 位,但您由于未能移动 1 位而将它们应用于 3:0。
    2. fourth 是第 12 位,也是一个符号位,必须进行符号扩展。

      否则,只需重新检查您的班次 [m:n] 必须 << n。所以,

      • [4:1] << 1
      • [11] << 11
      • [10:5] << 5
      • [12] << 12 — 但带有符号扩展名 — 所以在 32 位机器上,[12] << 31,则视为有符号 >> 20,或者使用 [12]? 0xFFFFF000 : 0

    【讨论】:

      猜你喜欢
      • 2019-12-18
      • 2018-08-03
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 2021-10-14
      • 2012-04-16
      • 2013-08-02
      • 2011-04-01
      相关资源
      最近更新 更多