Write a C program that would read an MIPS machine code file stored in hexadecima
ID: 669451 • Letter: W
Question
Write a C program that would read an MIPS machine code file stored in hexadecimal (*.hex) format and executes the machine code. The C code also prints and writes the values of all the MIPS registers before and after execution.
Requirements:
1. Reads a MIPS machine code file stored in hexadecimal format. Assume there are no loops in the code
2. Executes the opcode in a sequential fashion
3. Supports the following instructions from the MIPS reference ISA
- add, addi, addiu, addu, sub, subu
4. Before executing the machine code, prints (console) and write to a file the values of all the MIPS registers
5. Once the machine code is completely executed, print (console) and write to a file the values of all the MIPS registers.
# A demonstration of some simple MIPS instructions
addi $t0, $t0, 5
addi $t1, $t1, -2
add $t2, $t0, $t1
sub $t2, $t2, $t1
# Hex
21080005
2129fffe
01095020
01495022
Explanation / Answer
CODE :
#ifdef _WIN32
#include<windows.h>
#else
#include<sys/man.h>
#include<unistd.h>
#endif
#include<stdio.h>
#include<string.h>
int main()
{
Char ocode[100];
FILE *fp;
If((fp=fopen(“mips.hex”,”r”))==NULL)
{
printf(“ Unable to open MIPS file”);
exit(1);
}
while(fgets(str,10,fp)!=NULL)
{
int code=atoi(str);
ocode[i++]=c;
}
fclose(fp);
#ifdef _WIN32
HANDLE memy_handle=CreatFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, 0, length, NULL);
#else
void* memry_map=mmap(NULL, sizeof(ocode));
((void(*)())memry_map)();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.