#include <stdio.h> #include <string.h> #include <ctype.h> #include <math.h> #include <stdlib.h>
struct reg { char reg_name[3]; /*·¹Áö½ºÅÍ À̸§À» º¯¼ö·Î ¼±¾ð*/ char reg_num[4]; /*·¹Áö½ºÅÍ ¹øÈ£¸¦ º¯¼ö·Î ¼±¾ð*/ } Reg[20]; /* ·¹Áö½ºÅÍ¿¡ ´ëÇÑ À̸§°ú ¹øÈ£¸¦ ÀúÀåÇÏ´Â ±¸Á¶Ã¼ */
struct ins { char instruct[6]; /*ÀνºÆ®·°¼ÇÀ» º¯¼ö·Î¼±¾ð*/ char dest[2]; /*¾îµå·¹½º ¸ðµå¸¦ º¯¼ö·Î ¼±¾ð*/ char sour[2]; /*¾îµå·¹½º ¸ðµå¸¦ º¯¼ö·Î ¼±¾ð*/ char word_type[2]; /*¿öµå ŸÀÔÀ» º¯¼ö·Î ¼±¾ð*/ char ins_code[3]; /*ÀνºÆ®·°¼Ç ÄÚµåÁ¤º¸¸¦ º¯¼ö·Î ¼±¾ð*/ char ins_len[2]; /*ÀνºÆ®·°¼Ç ±æÀ̸¦ º¯¼ö·Î ¼±¾ð*/ char mod_reg[9]; /*·¹Áö½ºÅÍ ¸ðµå ÁöÁ¤*/ } Instr[100]; /* °¢ ÀνºÆ®·°¼ÇÀÇ Á¤º¸¸¦ º¸°üÇÏ´Â ±¸Á¶Ã¼ */ int MaxI;
struct symbol_tbl{ char symbol[10]; /*½Éº¼ Å×À̺í ÁöÁ¤*/ char word_type[2]; /*¿öµå ŸÀÔ ÁöÁ¤*/ int lc; char data[10]; /*µ¥ÀÌÅÍ ÁöÁ¤*/ } Symbol[30]; /* ½Éº¼ Å×À̺í */ int MaxS;
struct sentence{ char label[10]; char _operator[10]; char operand[3][10]; } Sen; int LC;
/* ÈÀÏ¿¡¼ ·¹Áö½ºÅÍ¿Í ÀνºÆ®·°¼ÇÀÇ Á¤º¸¸¦ Àд´Ù. */ void Initialize(){ int i=0,j=1; FILE *regi,*inst; /*·¹Áö½ºÅÍ¿Í ÀνºÆ®·°¼Ç Á¤º¸ Àоî¿Â´Ù*/
regi=fopen("reg.tbl","r"); /*·¹Áö½ºÅÍ Å×À̺í Àоî¿Â´Ù*/ inst=fopen("inst.tbl","r"); /*ÀνºÆ®·°¼Ç Å×À̺í Àоî¿Â´Ù*/ while(!feof(regi)){ fscanf(regi,"%s %s\n",Reg[i].reg_name,Reg[i].reg_num); i++; } /* ·¹Áö½ºÅÍ Å×À̺íÀ» ÀÛ¼ºÇÑ´Ù. */ while(!feof(inst)){
fscanf(inst,"%6s%2s%2s%4s%3s%2s%9s\n",Instr[j].instruct,Instr[j].dest, Instr[j].sour,Instr[j].word_type,Instr[j].ins_code,Instr[j].ins_len,Instr[j].mod_reg); j++; } /* ¸í·É¾î Å×À̺íÀ» ÀÛ¼ºÇÑ´Ù. */ MaxI=j-1; fclose(regi); fclose(inst); }
int Analyze(char *operand){ int i=0; char *regist[] = {"AX","BX","CX","DX","AL","BL","CL","DL","AH","BH","CH","DH",0X00}; /* ·¹Áö½ºÅÍÀÇ À̸§À» ÀúÀå */
if(isdigit(operand[0])) return 0; /* immediate ¾îµå·¹½º ¸ðµå¸¦ ÁöÁ¤ */
else while(regist[i]!= 0x00) /*·¹Áö½ºÅÍ i°¡ 0x00ÀÌ ¾Æ´Òµ¿¾È...,*/ if(!strcmp(operand,regist[i])){ /*·¹Áö½ºÅÍ ¸ðµå¸¦ ÁöÁ¤ */ if(i<4) return 1; /*¸¸¾à,i°¡ 4º¸´Ù ÀÛ´Ù¸é 1(¹øÁö)·Î µ¹¾Æ°£´Ù*/ else return 2; /*±×·¸Áö ¾Ê´Ù¸é 2(¹øÁö)·Î µ¹¾Æ°£´Ù*/ }
else i++; /*±×·¸Áö ¾Ê´Ù¸é i¸¦ Áõ°¡*/ return 3; /* 3(¹øÁö)À¸·Î µ¹¾Æ°£´Ù*/ }
#define MAX_INS 1 /* ¸í·É¾îÀÇ ÃÖ´ë °³¼ö¸¦ ÁöÁ¤ */ int Add_Chk(char *sen) /* ¿ÀÆÛ·£µåÀÇ ¾îµå·¹½º ¸ðµå¸¦ ÆÇÁ¤ */ { register k = MaxI; int i=0, j=0, l=0, wp=0; char op[5][10], *opcode[] = {"mov",""};
while(sen[wp]!='\n'){ while(sen[wp] == ' ' || sen[wp] == '\t' || sen [wp] == ',') wp++; /* °ø¹é , ÅÇ , ÄÞ¸¶´Â Åë°ú */ while(sen[wp]!=' '&&sen[wp]!='\t'&&sen[wp]!='\n'&&sen[wp]!=','){ *(op[j]+1)=sen[wp]; i++; wp++; } *(op[j]+i)='\0'; i=0; j++; } i=0; while(strcmp(opcode[i],"")) /*¿ÀÆÛ·£µå Äڵ尡 °ø¹éÀÎÁö ºñ±³..,*/ if(strcmp(opcode[i],op[0]))i++; /*¸¸¾à,¿ÀÆÛ·£µå°¡ 0À̸é iÁõ°¡*/ else{ /*±×·¸Áö ¾Ê´Ù¸é..,*/ strcpy(Sen._operator,op[0]);/*op[0]À» Sen._operator¿¡ ÀúÀå*/ for(l=1;l<j;l++)strcpy(Sen.operand[l-1],op[l]); break; } if(i==MAX_INS){ strcpy(Sen.label,op[0]); strcpy(Sen._operator,op[1]); for(l=2;l<j;l++)strcpy(Sen.operand[l-2],op[l]); } /* Çѹ®ÀåÀ» ºÐ¼®ÇÏ¿© ·¹À̺í, ¿ÀÆÛ·¹ÀÌÅÍ¿Í ¿ÀÆÛ·£µå·Î ºÐ·ùÇÑ´Ù */ strcpy(Instr[0].instruct,op[0]); /* OPÄڵ带 ÀúÀå */ switch(Analyze(op[1])){ case 0: strcpy(Instr[0].dest,"i"); /*i¸¦ instr[0].dest¿¡ ÀúÀå*/ break; case 1: strcpy(Instr[0].dest,"r"); /*rÀ» instr[0].dest¿¡ ÀúÀå*/ strcpy(Instr[0].word_type,"w"); /*w¸¦ instr[0].word_type¿¡ ÀúÀå*/ break; case 2: strcpy(Instr[0].dest,"r"); /*rÀ» instr[0].dest¿¡ ÀúÀå*/ strcpy(Instr[0].word_type,"b"); /*b¸¦ instr[0].word_type¿¡ ÀúÀå*/ break; case 3: strcpy(Instr[0].dest,"m"); /*mÀ» instr[0].dest¿¡ ÀúÀå*/ break; } switch(Analyze(op[2])){ case 0: strcpy(Instr[0].sour,"i"); /*i¸¦ instr[0].sour¿¡ ÀúÀå*/ break; case 1: strcpy(Instr[0].sour,"r"); /*r¸¦ instr[0].sour¿¡ ÀúÀå*/ strcpy(Instr[0].word_type,"w"); /*w¸¦ instr[0].word_type¿¡ ÀúÀå*/ break; case 2: strcpy(Instr[0].sour,"r"); /*r¸¦ instr[0].sour¿¡ ÀúÀå*/ strcpy(Instr[0].word_type,"b"); /*b¸¦ instr[0].word_type¿¡ ÀúÀå*/ break; case 3: strcpy(Instr[0].sour,"m"); /*m¸¦ instr[0].sour¿¡ ÀúÀå*/ break; } while(stricmp(Instr[k].instruct,Instr[0].instruct) /*instr[k]±¸Á¶Ã¼¿Í instr[0]±¸Á¶Ã¼ºñ±³*/ ||strcmp(Instr[k].dest,Instr[0].dest) ||strcmp(Instr[k].sour,Instr[0].sour) ||strcmp(Instr[k].word_type,Instr[0].word_type)) k--; return k; } void PassI(char *buf){ int i; static int j = 0;
i = Add_Chk(buf); if(i){ printf("%04X: %s",LC,buf); LC+=atoi(Instr[i].ins_len); } else{ /*ºñ±³¹®*/ if(!stricmp(Sen._operator,"dw")) /*Sen._operator°¡ dw¶ó¸é..,*/ strcpy(Symbol[j].word_type,"w"); /*w¸¦ symbol[j].word_type¿¡ º¹»ç*/ else if(!stricmp(Sen._operator,"db")) /*Sen._operator°¡ db¶ó¸é..,*/ strcpy(Symbol[j].word_type,"b"); /*b¸¦ Symbol[j].word_type¿¡ º¹»ç*/ strcpy(Symbol[j].symbol,Sen.label); /*Sen.label¸¦ Symbol[j].symbol¿¡ º¹»ç*/ strcpy(Symbol[j].data,Sen.operand[0]); /*Sen.operand[0]¸¦ Symbol[j].data¿¡ º¹»ç*/ Symbol[j].lc=LC; printf("%04X: %s",LC,buf); if(*Symbol[j].word_type == 'w') LC += 2; /* *Symbol[j].word_typeÀÌ w¹Ç·Î À§Ä¡ °è¼ö±â¸¦ 2·Î ÁöÁ¤*/ else if(*Symbol[j].word_type == 'b') LC += 1;/* *Symbol[j].word_typeÀÌ bÀ̹ǷΠÀ§Ä¡°è¼ö±â 1·Î ÁöÁ¤*/ j++; } }
int btoi(char *dig){ register i=0, ret=0;
while(*(dig + i)!= '\0'){ if(*(dig + i) == '1') ret+=pow(2,strlen(dig + i)-1); i++; } return ret; }
void PassII(char *buf){ int i,j = 0,k = 0;
i = Add_Chk(buf); if(i){ printf("%04x: %3s",LC,Instr[i].ins_code); if(!strcmp(Instr[i].dest,"r")){ /*Instr[i].dest°¡ rÀ̶ó¸é..,*/ while(stricmp(Reg[j].reg_name,Sen.operand[0]))/*·¹Áö½ºÅÍ¿Í¿ÀÆÛ·£µå[0]ºñ±³*/ j++; strncpy(strchr(Instr[i].mod_reg,'?'),Reg[j].reg_num,3); } j = 0; if(!strcmp(Instr[i].sour,"r")){ /*Instr[i].sour°¡ rÀ̶ó¸é..,*/ while(stricmp(Reg[j].reg_name,Sen.operand[1])) /*·¹Áö½ºÅÍ¿Í ¿ÀÆÛ·£µå[1] ºñ±³*/ j++; strncpy(strchr(Instr[i].mod_reg,'?'),Reg[j].reg_num,3); }
if(strcmp(Instr[i].dest,"m")&&strcmp(Instr[i].sour,"m")) printf(" %02X %s",btoi(Instr[i].mod_reg),buf); else{ if(!strcmp(Instr[i].dest, "m")) /*Instr[i].dest°¡ m À̶ó¸é..,*/ while(strcmp(Symbol[k].symbol,Sen.operand[0])) k++; /* ºñ±³Çؼ*k°ª Áõ°¡*/ else if(!strcmp(Instr[i].sour,"m")) /*Instr[i].sour°¡ m À̶ó¸é..,*/ while(strcmp(Symbol[k].symbol,Sen.operand[1])) k++; /* ºñ±³Çؼ k°ª Áõ°¡*/ printf(" %02X %04X %s",btoi(Instr[i].mod_reg),Symbol[k].lc,buf); } LC+=atoi(Instr[i].ins_len); } else{ k=0; while(strcmp(Symbol[k].symbol,Sen.label)) k++; if(!strcmp(Symbol[k].word_type,"w")) /*Symbol[k].word_typeÀÌ wÀÌ¸é ½ÇÇà*/ printf("%04X:%04X %20s",LC,atoi(Symbol[k].data),buf); if(!strcmp(Symbol[k].word_type,"b")) /*Symbol[k].word_type¤Ó bÀÌ¸é ½ÇÇà*/ printf("%04X: %02X %20s",LC,atoi(Symbol[k].data),buf); if(*Symbol[k].word_type=='w') LC +=2;/**Symbol[k].word_typeÀÌ wÀ̸é À§Ä¡°è¼ö±â 2·ÎÁöÁ¤*/ else if(*Symbol[k].word_type == 'b') LC +=1;/**Symbol[k].word_typeÀÌ bÀ̸é À§Ä¡°è¼ö±â 1·ÎÁöÁ¤*/ } }
void main(){ char buf[50]; FILE *in;
in = fopen("test1.asm","r"); Initialize(); printf("\nPass1:\n"); while(1){ fgets(buf, 30, in); if(feof(in)) break; PassI(buf); } /* Æнº 1 */ rewind(in); LC=0; /*À§Ä¡°è¼ö±â*/ printf("\nPass2:\n"); while(1){ fgets(buf,30,in); if(feof(in)) break; PassII(buf); } fclose(in); }
|