Skip to content

Commit 5fada71

Browse files
committed
Add RISC-V instruction encoding examples
1 parent a61d7c4 commit 5fada71

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

docs/instruction.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,73 @@ which generates the following assembler output as seen by `objdump`:
8787

8888
The `lui` (load-upper-immediate) instruction has a 20-bit immediate, so the other immediate-load instructions have only 12-bit immediates.
8989

90+
## Instruction Examples
91+
92+
### Encoding instruction `0x01e007ef`
93+
94+
First, convert hex into binary:
95+
96+
| hex | `0` | `1` | `e` | `0` | `0` | `7` | `e` | `f` |
97+
|:---:|:------:|:------:|:------:|:------:|:------:|:------:|:------:|:------:|
98+
| bin | `0000` | `0001` | `1110` | `0000` | `0000` | `0111` | `1110` | `1111` |
99+
100+
Instructions are better decoded reading them from right to left, so that the first thing you find are its quadrant and opcode.
101+
102+
| RV32I | `00000001111000000000` | `01111` | `11011` | `11` |
103+
|:-------:|:----------------------:|:---------:|:-------------:|:------------:|
104+
| Meaning | `<imm> (encoded)` | `rd` | `opcode` | `quadrant` |
105+
| Value | `<imm>` | 15 | Jump and Link | 4th |
106+
107+
We therefore obtain `jal x15 <imm>```, where `<imm>` is the still to be decoded immediate.
108+
109+
All the instruction fields except `<imm>` are decodable just from the tables, for this instruction. For `<imm>` we have to further scramble some bits to decode it.
110+
111+
Identify the subfields of the immediate:
112+
113+
| `<imm> (encoded)` | `0` | `0000001111` | `0` | `00000000` |
114+
|:-----------------:|:----:|:------------:|:----:|:----------:|
115+
| Encoding | `m2` | `imm2` | `m1` | `imm1` |
116+
117+
Re-order the subfields as written on the tables. Usually the immediate encoding is just below the corresponding instruction's encoding.
118+
119+
| Decoding di `<imm>` | `-m2-` | `imm1` | `m1` | `imm2` | `0` |
120+
|:-------------------:|:--------------:|:----------:|:----:|:------------:|:---:|
121+
| `<imm> (bin)` | `000000000000` | `00000000` | `0` | `0000001111` | `0` |
122+
123+
We obtain the 2's complement
124+
- `<imm> (bin)` = `00000000000000000000000000011110`
125+
126+
that is the decimal
127+
- `<imm> (dec)` = `30`
128+
129+
The complete disassembled instruction is therefore `jal x15 30`, or `jal a5 30` using the ABI register aliases.
130+
131+
### Deconding instruction `j -12`
132+
133+
The instruction `j`.
134+
135+
| Instruction Encoding `j` | `<imm> (encoded)` | 00000 | 11011 | 11 |
136+
|:------------------------:|:-----------------:|:-----:|:-----:|:--:|
137+
138+
We therefore know the least significant bits `xxxxxxxxxxxxxxxxxxxx000001101111`. The rest is just an immediate, `<imm> = 30 `, which we now have to encode.
139+
Then sign-extend it to cover all 32 bits of its width, converting `<imm> (bin)` = `11111111111111111110100` from `<imm> (bin)` = `-12` to `<imm> (bin)` = `10100`.
140+
141+
Divide this number into its subfields.
142+
143+
| `<imm> (bin)` | `111111111111` | `11111111` | `1` | `1111111010` | `0` |
144+
|:----------------:|:--------------:|:----------:|:----:|:------------:|:---:|
145+
| Decoding `<imm>` | `-m2-` | `imm1` | `m1` | `imm2` | `0` |
146+
147+
Re-order the fields to encode the immediate:
148+
149+
| Encoding | `m2` | `imm2` | `m1` | `imm1` |
150+
|:-----------------:|:----:|:------------:|:----:|:----------:|
151+
| `<imm> (encoded)` | `1` | `1111111010` | `1` | `11111111` |
152+
153+
We obtain `<imm> (encoded)` = `11111111010111111111xxxxxxxxxxxx`, that are the missing most significant bits.
154+
155+
The complete assembled instruction is therefore `11111111010111111111000001101111`, after having jointed the two half-results above.
156+
90157
## "RVC" compressed instructions
91158

92159
Chapter 12, p. 67 (79 of 145), explains a Thumb-2-like scheme, providing a

0 commit comments

Comments
 (0)