Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Develop an assembler that translates programs written in RISC - V assembly language into the binary code understood by the processor. You have been provided
Develop an assembler that translates programs written in RISCV assembly language into the binary code
understood by the processor.
You have been provided with starter code in C for the assembler which take the RISCV assemblylanguage
file as a commandline argument. If the input file name is trace usage is as follows:
$assembler trace
The trace file consists of text lines, each representing a RISCV assembly instruction. Your assembler must
translate each of these instructions into machine code and store them in instruction memory. Use the skeleton
code provided to you as a starting point for your solution. In particular, an example of how to assemble an
Rtype instruction is provided within the parse R type function in parser.c
Testing Your Assembler
Test the correctness of the machine code generated by your assembler using the provided trace file which
contains the following instruction classes: Rtype, Itype, and SBtype.
add x x x
ld xx
addi x x
slli x x
bne x x
First, translate these instructions into their corresponding binary representation by hand. Then, run the file
through your assembler implementation to compare the results and verify correctness.
Code for the Parser.c file to be edited is
#include "parser.h
FIXME: add support to identify and parse Itype and SBtype instructions
void loadinstructionsinstructionmemoryt imem, const char trace
printfLoading trace file: s
trace;
FILE fd fopentracer;
if fd NULL
perrorCannot open trace file.
;
exitEXITFAILURE;
Iterate over all the assembly instructions
char line NULL;
sizet len ;
ssizet read;
addrt PC ; program counter points to the zeroth location initially.
int IMEMindex ;
while read getline&line, &len, fd
Assign program counter
imeminstructionsIMEMindexaddr PC;
Extract operation or opcode from the assembly instruction
char rawinstr strtokline;
if strcmprawinstr, "add"
strcmprawinstr, "sub"
strcmprawinstr, sll
strcmprawinstr, srl
strcmprawinstr, "xor"
strcmprawinstr, or
strcmprawinstr, "and"
parseRtyperawinstr, &imeminstructionsIMEMindex;
imemlast &imeminstructionsIMEMindex;
IMEMindex;
PC ;
fclosefd;
Parse and assemble Rtype instruction
void parseRtypechar opr instructiont instr
instrinstruction ;
unsigned opcode ;
unsigned funct;
unsigned funct;
if strcmpopr "add"
opcode ;
funct;
funct;
char reg strtokNULL;
unsigned rd getregisternumberreg;
reg strtokNULL;
unsigned rs getregisternumberreg;
reg strtokNULL;
regstrlenreg;
unsigned rs getregisternumberreg;
Print the tokens
printfOpcode: u
opcode;
printffunct: u
funct;
printffunct: u
funct;
printfSource register : u
rs;
printfSource register : u
rs;
printfDestination register: u
rd;
Contruct instruction
instrinstruction opcode;
instrinstruction rd ;
instrinstruction funct;
instrinstruction rs;
instrinstruction rs;
instrinstruction funct;
FIXME: parse and assemble Itype instruction
void parseItypechar opr instructiont instr
FIXME: parse and assemble SBtype instruction
void parseSBtypechar opr instructiont instr
unsigned int getregisternumberchar reg
unsigned i ;
for i; i NUMOFREGS; i
if strcmpREGISTERNAMEi reg
break;
return i;
Code for main.c is
#include
#include
#include "parser.h
int mainint argc, char argv
if argc
printfUsage: s s
argv;
exitEXITSUCCESS;
Load instructions from the trace file into memory
instructionmemoryt instrmem;
instrmem.last NULL;
loadinstructions&instrmem, argv;
Print instructions in binary format
unsigned PC ;
while
instructiont instr &instrmem.instructionsPC ;
printf
Instruction at PC: u
PC;
unsigned mask ;
for int i ; i ; i
if instrinstruction & mask
printf;
else
printf
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started