1

It's my first time to ask questions on stackoverflow and I have done some searching about this question. And if there is something I missed, please let me know.

I want to ask how do hardware recognize RVC instructions.

For example, I fetch a 32-bit instruction and want to decide whether the first 16-bit is a RVC instruction. My question is about LUI instruction. LUI begins with 20-bit immediate. Since immediate can be any number, may the first 16-bit immediate be a RVC instruction?

How can I know the first 16-bit is a RVC instruction or just a part of imm/func/.. or something?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
孙志博
  • 13
  • 4
  • Does this answer your question? [How does RISC-V variable length of instruction work in detail?](https://stackoverflow.com/questions/56874101/how-does-risc-v-variable-length-of-instruction-work-in-detail) – Erik Eidt Nov 03 '21 at 15:28
  • @ErikEidt: Is this a duplicate? You answered this after close voting (or just commenting), and you answered the linked question. I can dup-hammer if you think it's appropriate. – Peter Cordes Apr 12 '22 at 01:58
  • @PeterCordes, I thought that link might help, but then realized the the OP was looking at things backwards, so gave an answer specific to that. – Erik Eidt Apr 12 '22 at 02:13

1 Answers1

4

I fetch a 32-bit instruction and want to decide whether the first 16-bit is a RCV instruction. My question is about LUI instruction. LUI begins with 20-bit immediate. Since immediate can be any number, may the first 16-bit immediate be a RVC instruction?

You're looking at the encoding from most significant bits down to least, but for RISC V, that is backwards.  From an instruction set perspective, LUI "begins" from the least significant bit and ends with the most significant bit.  Bit numbering on RISC V has 0 for the LSB and 31 for the MSB (when 32 bits).

The RISC V instruction set is defined from a little endian perspective, so the lowest numbered byte in the instruction encodes the size of the instruction with a fairly simple scheme.  Generally the first (lowest order) two bits specify either a 32-bit or 16-bit instruction, but other sizes like 48 and 64 and larger, are possible, and use additional bits.

In two bits, we can encode 4 different values: one is used for 32-bit instructions, two are used for 16-bit instructions (they're short so they need more encodings), and one for neither, which is used for the 48 bit or larger encodings (which then use more bits to indicate the actual length).

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53