📄 pass1.c
字号:
for(m=0;m<info->nobytes;m++) { fprintf(fpdest,"%.4X %.2X\n",LocCounter,info->hex_array[m]); //writing into temp hexfile... LocCounter++; } //fprintf(fpdest,"%.4X %.2X\n",LocCounter,info->hex_array[0]); //writing into temp hexfile... //LocCounter += info->nobytes ; // incrementing the LC by sizeof present instruction... //printf("%s\n",AssemblyInstruction); } //sscanf(buff,"%s:%s",Label,Mnemonic); //printf("%s %s\n",Label,Mnemonic); */ // } else // code to handle gas51 special escape sequence... { if(pat_index(buff,":start")) { strcpy(temp_info->file,gas51_file_being_assembled); temp_info->file_line_no = gas51_line_no ; push(temp_info); // pushing the current file info into stack... //push(gas51_line_no); sscanf(buff,"%c%s%s",&ch,gas51_file_being_assembled,str); //reinit'ing file and line no... gas51_line_no = 0 ; } else if(pat_index(buff,":end")) { temp_info=pop(); strcpy(gas51_file_being_assembled,temp_info->file); gas51_line_no = temp_info->file_line_no; gas51_line_no--; // to compensate for the '++' op at start... //current_line_no=pop(); } } } for(i=0;i<=20;i++) if(strlen(gas51_symbol_table[i].Symbol)!=0) printf("%s -> %X\n",gas51_symbol_table[i].Symbol,gas51_symbol_table[i].Address );dump_file(fpdest);return fpdest;}struct instruct_info *assemble(char *Instruction)//int *assemble(char *Instruction){ //static int hex_array[3] ; unsigned int i; static char tokens[5][10],newtokens[5][10],ins[20]; //a 2D array of chars to hold instruction tokens... char found_flag=0; //flag to denote match status -> 0 means match not found while 1 means match found... int num,m,l ; /* tokens[0]=malloc(10*sizeof(char)); tokens[1]=malloc(10*sizeof(char)); tokens[2]=malloc(10*sizeof(char)); tokens[3]=malloc(10*sizeof(char)); tokens[4]=malloc(10*sizeof(char)); */ //hex_array[0]=-1; //hex_array[1]=-1; //initialising the array properly before continuing... //hex_array[2]=-1; for (i=0;i<=0xFF;i++) { if(!strcasecmp(instructions[i].mnemonic,Instruction )) { found_flag = 1; // this is the code to test for 1 byte ins... break ; } } if(found_flag==1) //means instruction matched with opcode... { info->hex_array[0] = instructions[i].hex_code; info->nobytes = 1; //info = &instructions[i] ; return info ; //return hex_array; } else { //do some stuff here to match a 2 byte or 3 byte instruction... // and break the loop immediately if a match occurs.. num = get_tokens(tokens,Instruction); //all tokens copied into token array...returns total number of tokens... if ((strcasecmp(tokens[0],"cjne")==0) && (num > 4) ) // when a cjne ins contains more than 4 tokens... { gas51_errno = 5 ; gas51_perror() ; } else if ( (strcasecmp(tokens[0],"cjne")==0) && (num < 4) ) // when a cjne ins contains less than 4 tokens... { gas51_errno = 6 ; gas51_perror(); } else if( ((strcasecmp(tokens[0],"ljmp")==0) || (strcasecmp(tokens[0],"lcall")==0) ) && (num>2) ) { gas51_errno = 5 ; gas51_perror() ; } else if( ((strcasecmp(tokens[0],"ljmp")==0) || (strcasecmp(tokens[0],"lcall")==0) ) && (num<2) ) { gas51_errno = 6 ; gas51_perror() ; } // the basic strategy in this if-else ladder is that execution proceeds only when the number of // tokens are *EXACTLY* the same as required by the instruction being assembled or the program //is exited and an error is given, stating either extra or less tokens than expected... ;) // else if( (strcasecmp(tokens[0],"inc")==0) || (strcasecmp(tokens[0],"dec")==0) || (strcasecmp(tokens[0],"ajmp")==0) || (strcasecmp(tokens[0],"acall")==0)|| (strcasecmp(tokens[0],"jz")==0) || (strcasecmp(tokens[0],"jnz")==0) || (strcasecmp(tokens[0],"sjmp")==0) || (strcasecmp(tokens[0],"cpl")==0) || (strcasecmp(tokens[0],"push")==0) || (strcasecmp(tokens[0],"clr")==0) || (strcasecmp(tokens[0],"pop")==0) || (strcasecmp(tokens[0],"setb")==0) || && (num>2) ) else if( ((strcasecmp(tokens[0],"inc")==0) || (strcasecmp(tokens[0],"dec")==0) || \ (strcasecmp(tokens[0],"ajmp")==0) || (strcasecmp(tokens[0],"acall")==0) || \ (strcasecmp(tokens[0],"jz")==0) || (strcasecmp(tokens[0],"jnz")==0) || \ (strcasecmp(tokens[0],"sjmp")==0) || (strcasecmp(tokens[0],"cpl")==0) || \ (strcasecmp(tokens[0],"push")==0) || (strcasecmp(tokens[0],"clr")==0) || \ (strcasecmp(tokens[0],"pop")==0) || (strcasecmp(tokens[0],"setb")==0) ) && \ (num>2) ) { gas51_errno = 5 ; gas51_perror() ; } // else if( ((strcasecmp(tokens[0],"inc")==0) || (strcasecmp(tokens[0],"dec")==0) || (strcasecmp(tokens[0],"ajmp")==0) ) && (num<2) ) else if(((strcasecmp(tokens[0],"inc")==0) || (strcasecmp(tokens[0],"dec")==0) || \ (strcasecmp(tokens[0],"ajmp")==0) || (strcasecmp(tokens[0],"acall")==0)|| \ (strcasecmp(tokens[0],"jz")==0) || (strcasecmp(tokens[0],"jnz")==0) || \ (strcasecmp(tokens[0],"sjmp")==0) || (strcasecmp(tokens[0],"cpl")==0) || \ (strcasecmp(tokens[0],"push")==0) || (strcasecmp(tokens[0],"clr")==0) || \ (strcasecmp(tokens[0],"pop")==0) || (strcasecmp(tokens[0],"setb")==0) ) && (num<2) ) { gas51_errno = 6 ; gas51_perror() ; } else if((strcasecmp(tokens[0],"cjne")) && (strcasecmp(tokens[0],"ljmp" )) && \ (strcasecmp(tokens[0],"lcall")) && (strcasecmp(tokens[0],"inc" )) && \ (strcasecmp(tokens[0],"dec" )) && (strcasecmp(tokens[0],"ajmp" )) && \ (strcasecmp(tokens[0],"acall")) && (strcasecmp(tokens[0],"jz" )) && \ (strcasecmp(tokens[0],"jnz" )) && (strcasecmp(tokens[0],"sjmp" )) && \ (strcasecmp(tokens[0],"cpl" )) && (strcasecmp(tokens[0],"push" )) && \ (strcasecmp(tokens[0],"clr" )) && (strcasecmp(tokens[0],"pop" )) && \ (strcasecmp(tokens[0],"setb" )) && (num > 3 ) ) // region of code where number of tokens present { // are checked for correctness gas51_errno = 5 ; //this step need not be repeated later on gas51_perror(); //during matching 3 byte instructions... } // else if( strcasecmp(tokens[0],"cjne") && (strcasecmp(tokens[0],"ljmp" )) && (strcasecmp(tokens[0],"lcall" )) && (strcasecmp(tokens[0],"inc" )) && (strcasecmp(tokens[0],"dec" )) && (num < 3 ) ) else if( (strcasecmp(tokens[0],"cjne")) && (strcasecmp(tokens[0],"ljmp" )) && \ (strcasecmp(tokens[0],"lcall" )) && (strcasecmp(tokens[0],"inc" )) && \ (strcasecmp(tokens[0],"dec" )) && (strcasecmp(tokens[0],"ajmp" )) && \ (strcasecmp(tokens[0],"acall" )) && (strcasecmp(tokens[0],"jz" )) && \ (strcasecmp(tokens[0],"jnz" )) && (strcasecmp(tokens[0],"sjmp" )) && \ (strcasecmp(tokens[0],"cpl" )) && (strcasecmp(tokens[0],"push" )) && \ (strcasecmp(tokens[0],"clr" )) && (strcasecmp(tokens[0],"pop" )) && \ (strcasecmp(tokens[0],"setb" )) && (num < 3 ) ) { gas51_errno = 6 ; gas51_perror(); } else { //everything ok...number of tokens are correct...proceeding with matching process... for(i=0;i<num;i++) { /* if(!strcasecmp(tokens[i],"A")) { strcpy(tokens[i],"0E0"); //continue ; } */ if(!strcasecmp(tokens[i],"B")) { strcpy(tokens[i],"0F0"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0xF0); //continue ; } else if(!strcasecmp(tokens[i],"PSW")) { strcpy(tokens[i],"0D0"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0xD0); //continue ; } else if(!strcasecmp(tokens[i],"DPH")) { strcpy(tokens[i],"83"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x83); //continue ; } else if(!strcasecmp(tokens[i],"DPL")) { strcpy(tokens[i],"82"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x82); //continue ; } else if(!strcasecmp(tokens[i],"P0")) { strcpy(tokens[i],"80"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x80); //continue ; } else if(!strcasecmp(tokens[i],"P1")) { strcpy(tokens[i],"90"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x90); //continue ; } else if(!strcasecmp(tokens[i],"P2")) { strcpy(tokens[i],"0A0"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0xA0); //continue ; } else if(!strcasecmp(tokens[i],"P3")) { strcpy(tokens[i],"0B0"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0xB0); //continue ; } else if(!strcasecmp(tokens[i],"IP")) { strcpy(tokens[i],"0B8"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0xb8); //continue ; } else if(!strcasecmp(tokens[i],"SP")) { strcpy(tokens[i],"81"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x81); //continue ; } else if(!strcasecmp(tokens[i],"IE")) { strcpy(tokens[i],"0A8"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0xA8); //continue ; } else if(!strcasecmp(tokens[i],"TMOD")) { strcpy(tokens[i],"89"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x89); //continue ; } else if(!strcasecmp(tokens[i],"TCON")) { strcpy(tokens[i],"88"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x88); //continue ; } else if(!strcasecmp(tokens[i],"TH0")) { strcpy(tokens[i],"8C"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x8C); //continue ; } else if(!strcasecmp(tokens[i],"TL0")) { strcpy(tokens[i],"8A"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x8A); //continue ; } else if(!strcasecmp(tokens[i],"TH1")) { strcpy(tokens[i],"8D"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x8D); //continue ; } else if(!strcasecmp(tokens[i],"TL1")) { strcpy(tokens[i],"8B"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x8B); //continue ; } else if(!strcasecmp(tokens[i],"SCON")) { strcpy(tokens[i],"98"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x98); //continue ; } else if(!strcasecmp(tokens[i],"SBUF")) { strcpy(tokens[i],"99"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x99); //continue ; } else if(!strcasecmp(tokens[i],"PCON")) { strcpy(tokens[i],"87"); //fprintf(fpprehex,"%.4X %.2X\n",addr,0x87); //continue ; } } for(i=0,m=1,l=0;i<num;i++) { if (!isstandard(tokens[i]) ) //isstandard() returns 1 if standard , 0 if non-standard token... { if(islabel(tokens[i])) //islabel() returns number of tokens which are labels , 0 if none... { //this code handles the token if it is a label... info->label_flag++ ; strcpy(info->labels[l],tokens[i]); strcpy(newtokens[i],"ADDR"); //ADDR placeholder copied into newly created tokens... l++; } else /*brace marker**/ { info->label_flag=0; if(pat_index(tokens[i],"#")) strcpy(newtokens[i],"#DATA"); else strcpy(newtokens[i],"ADDR"); if(tokens[i][0]!='#') { sscanf(tokens[i],"%X",&info->hex_array[m]); // writing the operand in hex... m++; } else if(tokens[i][0]=='#') { sscanf(substr(tokens[i],2,strlen(tokens[i])-1) , "%X" ,&info->hex_array[m] ); m++; } /** brace marker**/ } } else strcpy(newtokens[i],tokens[i]); //standard tokens are being copied into newtokens array as-it-is... // }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -