C언어 macro치환부분이 잘안되서요 도와주세요
글쓴이: klokfc / 작성시간: 화, 2014/12/09 - 4:06오후
#include<stdio.h> #include<string.h> #include <stdlib.h> struct MacroNameTab{ char MacroName[10]; char fluent[10][10]; int fluent_count; }; struct MacroNameTab MacroTab[10]; int Mcount = 0; ////////////////////////////////////////////////////////////////////////////// ///////////////////////함수 선언부 void mpass1(); void mpass2(); int Search_M_N( char *token ); ///////////////////////////////////////////////////////////////////////////// //////////////////////MAIN 시작 void main() { mpass1(); mpass2(); } //////////////////////////////////////////////////////////////////////////// /////////////////////MPASS1 시작. void mpass1() { FILE *in, *middle, *macro; char input[80], tempin[80]; char *token1; //한 라인을 잘라 저장. char *token2; char *token3; int mfluent,i,temp; if(( in = fopen("input.asm","r")) == NULL )// 실패시 NULL { printf(" \"input.asm\" 파일이 존재하지 않음 \n"); exit(1); } else printf(" input.asm open !! \n"); //--------------- if((middle = fopen("middle.asm","w")) == NULL ) { printf(" \"middle.asm\" 파일 생성 실패 !!\n"); exit(1); } //--------------- if((macro = fopen("macro.txt","w")) == NULL ) { printf(" \"macro.txt\" 파일 생성 실패 !!\n"); exit(1); } while( 1 ) { fgets(input,80,in); strcpy(tempin, input); if( (temp = strlen(input)) == 1 ) //공백 처리 continue; token1 = strtok( input,"' '"); if( !strcmp( token1, "END" )) { printf(" \"input.asm\" 파일 읽기 완료 !! \n"); fprintf(middle, "%s",tempin); break; } if( (token2 = strtok( NULL, "' '")) != NULL ) { if( !strcmp( token2, "MACRO" ) { mfluent = 0; strcpy(MacroTab[Mcount].MacroName, token1); while( (token3 = strtok(NULL,", ^")) != NULL ) { strcpy( MacroTab[Mcount].fluent[mfluent],token3 ); mfluent++; } MacroTab[Mcount].fluent_count = mfluent; Mcount++; fprintf(macro, "%s", tempin); while( 1 ) { fgets(input,80,in); strcpy(tempin, input); if( (temp = strlen(input)) == 1 ) continue; token1 = strtok( input," "); if( !strcmp( token1, "ENDM\n" )) { fprintf(macro,"%s",token1); break; } if( *token1 == '$' ) { fprintf(macro,"%s ",token1); token1 = strtok( NULL," ', "); token2 = strtok( NULL," ', ^"); token3 = strtok( NULL," ', "); } else { token2 = strtok( NULL," ', ^"); token3 = strtok( NULL," ', "); } //----------------------------- fprintf(macro,"%s ",token1); if( !strcmp( token2, "=X")) { for( i=0; i<mfluent; i++ ) { if( !strcmp(MacroTab[Mcount-1].fluent[i],token3 )) break; } fprintf(macro,"%s'?%d'\n",token2,i); } else if( !strcmp((token2 +1 ),"&ID")) { for( i=0; i<mfluent; i++ ) { if( !strcmp(MacroTab[Mcount-1].fluent[i],(token2 + 1 ))) break; } fprintf(macro,"%c?%d\^%c\n",*token2,i,*token3); } //-------------------------- else if( *token2 == '$' ) { for( i=0; i<mfluent; i++ ) { if( !strcmp(MacroTab[Mcount-1].fluent[i],token2 )) break; } fprintf(macro,"?%d",i); if( token3 != NULL ) fprintf(macro,",X\n"); else fprintf(macro,"\n"); } else if( *token2 == '$' ) fprintf(macro,"%s",token2); else { fprintf(macro,"%s",token2); if( token3 != NULL ) fprintf(macro,",%s",token3); } }//while end } else fprintf(middle, "%s", tempin); } } fcloseall(); } ///////////////////////////////////////// mpass2 void mpass2() { FILE *middle, *macro, *result; char input[80],tempin[80],macin[80],mactemp[80]; char *token1, *token2, *token3, *mtoken1; char A = 'A'; int i, j, m,n,k; if(( middle = fopen("middle.asm","r")) == NULL ) { printf(" middle.asm 파일이 존재하지 않음 \n"); exit(1); } else printf(" middle.asm file open !!\n"); result = fopen("result.asm","w"); while( 1 ) { fgets(input,80,middle); strcpy(tempin, input); token1 = strtok( input,"' '"); m = 0; if( !strcmp( token1, "END" )) { printf(" \"middle.asm\" 파일 읽기 완료 !! \n"); fprintf(result, "%s",tempin); break; } if( (n = Search_M_N(token1)) > -1 ) { i = 0; while( (token2 = strtok(NULL,"' ', ")) != NULL ) { strcpy( MacroTab[Search_M_N(token1)].fluent[i],token2 ); i++; } m = 1; //--------------------------- } else { token2 = strtok(NULL,"' '"); if( (n = Search_M_N(token2)) >-1 ) { i = 0; while( (token3 = strtok(NULL,"' ', ")) != NULL ) { strcpy( MacroTab[Search_M_N(token2)].fluent[i],token3 ); i++; } //--------------------------- m = 2; } } if( m > 0 ) { fprintf(result,"%s",tempin); if( m == 2 ) { fprintf(result, "%s ", token1); strcpy(token1,token2); } if(( macro = fopen("macro.txt","r")) == NULL ) { printf(" \"macro.txt\" 파일이 존재하지 않음 \n"); exit(1); } else printf(" \"macro.txt\" file open !!\n"); // while( 1 ) { fgets(macin,80,macro); strcpy(mactemp,macin); mtoken1 = strtok(macin,"' '"); if( !strcmp(mtoken1, token1) ) while( 1 ) // { fgets(macin,80,macro); strcpy(mactemp,macin); if( !strcmp(macin, "ENDM\n") ) break; else { token1 = strtok( macin," "); if( *token1 == '$' ) { A += 1; fprintf(result,"$A%c%s ",A,(token1 +1 )); token1 = strtok( NULL,"' "); token2 = strtok( NULL," ', ^"); token3 = strtok( NULL," ', "); } //--------------------------- else { token2 = strtok( NULL," ', ^"); token3 = strtok( NULL," ', "); } fprintf(result,"%s ",token1 ); if( !strcmp( token2, "=X") ) { for( j=0; j<i; j++ ) if( *(token3 + 1) == j+48 ) fprintf(result,"=X'%s'\n",MacroTab[n].fluent[j]); } else if( !strcmp((token2 + 1),"?0")) { fprintf(result,"%c",*token2); for (k=0; k <strlen(MacroTab[n].fluent[0]) -1; k++ ) fprintf(result,"%c",MacroTab[n].fluent[0][k]); fprintf(result,"%c\n",*token3); } //--------------------------- else if( *token2 == '?') { for( j=0; j<i; j++ ) if( *(token2 + 1 ) == j+48 ) fprintf(result,"%s",MacroTab[n].fluent[j]); if( token3 != NULL ) fprintf(result,",X\n"); } else if( *token2 == '$' ) fprintf(result,"$A%c%s",A,(token2 + 1)); else //매크로 인자를 사용하지 않는 라인. { fprintf(result,"%s",token2); if( token3 != NULL ) fprintf(result,",%s",token3); } } //---------------------------------------------------------------------------------------- } break; } } } else fprintf(result, "%s",tempin); } fcloseall(); } int Search_M_N( char *token ) { int i; for( i=0; i<Mcount; i++ ) if( !strcmp( token,MacroTab[i].MacroName ) ) return i; return -1; }
여기까지가 소스코드입니다.
MOVES MACRO X,Y,Z
MOV AX,X
MOV BX,Y
MOV CX,Z
ENDM
MAIN SEGMENT
MOVES 1,2,3
MOV AH,4CH
INT 21H
MAIN ENDS
END
이게 INPUT.asm 파일소스 에요
MAIN SEGMENT
MOVES 1,2,3 // 이부분은 출력이 되면안되고
MOV AX,X //MOV AX,1
MOV BX,Y //MOV BX,2
MOV CX,Z //MOV CX,3 이렇게 나와야 하네요 ㅠㅠ
MOV AH,4CH
INT 21H
MAIN ENDS
END
이건 결과가 생성된 result.asm 결과파일인데요
치환이 제대로 안되네요 문제가 뭔지 찾아보고 있는데
잘모르겠어요
Forums:
댓글 달기