📄 pass1.c.withallins
字号:
{ strcpy(AssemblyInstruction,trim(buff)); if((strcasecmp(AssemblyInstruction," " ))==0) continue; info = assemble(AssemblyInstruction) ; //getting the number of bytes in the instruction fprintf(fpdest,"%.4X %.2X\n",LocCounter,info->hex_code); //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 -> %d\n",gas51_symbol_table[i].Symbol,gas51_symbol_table[i].Address );dump_file(fpdest);}struct instruct * assemble(char *Instruction){ unsigned int i; char tokens[5][10]; //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 ; /* 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)); */ 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 = &instructions[i] ; return info ; } else { //do some stuff here to match a 2 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],"cjne") && num < 3 ) { gas51_errno = 6 ; gas51_perror(); } else if ( strcasecmp(tokens[0],"cjne") && num > 3 ) { gas51_errno = 5 ; gas51_perror(); } else { //everything ok...number of tokens are correct...proceeding with matching process... if (!isstandard(tokens[0]) ) //isstandard() returns 1 if standard , 0 if non-standard token... { if(substr(tokens[0],"#")) strcpy(newtokens[0],#8DATA); else strcpy(newtokens[0],ADDR } } } if(found_flag==1 ) { info = &instructions[i] ; return info ; } else { // do some stuff here to match a 3 byte instruction... // and break the loop immediately if a match occurs... } if(found_flag==1) { info = &instructions[i]; return info ; } // we have reached here means that all possible ins have been searched..so there must be an error... else // this should come after all instructions are checked... { gas51_errno = 4 ; // 4 is the error code for illegal instruction... gas51_perror(); //exit(1); } //return 0;}void push (struct gas51_file_info *temp_info ) { gas51_file_info_stack[gas51_stack_index] = *(temp_info) ; // hope this copies the whole structure... gas51_stack_index++ ; }struct gas51_file_info * pop(void){ struct gas51_file_info *temp_info ; gas51_stack_index--; temp_info = &(gas51_file_info_stack[gas51_stack_index]); return temp_info ; }int get_tokens(char t_addr[][10],char *instruction){ unsigned int i,j,k; char buff[10]; i=0; j=0; k=1; puts(instruction); while(instruction[i]!=' ' ) //at this point we may be sure that only a sinle space will be between 1st and 2nd token... { buff[j]=instruction[i] ; i++; j++; } buff[j] = '\0' ; // terminating buff with a trailing NULL... strcpy(t_addr[0],buff); //first token gone...two more to go... i++; //to get over the single space... j=0 ; //reiniting j for a new buff... while(1){ if(instruction[i]=='\n' || instruction[i]=='\0') { buff[j] = '\0' ; strcpy(t_addr[k],buff); if(strlen(buff)) //increment only if buff is non empty... k++; break ; } if(instruction[i]==',' || instruction[i]==' ' || instruction[i]=='\t') { buff[j] = '\0' ; strcpy(t_addr[k],buff); if(strlen(buff)) // increment only if buff is non empty... k++; i++; j=0; } else { buff[j]=instruction[i]; i++; j++; }} /* while(1) { } */ /* while(instruction[i]!=',' ) { buff[j] = instruction[i]; i++; j++; } buff[j] = '\0' ; //terminating buff with a trailing NULL... strcpy(t_addr[1],buff); //second token gone...one more to go... i++; j=0; while(instruction[i]!='\n') { buff[j] = instruction[i]; i++; j++; } */ return k ; // returning the total number of tokens found... }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -