#include
#include
#include
#include
Copyright By PowCoder代写 加微信 powcoder
#include “emulator.h”
#define XSTR(x) STR(x) //can be used for MAX_ARG_LEN in sscanf
#define STR(x) #x
#define ADDR_TEXT 0x00400000 //where the .text area starts in which the program lives
#define TEXT_POS(a) ((a==ADDR_TEXT)?(0):(a – ADDR_TEXT)/4) //can be used to access text[]
#define ADDR_POS(j) (j*4 + ADDR_TEXT) //convert text index to address
const char *register_str[] = {“$zero”,
“$at”, “$v0”, “$v1”,
“$a0”, “$a1”, “$a2”, “$a3”,
“$t0”, “$t1”, “$t2”, “$t3”, “$t4”, “$t5”, “$t6”, “$t7”,
“$s0”, “$s1”, “$s2”, “$s3”, “$s4”, “$s5”, “$s6”, “$s7”,
“$t8”, “$t9”,
“$k0”, “$k1”,
“$sp”, “$fp”, “$ra”};
/* Space for the assembler program */
char prog[MAX_PROG_LEN][MAX_LINE_LEN];
int prog_len = 0;
/* Elements for running the emulator */
unsigned int registers[MAX_REGISTER] = {0}; // the registers
unsigned int pc = 0; // the program counter
unsigned int text[MAX_PROG_LEN] = {0}; // the text memory with our instructions
/* function to create bytecode for instruction nop
conversion result is passed in bytecode
function always returns 0 (conversion OK) */
typedef int (*opcode_function)(unsigned int, unsigned int*, char*, char*, char*, char*);
int add_imi(unsigned int *bytecode, int imi){
if (imi<-32768 || imi>32767) return (-1);
*bytecode|= (0xFFFF & imi);
return(0);
int add_sht(unsigned int *bytecode, int sht){
if (sht<0 || sht>31) return(-1);
*bytecode|= (0x1F & sht) << 6;
return(0);
int add_reg(unsigned int *bytecode, char *reg, int pos){
for(i=0;i
int add_lbl(unsigned int offset, unsigned int *bytecode, char *label){
char l[MAX_ARG_LEN+1];
while(j