簡體   English   中英

RISC-V 匯編器正在用 bne + jal 替換 beq 指令

[英]RISC-V assembler is replacing beq instructions by bne + jal

我有這個 RISC-V 匯編程序:

addi        x2,x0,5
addi        x3,x0,12
addi        x7,x3,-9
or          x4,x7,x2
and         x5,x3,x4
add         x5,x5,x4
beq         x5,x7,0x48

我想獲得十六進制的匯編指令。 格式以將它們加載到 FPGA 中。 我通過執行以下命令獲得這些值:

#!/bin/bash

# Given a source assembly file, assembly it and display
# the hex values for each instruction

SRC_FILE=$1

RV_AS=riscv64-unknown-elf-as
RV_OBJCOPY=riscv64-unknown-elf-objcopy

$RV_AS -o /tmp/gen_asm_instr.elf $SRC_FILE -march=rv32ima
$RV_OBJCOPY -O binary /tmp/gen_asm_instr.elf /tmp/gen_asm_instr.bin
xxd -e -c 4 /tmp/gen_asm_instr.bin | cut -d ' ' -f 2

如果我注釋掉最后一條匯編指令( beq ),一切正常。 我得到以下結果:

00500113
00c00193
ff718393
0023e233
0041f2b3
004282b3

那是6條指令,一切都很好。 但是,如果我取消注釋最后一條指令,我會得到:

00500113
00c00193
ff718393
0023e233
0041f2b3
004282b3
00729463
0000006f

這是8條指令。 如果我“分解”上述內容,我會得到:

拆卸

# Create a file 'template.txt' with the above instructions:
00000000: 00500113 00c00193 ff718393 0023e233
00000010: 0041f2b3 004282b3 00729463 0000006f

# Use xxd and obdjump to recover the assembly instructions
xxd -r template.txt > a.out # generate a binary file
xxd -e a.out > a-big.txt    # switch endianness
xxd -r ./a-big.txt > a2.out # generate a bin. file with the switched endianness
riscv64-unknown-elf-objdump -M no-aliases -M numeric -mabi=ilp32 -b binary -m riscv -D ./a2.out    # dis-assemble it

結果

./a2.out:     file format binary


Disassembly of section .data:

0000000000000000 <.data>:
   0:   00500113            addi    x2,x0,5
   4:   00c00193            addi    x3,x0,12
   8:   ff718393            addi    x7,x3,-9
   c:   0023e233            or  x4,x7,x2
  10:   0041f2b3            and x5,x3,x4
  14:   004282b3            add x5,x5,x4
  18:   00729463            bne x5,x7,0x20
  1c:   0000006f            jal x0,0x1c

因此 RISC-V 匯編器將beq指令轉換為兩個: bnejal

為什么會發生這種情況? 我怎樣才能避免它?

編輯

我試過這個在線匯編器:

https://riscvasm.lucasteske.dev/

同樣的事情也會發生。

當使用硬編碼的數字地址作為分支目標時,構建系統似乎會這樣做。 無法解釋為什么它選擇這樣做,但我會注意到jalbeq (12 位立即數)具有更遠的范圍(20 位立即數)。 由於bxxjal都是 PC 相關的,因此都不支持絕對尋址。 匯編器可能不知道代碼的位置,因此為您提供了額外的范圍以到達該絕對地址。

如果您使用標簽作為分支目標,它不會在觸手可及的情況下這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM