/*------------------------ * obj2bin.c * * Convert LC-3 load objects (.obj files) to plain text, * binary representation (ascii .bin files). * USAGE: * obj2bin < foo.obj > foo.bin *------------------------*/ #include #define WORDSIZE 16 #define BYTESIZE 8 int main(void) { int firstByte, secondByte; int i; char highOrderChars[BYTESIZE]; char lowOrderChars[BYTESIZE]; while (1) { /*-- Get next word.--*/ firstByte = getchar(); secondByte = getchar(); if (ferror(stdin)) { fprintf(stderr, "obj2bin: ERROR reading input. Giving up.\n"); return(1); } else if (feof(stdin)) { return(0); } /*-- Process one 16-bit word.------------- *-- The following is for big-Endian: *-- High-order byte comes first. *----------------------------------------*/ /*---- Convert high-order bits to ascii, lowest-order bit first. --*/ for (i=0; i> 1; } /*---- Convert low-order bits to ascii, lowest-order bit first. --*/ for (i=0; i> 1; } /*---- print high-order bits, left-to-right, highest bit first. --*/ for (i=(BYTESIZE-1); i>=0; i--) { printf("%c", highOrderChars[i]); } /*---- print low-order bits, left-to-right, highest bit first. --*/ for (i=(BYTESIZE-1); i>=0; i--) { printf("%c", lowOrderChars[i]); } printf("\n"); } /*end while*/ }