简体   繁体   English

RISC-V 分支偏移机器指令编码

[英]RISC-V branch offset machine instruction encoding

What's the decoded RISC-V assembly instruction of: 0001100 01010 11100 100 10001 1100011 ?解码后的RISC-V汇编指令是什么:0001100 01010 11100 100 10001 1100011? From the specification i know, that the opcode is the BLT instruction and rs1 = x28, rs2 = x10.规范中我知道,操作码是 BLT 指令和 rs1 = x28,rs2 = x10。

But what is the encoded offset?但是编码的偏移量是什么? imm[12|10:5] is 0001100 = 12 and imm[4:1|11] is 10001 = -8, right? imm[12|10:5]是 0001100 = 12 而imm[4:1|11]是 10001 = -8,对吧? Where will the jump go?跳跃会去哪里?

The RISC-V Instruction Set Manual lists the complete instruction set in chapter 19. The opcode (the 7 least significant bits) tells us that we deal with a B-type instruction. RISC-V 指令集手册在第 19 章列出了完整的指令集。 opcode (7 个最低有效位)告诉我们我们处理的是 B 类指令。 The funct3 bits ( [14:12] ) specify the BLT instruction. funct3位 ( [14:12] ) 指定BLT指令。

The BLT instruction is encoded as follows: BLT指令编码如下:

RISC-V 指令集 BLT 指令

This gives us:这给了我们:

imm[12|10:5] imm[12|10:5] rs2 rs2 rs1 rs1 funct3功能3 imm[4:1|11] imm[4:1|11] opcode操作码
instruction操作说明 0001100 0001100 01010 01010 11100 11100 100 100 10001 10001 1100011 1100011
value价值 0xc 0xc 0xa (x10) 0xa (x10) 0x1c (x28) 0x1c (x28) 0x4 0x4 0x11 0x11 0x63 0x63

The immediate value is the concatenation of the instruction bits [31|7|30:25|11:8] : 0|1|001100|1000 = 0x4c8 .立即数是指令位[31|7|30:25|11:8]的串联: 0|1|001100|1000 = 0x4c8 Note that the immediate value misses the bit at index 0 .请注意,立即数会遗漏索引0处的位。

The RISC-V Instruction Set Manual states in 2.3 Immediate Encoding Variants : RISC-V 指令集手册2.3 立即编码变体中说明

There are a further two variants of the instruction formats (B/J) based on the handling of immediates, as shown in Figure 2.3.根据立即数的处理,还有另外两种指令格式 (B/J) 变体,如图 2.3 所示。 The only difference between the S and B formats is that the 12-bit immediate field is used to encode branch offsets in multiples of 2 in the B format. S 和 B 格式之间的唯一区别是 12 位立即数字段用于在 B 格式中以 2 的倍数对分支偏移进行编码。 Instead of shifting all bits in the instruction-encoded immediate left by one in hardware as is conventionally done, the middle bits (imm[10:1]) and sign bit stay in fixed positions, while the lowest bit in S format (inst[7]) encodes a high-order bit in B format.不是像传统那样在硬件中将指令编码的立即数中的所有位左移一,而是中间位 (imm[10:1]) 和符号位保持在固定位置,而 S 格式中的最低位 (inst[ 7]) 以 B 格式编码高位。

This is because RISC-V has a 16-bit instruction alignment constraint ( 1.2 Instruction Length Encoding ):这是因为 RISC-V 有一个 16 位指令对齐约束( 1.2 指令长度编码):

The base RISC-V ISA has fixed-length 32-bit instructions that must be naturally aligned on 32-bit boundaries.基本 RISC-V ISA 具有固定长度的 32 位指令,这些指令必须在 32 位边界上自然对齐。 However, the standard RISC-V encoding scheme is designed to support ISA extensions with variable-length instructions, where each instruction can be any number of 16-bit instruction parcels in length and parcels are naturally aligned on 16-bit boundaries.但是,标准 RISC-V 编码方案旨在支持具有可变长度指令的 ISA 扩展,其中每条指令可以是任意数量的 16 位指令包长度,并且包在 16 位边界上自然对齐。 The standard compressed ISA extension described in Chapter 12 reduces code size by providing compressed 16-bit instructions and relaxes the alignment constraints to allow all instructions (16 bit and 32 bit) to be aligned on any 16-bit boundary to improve code density.第 12 章中描述的标准压缩 ISA 扩展通过提供压缩的 16 位指令来减少代码大小,并放宽对齐约束以允许所有指令(16 位和 32 位)在任何 16 位边界上对齐以提高代码密度。

Ergo we need to add a trailing 0 to the offset, which gives us: 0|1|001100|1000|0 = 0x990 .因此,我们需要在偏移量中添加一个尾随0 ,这给我们: 0|1|001100|1000|0 = 0x990

The decoded instruction is: blt x28, x10, 0x990解码后的指令为: blt x28, x10, 0x990

.word 0x18ae48e3

blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello
hello:
blt x28,x19,hello
blt x28,x19,hello
blt x28,x19,hello



00000000 <hello-0x38>:
   0:   18ae48e3            blt x28,x10,990 <hello+0x958>
   4:   033e4a63            blt x28,x19,38 <hello>
   8:   033e4863            blt x28,x19,38 <hello>
   c:   033e4663            blt x28,x19,38 <hello>
  10:   033e4463            blt x28,x19,38 <hello>
  14:   033e4263            blt x28,x19,38 <hello>
  18:   033e4063            blt x28,x19,38 <hello>
  1c:   013e4e63            blt x28,x19,38 <hello>
  20:   013e4c63            blt x28,x19,38 <hello>
  24:   013e4a63            blt x28,x19,38 <hello>
  28:   013e4863            blt x28,x19,38 <hello>
  2c:   013e4663            blt x28,x19,38 <hello>
  30:   013e4463            blt x28,x19,38 <hello>
  34:   013e4263            blt x28,x19,38 <hello>

00000038 <hello>:
  38:   013e4063            blt x28,x19,38 <hello>
  3c:   ff3e4ee3            blt x28,x19,38 <hello>
  40:   ff3e4ce3            blt x28,x19,38 <hello>

00000001001111100100001001100011
00000001001111100100000001100011
11111111001111100100111011100011


0 000000 1001111100100 0010 0 1100011
0 000000 1001111100100 0000 0 1100011
1 111111 1001111100100 1110 1 1100011 

you do see how to extract these numbers from the instruction as documented yes?您确实看到如何从文档中提取这些数字,是吗?

0 0 000000 0010 0
0 0 000000 0000 0
1 1 111111 1110 0

0 0000 0000 0100 = 0x0004 
0 0000 0000 0000 = 0x0000 (<-- hello label here)
1 1111 1111 1100 = 0x1FFC

Okay so no magic here, sometimes there is an offset, often there is an offset.好吧,这里没有魔法,有时会有偏移,通常会有偏移。 Often the address of the next instruction is where the immediate is applied.通常下一条指令的地址是应用立即数的地方。 With ARM for example the address of two instructions ahead is where the offset is applied.以 ARM 为例,前面两条指令的地址是应用偏移量的地方。

from your instruction
0001100 01010 11100 100 10001 1100011
0 001100 01010 11100 100 1000 1 1100011
010011001000 (add implied zero for bit 0)
0 1001 1001 0000 = 0x990

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM