/************************************************************************/
/* File Name : lc4_disassembler.c */
/* Purpose : This file implements the reverse assembler */
/* for LC4 assembly. It will be called by main() */
/* */
/* Author(s) : tjf and you */
/************************************************************************/
#include
#include “lc4_memory.h”
#include
#include
int reverse_assemble (row_of_memory* memory)
{
row_of_memory* opcode = NULL;
short unsigned int rd = 0;
short unsigned int rs = 0;
short unsigned int rt = 0;
short unsigned int subopcode = 0;
short unsigned int IMM5 = 0;
char* assembly = NULL;
do{
opcode = search_opcode(memory, 1);
if(opcode == NULL){
break;
};
//extract the relevant bits
rd = (opcode->contents>>9 & 0b0000111);
rs = (opcode->contents>>6 & 0b0000000111);
rt = (opcode->contents & 0b0000000000000111);
subopcode = (opcode->contents>>3 & 0b0000000000111);
IMM5 = (opcode->contents & 0b0000000000011111);
assembly = malloc(20* sizeof(char));
switch(subopcode){
case 0:
sprintf(assembly, “ADD R%d, R%d, R%d”, rd, rs, rt);
break;
case 1:
sprintf(assembly, “MUL R%d, R%d, R%d”, rd, rs, rt);
break;
case 2:
sprintf(assembly, “SUB R%d, R%d, R%d”, rd, rs, rt);
break;
case 3:
sprintf(assembly, “DIV R%d, R%d, R%d”, rd, rs, rt);
break;
default:
sprintf(assembly, “ADD R%d, R%d, #%d”, rd, rs, IMM5);
break;
}
opcode->assembly = assembly;
}
while(opcode != NULL);
return 0 ;
}