From fc7102f40a5badfacfd25b3b268f8ef4bd469efc Mon Sep 17 00:00:00 2001 From: glenda Date: Sat, 20 Aug 2022 09:49:30 +0000 Subject: [PATCH] Wrote a bunch more junk --- asm.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 79 insertions(+), 15 deletions(-) diff --git a/asm.c b/asm.c index c0377b0..19739c6 100644 --- a/asm.c +++ b/asm.c @@ -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) { @@ -67,6 +95,48 @@ gettoken(char **t, char *l) return len; } +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) { @@ -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; -- 2.45.2