~grimmware/lc3

fc7102f40a5badfacfd25b3b268f8ef4bd469efc — glenda 2 years ago 5e4a128
Wrote a bunch more junk
1 files changed, 79 insertions(+), 15 deletions(-)

M asm.c
M asm.c => asm.c +79 -15
@@ 6,14 6,15 @@
#define MAXTOKEN 32
#define MAXSYMBOL 128
#define MAXARGS 3
#define MEMORY 32

typedef struct Line Line;
struct Line {
	char	label[MAXTOKEN];
typedef struct Inst Inst;
struct Inst {
	u16int	op;
	u16int	arg1;
	u16int	arg2;
	u16int	arg3;
	u16int	argc;
	u16int	mode;
	u16int	arg[3];
	char	*label;
};

typedef struct Symbol Symbol;


@@ 23,9 24,11 @@ struct Symbol {
};

Symbol symtab[MAXSYMBOL];
Inst *mem[MEMORY];

int debug = 0;
int symcount = 0;
int memc = 0;
int lineno = 1;
u16int orig, pc = 0x3000;
char *asmfile;


@@ 45,10 48,35 @@ void
	if(p != 0)
		return p;
	fprint(2, "Could not allocate memory!\n");
	exits("Could not allocate memory!");
	exits("Could not allocate mem!");
	return p;
}

void
syntaxerr(char* msg)
{
	fprint(
		2,
		"%s:%d Syntax error: %s\n",
		asmfile,
		lineno,
		msg
	);
	exits("Syntax error");
}

void
newinst(void)
{
	mem[memc] = emalloc(sizeof(Inst));
	mem[memc]->argc = 0;
	for(int i = 0; i<3; i++)
		mem[memc]->arg[i] = 0;
	mem[memc]->mode = 0;
	mem[memc]->label = nil;
	return;
}

int
gettoken(char **t, char *l)
{


@@ 68,6 96,48 @@ gettoken(char **t, char *l)
}

int
findreg(char* name)
{
	for(int i = 0; i < R_COUNT; i++){
		if(!strcmp(name, regname[i]))
			return i;
	}
	return -1;
}

void
newnop(void)
{
	newinst();
	mem[memc]->op = OP_BR;
	mem[memc]->argc+=2;
}

/* FIXME: Should pass in string split into 3 args and then write something to parse a hex value when not a register.
void
newadd(char* arg[])
{
	int i;

	newinst();
	mem[memc]->op = OP_ADD;
	for(int j = 0; j<2; j++){
		if((i = findreg(arg[j])) < 0)
			syntaxerr(smprint("%s is not a register"));
		mem[memc]->arg[j] = i;
	}
	if((i = findreg(arg[j])) < 0)
		mem[memc]->arg[j] = PARSE IMM;
	
}
*/

void
newaddimm(char* dr, char* sr1, char* imm5)
{
}

int
parseline(char *l)
{
	int pos = 0;


@@ 108,18 178,12 @@ parseline(char *l)
					print("updated origin to %x\n", origin);
				return 0;
			} else if(strcmp(t,"NOP") == 0) {
				newnop();
				pc++;
				pos++;
				return 1;
			} else {
				fprint(
					2,
					"%s:%d Invalid instruction or directive: %s\n",
					asmfile,
					lineno,
					t
				);
				exits("Syntax error");
				syntaxerr(smprint("Invalid instruction %s", t));
			}
		} else if(pos==2){
			return 1;