Disassembler
Macros | Functions
Disassembler.cpp File Reference
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cmath>

Macros

#define size_of_instruction_hex   8
 

Functions

string convert_to_binary (char num)
 This function converts given character to its Binary format.
 
int binary_to_decimal (int token, string binary)
 Converts the given Binary value to Decimal value.
 
string sub_string (string parent, int start, int end)
 This function is used extract substring from string.
 
string R_format (const string &stringBinary)
 If the given machine code represents RISC-V R-format instruction this function generates RISC-V instruction from the machine code.
 
string I_format_immediate (const string &stringBinary)
 If the given machine code represents RISC-V I-format instruction this function generates RISC-V instruction from the machine code.
 
string I_format_load (const string &stringBinary)
 If the given machine code represents RISC-V I-format load instruction type this function generates RISC-V instruction from the machine code.
 
string S_format (const string &stringBinary)
 If the given machine code represents RISC-V S-format this function generates RISC-V instruction from the machine code.
 
string B_format (string stringBinary, int inst_no, int total_instructions)
 If the given machine code represents RISC-V B-format this function generates RISC-V instruction from the machine code.
 
string jal (string stringBinary, int inst_no, int total_instructions)
 If the given machine code represents RISC-V jal format this function generates RISC-V instruction from the machine code.
 
string jalr (const string &stringBinary)
 If the given machine code represents RISC-V jalr format this function generates RISC-V instruction from the machine code.
 
string lui (const string &stringBinary)
 If the given machine code represents RISC-V lui format this function generates RISC-V instruction from the machine code.
 
string disassembler (int total_instructions, string instruction, int inst_no)
 This function conversion of given machine code to binary value is done and based on opcode the corresponding instruction type is found and is processed accordingly.
 
void program ()
 This function reads machine code and each machine code is sent to disassembler() for further processing.
 
int main ()
 Main function where program execution starts.
 

Detailed Description

This file contains the code for Disassembler of RISC-V machine code

Author
Anudeep Rao Perala

Function Documentation

◆ B_format()

string B_format ( string  stringBinary,
int  inst_no,
int  total_instructions 
)

If the given machine code represents RISC-V B-format this function generates RISC-V instruction from the machine code.

B-format instructions are: beq, bne, blt, bge, bltu, bgeu

Parameters
stringBinaryRISC-V instruction machine code from which the RISCV instruction is generated
Returns
RISC-V instruction

◆ binary_to_decimal()

int binary_to_decimal ( int  token,
string  binary 
)

Converts the given Binary value to Decimal value.

Parameters
tokenThis is used to specify if the given binary value is in it's signed/ unsigned format
binaryBinary value
Returns
Decimal value

◆ convert_to_binary()

string convert_to_binary ( char  num)

This function converts given character to its Binary format.

Parameters
numInput
Returns
This function returns binary number format of the input character for concatenation

◆ disassembler()

string disassembler ( int  total_instructions,
string  instruction,
int  inst_no 
)

This function conversion of given machine code to binary value is done and based on opcode the corresponding instruction type is found and is processed accordingly.

Parameters
total_instructionsTotal number of instructions given as input
instructionThis is machine code of the RISC-V instruction
inst_noStates the instruction number
Returns
Disassembled RISC-V instruction

◆ I_format_immediate()

string I_format_immediate ( const string &  stringBinary)

If the given machine code represents RISC-V I-format instruction this function generates RISC-V instruction from the machine code.

I-format instructions are: addi, xori, ori, andi, slli, srli, srai

Parameters
stringBinaryRISC-V instruction machine code from which the RISCV instruction is generated
Returns
RISC-V instruction

◆ I_format_load()

string I_format_load ( const string &  stringBinary)

If the given machine code represents RISC-V I-format load instruction type this function generates RISC-V instruction from the machine code.

I-format load instructions are: ld, lh, lw, lbu, lhu, lwu

Parameters
stringBinaryRISC-V instruction machine code from which the RISCV instruction is generated
Returns
RISC-V instruction

◆ jal()

string jal ( string  stringBinary,
int  inst_no,
int  total_instructions 
)

If the given machine code represents RISC-V jal format this function generates RISC-V instruction from the machine code.

Parameters
stringBinaryRISC-V instruction machine code from which the RISCV instruction is generated
Returns
jal RISC-V instruction

◆ jalr()

string jalr ( const string &  stringBinary)

If the given machine code represents RISC-V jalr format this function generates RISC-V instruction from the machine code.

Parameters
stringBinaryRISC-V instruction machine code from which the RISCV instruction is generated
Returns
jalr RISC-V instruction

◆ lui()

string lui ( const string &  stringBinary)

If the given machine code represents RISC-V lui format this function generates RISC-V instruction from the machine code.

Parameters
stringBinaryRISC-V instruction machine code from which the RISCV instruction is generated
Returns
lui RISC-V instruction

◆ main()

int main ( )

Main function where program execution starts.

Returns
0

◆ program()

void program ( )

This function reads machine code and each machine code is sent to disassembler() for further processing.

At a time single machine code is processed and corresponding RISC-V instruction is generated, labelling of jump operations is taken care in this function

Returns

◆ R_format()

string R_format ( const string &  stringBinary)

If the given machine code represents RISC-V R-format instruction this function generates RISC-V instruction from the machine code.

R-format instructions are: add, sub, xor, or, and, sll, srl, sra

Parameters
stringBinaryRISC-V instruction machine code from which the RISCV instruction is generated
Returns
RISC-V instruction

◆ S_format()

string S_format ( const string &  stringBinary)

If the given machine code represents RISC-V S-format this function generates RISC-V instruction from the machine code.

S-format instructions are: sb, sh, sw, sd

Parameters
stringBinaryRISC-V instruction machine code from which the RISCV instruction is generated
Returns
RISC-V instruction

◆ sub_string()

string sub_string ( string  parent,
int  start,
int  end 
)

This function is used extract substring from string.

Parameters
parentParent string from which substring is generated
startStarting index of element in parent string
endEnd index of element in parent string
Returns
Substring from parent string