#아래와 같이 구조체로 예약 단어 및 길이를 정의하고, 이를 검색하여텍스트 파일의 각 줄에 나타나는 단어들에 대하여 “줄번호-위치-단어- 길이”를 출력한다. 단, 예약되지 않은 단어가 나타나면 “Undefined word” 메시지를 출력하고 무시한다(5 단어 이상 정의하고, 텍스트파일 10줄 이상) #
[word.s]

[출력결과]

J, COMP, JEQ 는 소스코드를 참고하면 정의 되지 않은 단어이다. 이 코드는 정의할 수 없는 코드라 출력이 되고 나머지는 줄번호 – 위치 – 단어 – 길이 순서로 출력된 것을 확인 할 수 있다.
<소스코드>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*단어 정의에 필요한 구조체*/
struct OPTAB{
char name[8];
int len;
}Wordtab[]={{"STA",4},{"JSUB",5},{"TIX",2},{"LDA",3},{"LDX",2}};
void get_token_num(char *bp,int *num, int *start);
int main() {
FILE *fp;
char buf[80];
int num=0;
int start=0;
int i;
if ((fp = fopen("word.s", "r")) == NULL) {
fprintf(stderr, "file not found...\n"); exit(1);
}
while(fgets(buf, sizeof(buf), fp) != NULL) {
get_token_num(buf,&num,&start);
}
fclose(fp);
}
/*num과 start는 해당 함수와 별개로 main함수에서 cp를 받을때마다
숫자와 길이를 이어 계산해야 함으로 포인터를 쓰는게 편리하다. */
void get_token_num(char *bp,int *num,int *start)
{
char *cp;
int i;
for(cp = strtok(bp, "\n"); cp != NULL; ) {
int un; //정의가 안된 단어를 판별하기 위한 변수
un=0;
for(i=0; i<sizeof(struct OPTAB);i++){
//만약 정의된 단어에 포함되는 단어라면 (구조체의 변수와 비교)
if(strcmp(cp,Wordtab[i].name)==0){
un++;
*num= *num +1;
printf("%d, %.2x, %s, %.2d\n",*num,*start,Wordtab[i].name,Wordtab[i].len);
//전 단어의 길이 뒤에 숫자 = 위치
*start = *start + Wordtab[i].len;
}
}
//만약 정의가 안된 단어이면 해당 변수는 초기화된 0으로 남아 있게 된다.
if(un==0)
printf("Undefined word\n");
cp = strtok(NULL," \n");
}
}
'ASSEMBLY' 카테고리의 다른 글
MAKING ASSEMBLYSYSTEM SOFTWARE (0) | 2019.06.26 |
---|---|
System Software _ project 4 (0) | 2019.06.26 |
System Software _ project 2 (0) | 2019.06.26 |
System Software _ project 1 (0) | 2019.06.26 |