Assembler Project main.c

...har Binary[WORD+1]; char Oct[OCT_STR]; char Name[2]; OpenFile->CurrentLine->Operands = NULL; while (*P != '') { ComputeBinary(*P,Binary); TranslateBinToOctal(Binary,Oct); Name[0] = *P; Name[1] = ''; AllocateOperand(Name,Binary,Oct,"-"); P= P + 1; OpenFile->Counter.DC+=1; } /* releasing pointers */ P = NULL; free(P); } /* breaking apart operands - string of data instruction and allocating operands */ void BrakingDataOpr() { char *P = OpenFile->CurrentLine->OperandStr; char *comma = OpenFile->CurrentLine->OperandStr; char Binary[WORD+1]; char Oct[OCT_STR]; int Len, Number; char *Str = malloc(LINE_LEN); comma = strchr(P,','); while ( comma != NULL ) { Len = comma - P; ReturnString(P,Len,&Str); /* translate string to integer */ Number = translate_dec(Str); ComputeBinary(Number,Binary); TranslateBinToOctal(Binary,Oct); AllocateOperand(Str,Binary,Oct,"-"); OpenFile->Counter.DC +=1; P = comma+1; comma = strchr(P,','); } /* search the last operand */ comma = P+ strlen(OpenFile->CurrentLine->OperandStr); Len = comma - P; ReturnString(P,Len,&Str); /* translate string to binary & Octal */ Number = translate_dec(Str); ComputeBinary(Number,Binary); TranslateBinToOctal(Binary,Oct); AllocateOperand(Str,Binary,Oct,"-"); OpenFile->Counter.DC +=1; /* releasing pointers */ P = NULL; comma = NULL; Str = NULL; free(P); free(comma); free(Str); } /* extract operands string from instruction line type */ void ExtractOperandsInstr() { char *Oper = malloc(LINE_LEN); char *LineEnd = NULL; int Len; IgnoreWhiteSpaces(); LineEnd = strchr(CodeLine,' '); if (LineEnd == NULL) { LineEnd = strchr(CodeLine,EOF); if (LineEnd == NULL) ErrMsg(20); } Len = LineEnd - CodeLine; ReturnString(CodeLine,Len,&Oper); OpenFile->CurrentLine->OperandStr = Oper; /* releasing pointers */ Oper = NULL; LineEnd = NULL; free(Oper); free(LineEnd); } /* extract operands - string from string data line type */ int ExtractOperandsString() { char *Oper = malloc(LINE_LEN); char *FQuote = NULL; char *EQuote = NULL; int Len; IgnoreWhiteSpaces(); /* Check for quotation marks */ FQuote = strchr(CodeLine,'"'); if (FQuote == NULL) { ErrMsg(24); CRITICAL_ERROR = 1; return 0; } EQuote = strrchr(CodeLine,'"'); if (EQuote == FQuote) /* if same quotation mark */ { ErrMsg(24); CRITICAL_ERROR = 1; return 0; } else { Len = EQuote - FQuote; ReturnString(FQuote + 1,Len - 1,&Oper); OpenFile->CurrentLine->OperandStr = Oper; } /* releasing pointers */ Oper = NULL; FQuote = NULL; EQuote = NULL; free(Oper); free(EQuote); free(FQuote); return 1; } /* extract from operation line operands - string */ void ExtractOperands() { char *Oper = malloc(LINE_LEN); char *EndOfLine = NULL; int Len; IgnoreWhiteSpaces(); EndOfLine = strchr(CodeLine,' '); /* Operands list goes to end of line */ if (EndOfLine == NULL) EndOfLine = strchr(CodeLine,''); /* Case last line in file */ if (EndOfLine == NULL) ErrMsg(20); else { Len = EndOfLine - CodeLine; ReturnString(CodeLine,Len,&Oper); OpenFile->CurrentLine->OperandStr = Oper; } /* releasing pointers */ Oper = NULL; EndOfLine = NULL; free(Oper); free(EndOfLine); } /******************************************************** * This section is mainly for Operation interpretation * ********************************************************/ void OperationCodeLine() { char *CmdName = malloc(CMD_LEN); char Dec[OCT_STR]; char Oct[OCT_STR]; char Bin[WORD+1]; int CmdStat; OpenFile->CurrentLine->Type = 'O'; strcpy(OpenFile->CurrentLine->A_R_E,"Absolute"); /* convert address to binary & octal */ Itoa(OpenFile->Counter.IC,Dec); ComputeBinary(OpenFile->Counter.IC,Bin); TranslateBinToOctal(Bin,Oct); strcpy(OpenFile->CurrentLine->DecAdd ,Dec); strcpy(OpenFile->CurrentLine->OctAdd ,Oct); FindLabel(CodeLine); OpenFile->Counter.IC +=1; ExtractCommand(&CmdName); CmdStat = CheckCmdName(CmdName); ExtractOperands(); if(CmdStat) BrakingOperationOpr(); /* releasing pointers */ CmdName = NULL; free(CmdName); } /* extracting the name of the command from the line */ void ExtractCommand(char **Cmd) { char *CmdEnd = NULL; int Len; IgnoreWhiteSpaces(); /* in case there was a label */ CmdEnd = CodeLine; while (!isspace(*CmdEnd)) /* if char is not 'empty' move forward */ CmdEnd++; Len = CmdEnd - CodeLine; if (Len > 1) /* only if there is a type */ { ReturnString(CodeLine,Len,&*Cmd); CodeLine = CmdEnd; } else printf("Error - Command is missing"); /* releasing pointers */ CmdEnd = NULL; free(CmdEnd); } /* Checking Command name. if Command is not found the command recives the binary code of 'mov' command and a NULL name */ int CheckCmdName(char *CmdName) { struct CmdStruct *P = Cmd; int Flag = 0; while ( P != NULL) { if (strcmp(P->Name, CmdName) == 0) { Flag = 1; OpenFile->CurrentLine->Cmd = P->Name; strcpy(OpenFile->CurrentLine->BinCode,P->Val); } P = P->Next; } if (Flag == 0 ) { OpenFile->CurrentLine->Cmd = "NULL"; CRITICAL_ERROR = 1; ErrMsg(15); return 0; } else return 1; } /* braking the operands - string of the operation line to separate operands */ void BrakingOperationOpr() { char *P = OpenFile->CurrentLine->OperandStr; char *comma = OpenFile->CurrentLine->OperandStr; int Len, OperNum = 0; char *Str = malloc(LINE_LEN); if (strcmp(P,"") == 0) /* Case no operands */ { strcat(OpenFile->CurrentLine->BinCode,"000000000000"); TranslateBinToOctal(OpenFile->CurrentLine->BinCode,OpenFile->CurrentLine->OctCode); strcpy(OpenFile->CurrentLine->OperandStr,"-"); } else { comma = strchr(P,','); /* if two operands */ if (comma != NULL) { while ( comma != NULL ) { Len = comma - P; ReturnString(P,Len,&Str); ReturnString(P,Len,&Str); AnalizeOperationOperand(&Str,'s'); /* Source Operand */ OperNum +=1; P = comma+1; comma = strchr(P,','); } /* search the last operand */ comma = P+ strlen(OpenFile->CurrentLine->OperandStr); Len = comma - P; ReturnString(P,Len,&Str); AnalizeOperationOperand(&Str,'d'); /* Destination Operand */ OperNum +=1; TranslateBinToOctal(OpenFile->CurrentLine->BinCode,OpenFile->CurrentLine->OctCode); } else /* Only one operand */ { comma = P+ strlen(OpenFile->CurrentLine->OperandStr); Len = comma - P; strcat(OpenFile->CurrentLine->BinCode,"000000"); ReturnString(P,Len,&Str); AnalizeOperationOperand(&Str,'d'); /* Destination Operand */ OperNum +=1; TranslateBinToOctal(OpenFile->CurrentLine->BinCode,OpenFile->CurrentLine->OctCode); } } CheckNumOfOper(OperNum); /* releasing pointers */ P = NULL; comma = NULL; Str = NULL; free(P); free(comma); free(Str); } void AnalizeOperationOperand(char **Str, char Oper) { char Chr; char Bin[4]; char BinNum[WORD+1]; char OctNum[OCT_STR]; int Number; Chr = **Str; if(!isalpha(Chr)) /* if not an alphabetic letter */ { switch(Chr) { case '#': strcat(OpenFile->CurrentLine->BinCode,"000"); /* immediate */ strcat(OpenFile->CurrentLine->BinCode,"000"); /* No Reg */ if (isdigit(*(*Str+1)) || *(*Str+1) == '-' ) { /* translate string to integer */ Number = translate_dec(*Str+1); ComputeBinary(Number,BinNum); TranslateBinToOctal(BinNum,OctNum); AllocateOperand(*Str+1,BinNum,OctNum,"Absolute"); OpenFile->Counter.IC +=1; } else ErrMsg(22); CheckAdrsMethod(Oper,0); break; case '@': if (checkOperandName(*Str+1,Bin)) /* if a register */ { strcat(OpenFile->CurrentLine->BinCode,"101"); /* reg indirect */ strcat(OpenFile->CurrentLine->BinCode,Bin); CheckAdrsMethod(Oper,5); } else { AllocateOperand(*Str+1,"???","???","???"); OpenFile->Counter.IC +=1; strcat(OpenFile->CurrentLine->BinCode,"010"); /* Label indirect */ strcat(OpenFile->CurrentLine->BinCode,"000"); }; CheckAdrsMethod(Oper,2); break; case '*': if (checkOperandName(*Str+1,Bin)) /* if a register */ { strcat(OpenFile->CurrentLine->BinCode,"011"); /* Relative */ strcat(OpenFile->CurrentLine->BinCode,Bin); ErrMsg(23); } else { AllocateOperand(*Str+1,"???","???","???"); OpenFile->Counter.IC +=1; strcat(OpenFile->CurrentLine->BinCode,"011"); /* Relative */ strcat(OpenFile->CurrentLine->BinCode,"000"); CheckAdrsMethod(Oper,3); }; break; }; } else /* if Alpahabetic Letter */ { if (checkOperandName(*Str,Bin)) /* if a register */ { strcat(OpenFile->CurrentLine->BinCode,"100"); /* reg direct */ strcat(OpenFile->CurrentLine->BinCode,Bin); CheckAdrsMethod(Oper,4); } else { AllocateOperand(*Str,"???","???","???"); OpenFile->Counter.IC +=1; strcat(OpenFile->CurrentLine->BinCode,"001"); /* Label Direct */ strcat(OpenFile->CurrentLine->BinCode,"000"); CheckAdrsMethod(Oper,1); } } } /* checking the addressing method of the operands */ void CheckAdrsMethod(char Type,int MethodNum) { struct CmdStruct *P = Cmd; while ( P->Name != OpenFile->CurrentLine->Cmd) P=P->Next; if(Type == 's') { if(P->Src[MethodNum] != 1) { CRITICAL_ERROR = 1; ErrMsg(27); } } else if(Type == 'd') if(P->Des[MethodNum] != 1) { CRITICAL_ERROR = 1; ErrMsg(27); } /* releasing pointers */ P = NULL; free(P); } /* checking number of operands is OK */ void CheckNumOfOper(int Num) { struct CmdStruct *P = Cmd; while ( P->Name != OpenFile->CurrentLine->Cmd) P=P->Next; if (P->OperNum != Num) ErrMsg(21); /* releasing pointers */ P = NULL; free(P); } /* Function returns 0 if name of operand is not a register name or the number of the register in case it exists */ int checkOperandName(char *Str, char Bin[]) { if (strcmp(Str,"r1") == 0 ) { strcpy(Bin,"001"); return 1; } else if (strcmp(Str,"r2") == 0 ) { strcpy(Bin,"010"); return 1; } else if (strcmp(Str,"r3") == 0 ) { strcpy(Bin,"011"); return 1; } else if (strcmp(Str,"r4") == 0 ) { strcpy(Bin,"100"); return 1; } else if (strcmp(Str,"r5") == 0 ) { strcpy(Bin,"101"); return 1; } else if (strcmp(Str,"r6") == 0 ) { strcpy(Bin,"110"); return 1; } else if (strcmp(Str,"r7") == 0 ) { strcpy(Bin,"111"); return 1; } else /* not a register */ { strcpy(Bin,"000"); return 0; } } void IgnoreWhiteSpaces() { char *Curr = CodeLine; while (*Curr == ' ' || *Curr == ' ') Curr++; CodeLine = Curr; /* releasing pointers */ Curr = NULL; free(Curr); } /* the function finds a label if exists and allocates it */ void FindLabel() { int Len; char *Stat = NULL; char *Str = malloc(LINE_LEN); Stat = strchr(CodeLine,':'); if (Stat != NULL) /* charecter was found */ { IgnoreWhiteSpaces(); Len = Stat - CodeLine; ReturnString(CodeLine,Len,&Str); if(OpenFile->CurrentLine->Type == 'O') AllocateLabel(Str,OpenFile->Counter.IC); /* Case Operation */ else AllocateLabel(Str,OpenFile->Counter.DC); /* Case instruction/data */ OpenFile->CurrentLine->Label = Str; CodeLine = Stat + 1; /* pointer to code line advances */ } /* releasing pointers */ Stat = NULL; Str = NULL; free(Stat); free(Str); } void ReturnString(char *StartPos,int Len,char **Str) { char *CodeLineCpy = malloc(LINE_LEN); strcpy(CodeLineCpy,StartPos); *(CodeLineCpy + Len) = ''; strcpy(*Str,CodeLineCpy); /* releasing pointers */ CodeLineCpy = NULL; free(CodeLineCpy); } /* in case Entries were defined copy the address from label list */ void CompleteEntryAddress() { struct EntryStruct *En = NULL; struct LabelStruct *Lb = NULL; int I; for (I=0;I<=LETTER_NUM;I++) { En = OpenFile->Entry[I]; while (En != NULL) { Lb = OpenFile->Lbl[I]; /* Same position in array */ while(Lb != NULL) { if (strcmp(Lb->Name,En->Name) == 0) /* copy address to entry file */ { strcpy(En->DecAdds,Lb->DecAdds); strcpy(En->OctAdds,Lb->OctAdds); } Lb = Lb->Next; } En = En->Next; } } /* releasing pointers */ En = NULL; Lb = NULL; free(En); free(Lb); } void WriteEntryFile() { FILE *FileHandle; char EntryFileName[NAME]; struct EntryStruct *En = NULL; int I; printf("Writing entry file for %s",OpenFile->CurrentFile); strcpy(EntryFileName,OpenFile->Name); strcat(EntryFileName,".ent");...

Essay Information


Words: 2351
Pages: 9.4
Rating: None

All Papers Are For Research And Reference Purposes Only. You must cite our web site as your source.