#include #include #include #include #include #include using namespace std; #define LABEL_SIZE 28 // our opcodes are nicely incremental enum OPCODES { ADD_OPCODE, SUB_OPCODE, AND_OPCODE, OR_OPCODE,...

I have a pdf of the question.I have a sample file as well


#include #include #include #include #include #include using namespace std; #define LABEL_SIZE 28 // our opcodes are nicely incremental enum OPCODES { ADD_OPCODE, SUB_OPCODE, AND_OPCODE, OR_OPCODE, XOR_OPCODE, MOVE_OPCODE, SHIFT_OPCODE, BRANCH_OPCODE }; // used to store label/address pairs so we can calculate branch addresses struct BRANCH_POINT { // no need for 16 bits since we can't go more than 6 bits for an offset unsigned char address; char label[LABEL_SIZE]; }; typedef struct BRANCH_POINT BranchPoint; // constants for our processor definition #define WORD_SIZE 2 #define DATA_SIZE 1024*WORD_SIZE #define CODE_SIZE 1024*WORD_SIZE #define REGISTERS 16 // number of bytes to print on a line #define LINE_LENGTH 16 // checks the hex value to ensure it a printable ASCII character. If // it isn't, '.' is returned instead of itself char valid_ascii( unsigned char hex_value ) { if ( hex_value < 0x21="" ||="" hex_value=""> 0x7e ) hex_value = '.'; return (char)hex_value; } // takes the data and puts it into a file void create_object_file( char *filename, unsigned char *data, int length ) { FILE *object_file = NULL; char object_filename[strlen(filename)]; // assumes that we have .asm at the end of each file name strncpy( object_filename, filename, strlen(filename)-3 ); object_filename[strlen(filename)-3] = 'o'; object_filename[strlen(filename)-2] = '\0'; // create the new file // doing RAW C here so I can do a straight binary write to the file object_file = fopen( object_filename, "w+" ); // spit it out if we have a file to write to if ( object_file ) { fwrite( data, 1, length, object_file ); fclose( object_file ); } } // takes the data and prints it out in hexadecimal and ASCII form void print_formatted_data( unsigned char *data, int length ) { int i, j, k; char the_text[LINE_LENGTH+1]; // print each line 1 at a time for ( i=0 ; i= length ) { for ( k=j ; k<= xor_opcode="" )="" {="" need="" to="" indicate="" if="" the="" 2nd="" operand="" is="" a="" register="" if="" (="" operand2[0]="=" 'r'="" )="" type="0x01;" }="" else="" if="" (="" opcode="=" move_opcode="" )="" {="" need="" to="" specify="" addressing="" mode="" handle="" destination="" being="" a="" memory="" locatoin="" if="" (="" operand1[0]="=" '['="" )="" {="" type="0x04;" a="" register="" for="" a="" source="" is="" fine="" if="" (="" operand2[0]="=" 'r'="" )="" type="" |="0x01;" but="" another="" memory="" location="" isn't="" else="" if="" (="" operand2[0]="=" '['="" )="" type="" |="0x02;" }="" it's="" a="" register="" else="" if="" (="" operand1[0]="=" 'r'="" )="" {="" memory="" for="" a="" source="" is="" fine="" if="" (="" operand2[0]="=" '['="" )="" type="" |="0x01;" but="" another="" register="" isn't="" else="" if="" (="" operand2[0]="=" 'r'="" )="" type="" |="0x02;" }="" it="" must="" be="" a="" literal,="" which="" isn't="" allowed="" else="" {="" type="" |="0x02;" }="" }="" else="" if="" (="" opcode="=" shift_opcode="" )="" {="" indicate="" left="" or="" right="" shifting="" if="" (="" operation[2]="=" 'l'="" )="" type="0x01;" }="" else="" if="" (="" opcode="=" branch_opcode="" )="" {="" identify="" the="" branch="" based="" on="" the="" instruction="" if="" (="" strcmp(="" operation,="" "beq"="" )="=" 0="" )="" type="0x01;" else="" if="" (="" strcmp(="" operation,="" "bne"="" )="=" 0="" )="" type="0x02;" else="" if="" (="" strcmp(="" operation,="" "blt"="" )="=" 0="" )="" type="0x03;" else="" if="" (="" strcmp(="" operation,="" "bgt"="" )="=" 0="" )="" type="0x04;" else="" if="" (="" strcmp(="" operation,="" "ble"="" )="=" 0="" )="" type="0x05;" else="" if="" (="" strcmp(="" operation,="" "bge"="" )="=" 0="" )="" type="0x06;" else="" if="" (="" strcmp(="" operation,="" "bge"="" )="=" 0="" )="" type="0x06;" else="" if="" (="" strcmp(="" operation,="" "bge"="" )="=" 0="" )="" type="0x06;" }="" return="" type;="" }="" extracts="" the="" register="" value="" from="" an="" operand="" unsigned="" char="" get_register(="" char="" *operand="" )="" {="" int="" reg;="" if="" (="" operand[0]="=" 'r'="" )="" sscanf(="" operand,="" "r%d",="" ®="" );="" else="" sscanf(="" operand,="" "[r%d]",="" ®);="" return="" (unsigned="" char)reg;="" }="" goes="" through="" each="" branch="" and="" finds="" the="" appropriate="" label.="" adjusts="" the="" last="" 6="" bits="" of="" the="" instruction="" at="" the="" branch="" location="" with="" the="" address="" of="" the="" found="" label.="" void="" fix_branches(="" unsigned="" char="" *machine_code,=""> &labels, vector &branches ) { int i,j; bool found = false; char offset; unsigned char *instr; // do each branch 1 at a time for ( i=0 ; i<(int)branches.size() ;="" i++="" )="" {="" simple="" linear="" search="" for="" the="" label="" found="false;" for="" (="" j="0" ;=""><(int)labels.size() && !found ; j++ ) &&="" !found="" ;="" j++="">
Feb 09, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here