|
|
DataMuseum.dkPresents historical artifacts from the history of: Commodore CBM-900 |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about Commodore CBM-900 Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - download
Length: 4453 (0x1165)
Types: TextFile
Notes: UNIX file
Names: »asm.c«
└─⟦f27320a65⟧ Bits:30001972 Commodore 900 hard disk image with partial source code
└─⟦0a3c255ba⟧ UNIX Filesystem
└─⟦this⟧ »assm/asm.c«
#include <stdio.h>
#include "opcode.c"
char line_buf[255];
unsigned int pc;
FILE *fopen();
FILE *fp0, *fp1;
int dlab, ulab;
typedef struct label { /* structure for label */
char *lbl;
unsigned int lbl_add; /* 16 bit address */
unsigned int flag; /* flag = 0, defined */
} LABEL; /* flag = 1, undefined */
LABEL *lbl_lst, *def_label, *udef_label;
main()
{
register int i;
char file[32];
printf("\033[E");
printf("\n\n8085 Cross Assembler\n\n\n\n");
printf("Source file: ");
scanf("%s", file);
if((fp0 = fopen(file, "r")) == NULL) {
panic("\n\7File not found\n\7\n");
}
def_label = (LABEL *) malloc(3000 * sizeof (LABEL)); /* 3000 total */
udef_label = (LABEL *) malloc(1000 * sizeof (LABEL)); /* 1000 here */
pass1();
printf("\n\n# labels = %d, pc = %d\n", dlab, pc);
for(i = 0; i < dlab; i++) {
printf("Label = |%s|, value = |%d|\n", def_label[i].lbl,
def_label[i].lbl_add);
}
pass2();
printf("Assembly complete.\n");
fclose(fp0);
}
/*
* Pass 1 code
*/
pass1()
{
register int i,ii;
pc = 0; /* initial PC counter */
dlab = 0; /* no defined labels */
ulab = 0; /* no undefined labels */
for(;;) {
ii = get_line();
if (ii == 0 && line_buf[0] != 0) {
printf("line point = %d\n", &line_buf[0]);
if (parse(&line_buf[0]) == 0xFF)
return(0); /* done - return */
printf("line point = %d\n", &line_buf[0]);
printf("%s\n", line_buf);
}
}
}
/*
* Pass 2 code
*/
pass2()
{
}
/*
* get a line from the file.
*/
get_line()
{
char c, *tmp;
register int cnt;
tmp = &line_buf[0]; /* point tmp @ buffer */
cnt = 0;
*tmp = 0; /* set NULL at start */
while (( c=getc(fp0)) != EOF) {
if(((c == 0x3B) || (c == 0x0A)) && (cnt == 0)) {
if(c == 0x3B)
while((c=getc(fp0)) != EOF) {
if(c == 0x0A)
return(1);
}
return(1);
}
cnt++; /* bump char count */
if(c == 0x3B ) { /* chuck rest of line */
while ((c = getc(fp0)) != EOF) {
if(c == 0x0A) {
*tmp++ = 0;
return(0);
}
}
panic("Pass 1: Unexpected EOF\n");
}
if ( c == 0x0A ) {
*tmp++ = 0;
return(0); /* got line - return */
}
*tmp++ = c; /* c into buf & inc pointer */
/* bump counter */
if (cnt == 255)
panic("Pass 1: Bogus source file!\n");
}
panic("Pass 1: Unexpected EOF\n");
}
/*
* parse the line
*/
parse(buf)
char *buf;
{
int i, cnt, end;
char opcd[6]; /* temp opcode buffer */
/* char param[20]; temp parameter buffer */
printf("Parse: %d, %s\n", buf, buf);
i = 0;
cnt = 0;
end = 0;
if (*buf !=0x20 && *buf != 0x09)
buf = get_label(i, buf);
while((*buf == 0x20) || (*buf == 0x09))
*buf++;
printf("PARSE 2:%s\n", buf);
buf = get_op(buf, &end); /* get opcode */
/* and necessary param. */
if(end == 0xFF)
return(0xFF);
printf("End Parse:\n");
return(0);
}
/*
* Get a defined label - i.e. first label in a line.
*/
get_label(i, buf)
int i;
char *buf;
{
char tmp[10]; /* temp label storage */
i = 0;
printf("Label: %d \n", buf);
while(*buf != 0x20 && *buf != 0x09) {
tmp[i] = *buf++;
if (i == 11)
panic("get_label: label error\n");
i++;
}
tmp[i] = 0;
add_d(&tmp[0]);
def_label[dlab].lbl_add = pc; /* set value to current PC */
def_label[dlab].flag = 0; /* show as defined */
printf("End Label: %s\n" ,def_label[dlab].lbl);
dlab++; /* bump count */
printf(" return now\n");
return(buf);
}
/*]
* add a defined label to structure
*/
add_d(tmp)
char *tmp;
{
/* check for overflow */
def_label[dlab].lbl = (char *) malloc((unsigned) strlen(tmp) + 1);
strcpy(def_label[dlab].lbl, tmp);
}
/*
* get opcode
*/
get_op(buf, endflg)
char *buf;
int *endflg;
{
register int i;
char tmpop[6];
i = 0;
printf("OP: point = %d buf = |%s|\n", buf, buf);
while(*buf != 0x00 && *buf != 0x20 && *buf != 0x09 && *buf !=0x0A) {
tmpop[i] = *buf++;
if (i == 6) {
printf("tmp op = |%s| i = %d b point = %d\n", tmpop
, i, buf);
panic("get_op: op error\n");
}
i++;
}
tmpop[i] = 0;
printf("tmpop = %s\n", tmpop);
for(i = 0; ; i++) {
if (strcmp(op_list[i].mn, "LAST") == 0) /* bad opcode? */
panic("s/w error\n");
if (strcmp(op_list[i].mn, &tmpop[0]) == 0) {
printf("Match = %x\n", op_list[i].val);
pc = pc + (op_list[i].param & 0x03);
*endflg = op_list[i].param;
return(buf);
}
}
printf("Why here? %d\n", i);
return(buf);
}
/*
* Error exit.
*/
panic(msg)
char *msg;
{
printf("%s", msg);
exit(1);
}