/* assembler (for 4-bit machine pucci) */ /* meirei no hyou(opfile) to */ /* source program(source) wo youi site kudasai */ #include #define OPHSIZE 256 #define LBHSIZE 256 #define BUFSIZE 5000 #define LINSIZE 128 #define ONEOP 0 #define TWOOP 1 #define DCOP 2 #define DSOP 3 struct tbl { char *name;int code,type; }; struct tbl optable[OPHSIZE],labtable[LBHSIZE]; char buff[BUFSIZE]; int bp,errflag; main(argc,argv) int argc; char *argv[]; { int i; if (argc!=3) { printf("invalid argument count\n"); printf("Input (%%asm opfile source).\n"); exit(); } bp=0; opinit(argv[1]); for (i=0;i'9' && line[p]<'a' || line[p]>'f') { printf("not hexadecimal: %s\n",line); break; } if (line[p]<='9') code=16*code+line[p]-'0'; else code=16*code+line[p]-'a'+10; p++; } code &= 255; while (line[p]==' ') p++; if (line[p]==0) { printf("no operator name: %s\n",line); continue; } s=&line[p]; while (line[p]!=' ' && line[p]!=0) p++; line[p]=0; insert(optable,OPHSIZE,s,code,type); } fclose(fp); } assemble(source,pass) char *source; int pass; { FILE *fp; int lc,i,hex1,hex2,hex3,p,f,v; int hex4,v4; char line[LINSIZE],list[LINSIZE],word[LINSIZE]; lc=0; if ((fp=fopen(source,"r")) == NULL) { printf("%s: open error\n",source); exit(); } while (getline(fp,line,list)) { errflag=' '; hex1=-1; hex2=-1; hex3=-1; hex4=-1; for (;;) { /* dummy loop */ if (line[0]=='*') break; p=0; while (line[p]==' ') p++; if (line[p]==0) break; hex1=lc; if (p==0) { i=0; while (line[p]!=' ' && line[p]!=0) word[i++]=line[p++]; word[i]=0; label(word,lc,pass); while (line[p]==' ') p++; } i=0; while (line[p]!=' ' && line[p]!=0) word[i++]=line[p++]; word[i]=0; f=search(optable,OPHSIZE,word); if (f<0) { errflag='o'; hex2=0; lc++; break; } if (optable[f].type!=ONEOP) { while (line[p]==' ') p++; if (line[p]==0) { errflag='f'; v=0; } else { i=0; while (line[p]!=' ' && line[p]!=0) word[i++]=line[p++]; word[i]=0; v=value(word); } } if (optable[f].type==ONEOP) { while (line[p]==' ') p++; if (line[p]==0) { errflag='f'; v4=0; } else { i=0; while (line[p]!=' ' && line[p]!=0) word[i++]=line[p++]; word[i]=0; v4=value1(word); } } switch (optable[f].type) { case ONEOP: hex2=optable[f].code; hex4=v4; lc++; break; case TWOOP: hex2=optable[f].code; hex3=v; lc+=2; break; case DCOP: hex2=v; lc++; break; case DSOP: lc+=v; break; } break; } if (pass==2) { printf("%c",errflag); puthex1(hex1); putchar(' '); puthex2(hex2); switch (optable[f].type) { case TWOOP: puthex1(hex3); break; case ONEOP: puthex2(hex4); break; } printf(" "); p=0; while (list[p]) putchar(list[p++]); putchar('\n'); } } fclose(fp); } getline(fp,line,list) FILE *fp; char line[LINSIZE],list[LINSIZE]; { int c,p; c=getc(fp); if (c==EOF) return(0); p=0; while (c!='\n') { if (p>=LINSIZE-1) { printf("too long line\n"); exit(); } if (c==EOF) { printf("last line incomplete\n"); return(0); } list[p]=c; if (c=='\t') c=' '; line[p++]=c; c=getc(fp); } list[p]=0; line[p]=0; return(1); } insert(table,modulo,s,code,type) struct tbl table[]; char *s; int modulo,code,type; { int h,hinit; h=hashfunc(s,modulo); hinit=h; while (table[h].name!=0) { if (equal(s,table[h].name)) { printf("double definition: %s\n",s); return; } h++; if (h>=modulo) h=0; if (h==hinit) { printf("table overflow\n"); exit(); } } table[h].name=&buff[bp]; while (*s) { if (bp>=BUFSIZE-1) { printf("character buffer overflow\n"); exit(); } buff[bp++]=*s++; } buff[bp++]=0; table[h].code=code; table[h].type=type; } search(table,modulo,s) struct tbl table[]; int modulo; char *s; { int h,hinit; h=hashfunc(s,modulo); hinit=h; while (table[h].name!=0) { if (equal(s,table[h].name)) return(h); h++; if (h>=modulo) h=0; if (h==hinit) break; } return(-1); } hashfunc(s,modulo) char *s; int modulo; { int hval; hval=0; while (*s) hval+=*s++; hval%=modulo; while (hval<0) hval+=modulo; return(hval); } label(s,lc,pass) char *s; int lc,pass; { int f; if (pass==1) insert(labtable,LBHSIZE,s,lc,0); else { f=search(labtable,LBHSIZE,s); if (f<0) printf("system error!!\n"); else if (lc!=labtable[f].code) errflag='l'; } } value(s) char *s; { int v,f; if (*s>='0' && *s<='9') { v=0; while (*s>='0' && *s<='9') v=10*v+(*s++)-'0'; if (*s!=0) errflag='f'; } else { f=search(labtable,LBHSIZE,s); if (f<0) { errflag='u'; v=0; } else v=labtable[f].code; } return(v&255); } value1(s) char *s; { int v,f; if (*s>='0' && *s<='9') { v=0; while (*s>='0' && *s<='9') v=10*v+(*s++)-'0'; if (*s!=0) errflag='f'; } if (*s>'9') { v=0; while (*s>'9') v=10*v+(*s++)-'a'+10; if (*s!=0) errflag='f'; } return(v&255); } equal(s1,s2) char *s1,*s2; { while (*s1 && *s1==*s2) { s1++; s2++; } return(*s1==*s2); } puthex(v) int v; { int u,l; if (v<0) printf(" "); else { v%=256; u=v/16; l=v%16; putchar((u<10) ? u+'0' : u+'a'-10); putchar((l<10) ? l+'0' : l+'a'-10); putchar(' '); } } puthex1(v) int v; { int u,l,w; if (v<0) printf(" "); else { v%=4096; w=v/256; u=v/16; l=v%16; putchar((w<10) ? w+'0' : w+'a'-10); putchar((u<10) ? u+'0' : u+'a'-10); putchar((l<10) ? l+'0' : l+'a'-10); putchar(' '); } } puthex2(v) int v; { int u,l; if (v<0) printf(" "); else { v%=256; u=v/16; l=v%16; /* putchar((u<10) ? u+'0' : u+'a'-10);*/ putchar((l<10) ? l+'0' : l+'a'-10); putchar(' '); } }