Decoding machine Language - muneeb-mbytes/computerArchitectureCourse GitHub Wiki
Decoding machine Language
Computer Organization and Design RISC-V edition
David A. Patterson, University of California, Berkeley
John L. Hennessy, Standford University
Page 247-249
Encoding involves assembler which translates assembly language to machine language,by encoding assembly instructions with their binary representation.
Decoding Machine Language involves disassembler which translates binary machine language into assembly language-the inverse operation to that of an assembler.
Sometimes it becomes a necessity to convert machine language to the original assembly language.
- Lets take an example of a machine instruction which is in hexadecimal format hex(00578833)
- The first step is converting hexadecimal to binary:
0000 0000 0101 0111 1000 1000 0011 0011
- To know how to interpret the bits, we need to determine the instruction format, and to do that we first need to determine the opcode. The opcode is the rightmost 7 bits, or 0110011(this indicates that the instruction format that is used here is the r-type).
- Thus, we can parse the binary format into fields listed in figure given below.
funct7 | rs2 | rs1 | funct7 | rd | opcode |
---|---|---|---|---|---|
0000000 | 00101 | 01111 | 000 | 10000 | 0110011 |
- We decode the rest of the instruction by looking at the field values.
- The funct7 and funct3 fields are both zero, indicating the instruction is add.
- The decimal values for the register operands are 5 for the rs2 field, 15 for rs1, and 16 for rd.
- These numbers represent registers x5, x15, and x16.
- Now we can reveal the assembly instruction: add x16, x15, x5 .
RISC-V instruction encoding
All instructions have an opcode field, and all formats except U-type and UJ-type use the funct3 field.
R-type instructions use the funct7 field, and immediate shifts (slli, srli, srai) use the funct6 field.