#include #include #include #include #include #include "asm.h" #include "defines.h" #include "main.ext" #include "structs.h" #include "externs.h" #include "asm.pro" UWORD passtwo() { UBYTE more_input; UBYTE line[MAX_LINE]; UBYTE * line_ptr; UBYTE instruction_number; UBYTE * nl_loc; UBYTE error; UBYTE pseudo_status; UBYTE symbol; if( openfiles("passone.out", "passtwo.out") != ERROR) { // variable initializations instruction_number = 0; error = FALSE; more_input = TRUE; SymTabPtr = &SymTab[0]; InstructionPtr = &Instruction[0]; // read input file until end while( !feof(In_Ptr) && (more_input) && (!error) ) { memset(line, NULL, sizeof(line) ); fgets(line, sizeof(line),In_Ptr); // read in line from input file line_ptr = line; if( InstructionPtr->opcode != HLT) { InstructionPtr->relocate = TRUE; // set relocate bit if( strchr(line_ptr, COMMA) ) { // is this a 2 operand instruction ? if(get_reg_num(&line_ptr, &InstructionPtr->opcode) == ILLEGAL_REGISTER) //return(ERROR); error = TRUE; } else if(InstructionPtr->opcode != RET) // stop if RET instruction get_next_symbol(&line_ptr); if(InstructionPtr->opcode != RET) { if( (InstructionPtr->eff_add = search_symtab(line_ptr) ) == ERROR ) error = TRUE; } SymTabPtr++; // point to next entry in symbol table InstructionPtr++; // point to next instruction to be assembled } // end if ! HLT else more_input = FALSE; } } else error = TRUE; if(error) return(ERROR); else return(OK); } ///////////////////////////////////////////////////////////////////////////// UBYTE get_reg_num(UBYTE * * line, UWORD * reg_num_ptr) { UBYTE * ptr; // puls out register number and check to see if it is valid if( (ptr = strchr(*line, COMMA)) != NULL ) { *line = ptr; // point to comma ptr --; // point to register number if( ( (*ptr) >= R0) && ( (*ptr) <= R3) ) { *reg_num_ptr |= ( ( (*ptr) - ASCII_TO_DEC) & 0x3); // register number occupies bits 0 & 1 (*line)++; // point to second operand return(OK); } } return(ILLEGAL_REGISTER); } ///////////////////////////////////////////////////////////////////////////// UWORD search_symtab(UBYTE * operand2) { UWORD loop; UBYTE * loc; // looks to see if symbol is in Symbol Table if( (loc = strchr(operand2, NEWLINE)) != NULL) *loc = NULL; // null terminate for string comparisons for(loop=0; loop < MAX_INSTRUCTIONS; loop++) if( strcmp(SymTab[loop].symbol, operand2) == 0 ) return(SymTab[loop].lctr); return(ERROR); } ///////////////////////////////////////////////////////////////////////////// UWORD write_object_file() { UBYTE filename[MAX_FILENAME]; UBYTE * loc; // saves object module to disk files clrscr(); gotoxy(30,8); printf("\nEnter Object filename. (filename.obj) "); gets(filename); if ((Out_Ptr = fopen(filename, "wt")) == NULL) { printf("\nError Opening Output file %s", filename); pause(); return(ERROR); } // write out intstructions to object file Swap.w = OrgLoc; fprintf(Out_Ptr, "ORG=%d\r\n", OrgLoc); for(InstructionPtr = &Instruction[0]; InstructionPtr->lctr != ModuleEnd; InstructionPtr++) { // pseudo opcode ? if( InstructionPtr->pseudo == DS ) { fprintf( Out_Ptr, "%2.2X %2.2X %2.2X\r\n",(InstructionPtr->lctr + OrgLoc), InstructionPtr->instr_len, InstructionPtr->pseudo); } else { if( (InstructionPtr->pseudo >= ORG) && (InstructionPtr->pseudo <= END) ) { if(Instruction->relocate == TRUE ) { fprintf( Out_Ptr, "%2.2X %2.2X %2.2X %2.2X %2.2X\r\n",(InstructionPtr->lctr + OrgLoc), InstructionPtr->instr_len, InstructionPtr->pseudo, ( InstructionPtr->eff_add >> 8 ) , (InstructionPtr->eff_add & 0xFF) ); } else { fprintf( Out_Ptr, "%2.2X %2.2X %2.2X %2.2X %2.2X\r\n",InstructionPtr->lctr, InstructionPtr->instr_len, InstructionPtr->pseudo, (InstructionPtr->eff_add >> 8) , (InstructionPtr->eff_add & 0xFF) ); } } else { // must be a non-pseudo opcode if( InstructionPtr->instr_len == ONE_BYTE ) { fprintf( Out_Ptr, "%2.2X %2.2X %2.2X\r\n", (InstructionPtr->lctr + OrgLoc), InstructionPtr->instr_len, InstructionPtr->opcode); } else { fprintf( Out_Ptr, "%2.2X %2.2X %2.2X %2.2X %2.2X\r\n",(InstructionPtr->lctr + OrgLoc), InstructionPtr->instr_len,InstructionPtr->opcode,( (InstructionPtr->eff_add >> 8) + Swap.b[1]), ( (InstructionPtr->eff_add & 0xFF) + Swap.b[0]) ); } } } fflush(Out_Ptr); } fprintf(Out_Ptr, "SIZE=%d\r\n", ModuleEnd); fclose(Out_Ptr); return(OK); } /////////////////////////////////////////////////////////////////////////////