/************************************************************************/
/* File Name : lc4_loader.c */
/* Purpose : This file implements the loader (ld) from PennSim */
/* It will be called by main() */
/* */
/* Author(s) : tjf and you */
/************************************************************************/
#include
#include “lc4_memory.h”
#include
#include
/* declarations of functions that must defined in lc4_loader.c */
FILE* open_file(char* file_name)
{ char* obj_ptr;
//check if file_name is .obj filename
obj_ptr = strchr(file_name,’.’);
if(obj_ptr == NULL || strcmp(“.obj”, obj_ptr) != 0){
printf(“error1: usage: ./lc4
printf(” (not a .obj file)\n”);
return NULL;
}
// call to open file
FILE *file_ptr = fopen(file_name, “rb”);
// check if file ptr is NULL; if cannot open file, return error
if(file_ptr == NULL){
printf(“error1: usage: ./lc4
printf(” (could not open file)\n”);
return NULL;
}
return file_ptr ;
}
int parse_file (FILE* my_obj_file, row_of_memory** memory){
/*check for endianness*/
unsigned int i = 1;
char *c = (char*)&i;
unsigned int header, addr, wordnum, instr;
short int n;
row_of_memory* memPtr = *memory;
do{ // read header
int header_byte_one_read = fgetc(my_obj_file);
if(header_byte_one_read == EOF){
break;
}
int header_byte_two_read = fgetc(my_obj_file);
if(header_byte_two_read == EOF){
break;
}
if((int)*c == 0){ // big endian
header = (header_byte_one_read << 8) | header_byte_two_read;
} else { //small endian
header = (header_byte_two_read << 8) | header_byte_one_read;
}
// read address
int addr_byte_one_read = fgetc(my_obj_file);
int addr_byte_two_read = fgetc(my_obj_file);
if((int)*c == 0){ // big endian
addr = (addr_byte_one_read << 8) | addr_byte_two_read;
} else { //small endian
addr = (addr_byte_two_read << 8) | addr_byte_one_read;
}
// read num of word
int wordnum_byte_one_read = fgetc(my_obj_file);
int wordnum_byte_two_read = fgetc(my_obj_file);
if((int)*c == 0){ // big endian
wordnum = (wordnum_byte_one_read << 8) | wordnum_byte_two_read;
} else { //small endian
wordnum = (wordnum_byte_two_read << 8) | wordnum_byte_one_read;
}
if (header == 0XCADE || header == 0xDADA){
for (n = 0; n < wordnum; n++){
int instr_byte_one_read = fgetc(my_obj_file);
int instr_byte_two_read = fgetc(my_obj_file);
if((int)*c == 0){ // big endian
instr = (instr_byte_one_read << 8) | instr_byte_two_read;
} else { //small endian
instr = (instr_byte_two_read << 8) | instr_byte_one_read;
}
add_to_list(memory, addr + n, instr);
}
}
if (header == 0xC3B7){
char* symbol;
row_of_memory* labelSearch = search_address(*memory, addr);
if(labelSearch == NULL){
add_to_list(memory, addr, 0000);
}
labelSearch->label = malloc(sizeof(char) * (wordnum +1));
if((int)*c == 0){ // big endian
for(n =0; n < wordnum; n++){
symbol[n] = fgetc(my_obj_file);
} else { // small endian
symbol[n+1] = fgetc(my_obj_file);
symbol[n] = fgetc(my_obj_file);
n++;
}
}
symbol[wordnum] = '\0';
labelSearch->label = symbol;
}
} while(1);
int close = fclose(my_obj_file);
*memory = memPtr;
if (close != 0){
printf(“File failed to close. \n”);
delete_list(memory);
}
return 0 ;
}