Offset address for JAL and JALR instrctions in RISC-V

寵の児 提交于 2021-02-07 13:19:32

问题


In the RISC-V specification, it is written that the immediates in JAL and JALR instructions are converted to jump offsets as :

  1. Sign extend the given immediate to XLEN bits.

  2. Set the LSB to zero.

I have a couple of questions regarding this.

QUESTION 1

For JAL, this gives a range :

000000000000 to 111111111110

that is, 4KiB.

Here, if the LSB is going to have to be zero always, why isn't the immediate just considered as the 12 bits before a mandatory zero LSB for the address, hence increasing the range of addresses to:

[000000000000]0 to [111111111111]0      

[ ] represents the given immediate offset, and a zero is added to the end of a given immediate offset internally. That is,

  1. Left shift give address by a bit.

  2. Sign extend the result to XLEN bits.

QUESTION 2

How are positive and negative offsets distinguished from one another? Is the MSB of the given offset used?


回答1:


JAL has a 20 bit offset and a register as operands.

Its operation is pc := pc + sxt ( imm20 << 1 ).

As you can see by the formula, the branch is pc-relative.  The immediate can reach +/- 1 MB from the JAL itself.  The immediate is shifted by one bit, the true LSB is always zero, so is not encoded.

Because RISC V supports instructions in multiples of 16-bits (two bytes), we cannot assume the next-to-LSB is also zero, as it would be with MIPS (which has 32-bit instructions).

The register operand in JAL is optionally used to capture the return address in addition to performing the branch.

JAL's function is to perform modestly far pc-relative branches or calls using its 20-bit range.  (Contrast with RISC V conditional branch instructions that have only have 12 bits for +/- 4 KB range.)


JALR has a 12 bit offset and two registers as operands.

Its operation is pc := ( rs1 + sxt ( imm12 ) ) & -2.

As you can see by the formula, the branch is register indirect, relative to the value in rs1.

Like JAL, JALR can also capture the the return address.

JALR is used to return from a function (aka RET in assembly.  In this form $ra is used as the source register, and no return address is captured).  This uses zero for the offset (i.e. an offset is not required).

JALR is also used to perform indirect function calls: calls via function pointer, virtual method dispatches, etc..   These use also use zero for the offset.

JALR can also be used in sequence with AUIPC.


AUIPC has a 20 bit offset and a register as operands.

Its operation is rd := pc + ( imm20 << 12 ).

It computes the upper part of a pc-relative immediate (while also providing a lower part of the pc that is not relative).

Combined with JALR, this can accomplish a 32-bit pc-relative branch or call.

AUIPC r5, labelFarAway      # AUIPC encodes upper 20 bits of label's distance from pc
JALR r5, $ra, labelFarAway  # JALR encodes the lower 12 bits of same


来源:https://stackoverflow.com/questions/59150608/offset-address-for-jal-and-jalr-instrctions-in-risc-v

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!