📄 asm2ghs.c
字号:
/************************************************************* * File: tools/asm2ghs.c * Purpose: Convert standard MIPS asm syntax to GHS syntax * Author: Phil Bunce (pjb@carmel.com) * Revision History: * 981019 Created *//* * This program is designed to convert files that are written using * the standard MIPS assembler syntax, into the assembler syntax * required by the Green Hills assembler. * * For example, if your file is named foo.s you would write... * asm2ghs foo.s > foo.mip * ccmipe -c -o foo.o foo.mip * * The intermediate file *must* be named .mip. * For little endian use ccmipel. * * Caveat * This program uses a number of shortcuts to simplify parsing the * assembly language file. This means that it won't always work * correctly. */#include <stdio.h>#define MAX_AC 40#define strequ(x,y) ((strcmp(x,y))?0:1)/**************************************************************/main(int argc,char *argv[]){FILE *ifp;char *ifn;if (argc != 2) { fprintf(stderr,"usage: asm2ghs infile > outfile\n"); exit(1); }ifn = argv[1];ifp = fopen(ifn,"r");if (ifp == 0) { fprintf(stderr,"Can't open %s\n",ifn); exit(1); }fixasm(ifp);fclose(ifp);exit(0);}/************************************************************** fixasm(ifp)* This routine fixes all of the GHS assembly syntax differences.* It does use a few shortcuts and so may not work for all cases.* Output file must be named .mip.*/fixasm(ifp)FILE *ifp;{char buf1[200],buf2[200],*field[40],*p,*s,*e;int nf,n;while (fgets(buf1,200,ifp)) { chomp(buf1); /* delete the newline char(s) */ /* GHS doesn't like '#' comments in a .mip file */ /* delete all of the comments */ while ((s=strstr(buf1,"/*")) && (e=strstr(s,"*/"))) strcpy(s,e+2); if (strlen(buf1) && (p=(char *)strchr(&buf1[1],'#'))) *p = 0; strcpy(buf2,buf1); nf = argvize(field,buf2); /* split the line into args */ /* now handle the directives that are different */ /* GHS align is # of bytes, MIPS align is 2^# bytes */ if (nf >= 2 && strequ(field[0],".align")) { sscanf(field[1],"%d",&n); printf("\t.align %d\n",1<<n); } /* GHS .word does not accept repeat ':' option */ else if (nf == 2 && strequ(field[0],".word") && (p=strchr(field[1],':'))) { *p = 0; p++; printf(" .rept %s\n",p); printf(" .word %s\n",field[1]); printf(" .endr\n"); } /* GHS .extern does not accept the size option */ else if (nf >= 2 && strequ(field[0],".extern") && (p=strchr(field[1],','))) { *p = 0; p++; printf(" .extern %s\n",field[1]); } else printf("%s\n",buf1); }}/************************************************************** int argvize(av,s) * place address of each word in s into the array av */int argvize(av,s)char *av[];char *s;{char **pav = av, c;int ac;for (ac=0;ac<MAX_AC;ac++) { /* step over cntrls and spaces */ while(*s && *s <= ' ') ++s; /* if eos quit */ if(!*s) break; c = *s; /* if it's a quote skip forward */ if(c == '\'' || c == '"') { if (pav) *pav++ = ++s; while(*s && *s != c) ++s; if(*s) *s++ = 0; } else { /* find end of word */ if (pav) *pav++ = s; while(' ' < *s) ++s; } /* not eos inc ptr */ if(*s) *s++ = 0; }return(ac);}/************************************************************** chomp(p)* Remove trailing all EOL chars. Useful for lines read with fgets().*/chomp(p)char *p;{int n;n = strlen(p);while (n > 0) { if (!(p[n-1] == '\n' || p[n-1] == '\r')) break; n--; }p[n] = 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -