From d643dc3e5565647888c5c357d6ae163d50f9ee68 Mon Sep 17 00:00:00 2001 From: "(Tim) Efthimis Kritikos" Date: Wed, 14 Feb 2024 12:57:07 +0000 Subject: [PATCH] ASM: Added support for defining arbitrary double-words --- assembly.c | 103 +++++++++++++++++++++++++++++++---------------------- assembly.h | 2 +- main.c | 10 +++--- test.asm | 2 ++ 4 files changed, 68 insertions(+), 49 deletions(-) diff --git a/assembly.c b/assembly.c index 81334db..30cfc50 100644 --- a/assembly.c +++ b/assembly.c @@ -230,13 +230,13 @@ char *disassemble(uint32_t opcode_be){ return ret; } -int32_t parse_immediate(char* str,struct assembler_context_t *assembler_context){ +int64_t parse_immediate(char* str,struct assembler_context_t *assembler_context){ if(*str=='0'){ uint32_t addr; - if(sscanf(str,"%06X",&addr)!=1) + if(sscanf(str,"0x%08X",&addr)!=1){ return -1; - else - return addr&0x00FFFFFF; + }else + return addr; }else{ // Get name size int len=0; @@ -259,7 +259,7 @@ int32_t parse_immediate(char* str,struct assembler_context_t *assembler_context) } } -uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context){ +int64_t assemble_line(char *line, struct assembler_context_t *assembler_context){ uint16_t opcode; int x; int call; @@ -290,26 +290,26 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context opcode|=0x09; x++; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; while(line[x]==' ')x++; if(line[x]=='$'){ x++; - int32_t imm=parse_immediate(line+x,assembler_context); + int64_t imm=parse_immediate(line+x,assembler_context); if(imm<0){ switch(imm){ default: - return 0xFFFFFFFF; + return -2; case -2: - return 0xFFFFFFFE; + return -3; } }else return opcode<<24|(imm&0x00FFFFFF); }else - return 0xFFFFFFFF; + return -2; }else{ uint8_t r1,r0,params; if(strncmp(line,"ADD ",4)==0){ @@ -356,21 +356,21 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context r1=line[x]-'0'; return (opcode<<16)|r0<<8|r1; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return (opcode<<16)|r0<<8; + return (opcode<<16)|r0<<2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else if(strncmp(line,"MOV ",4)==0){ uint32_t data; x=3; @@ -399,23 +399,23 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context else if(line[x]=='h') return 0x41000000|r1<<16|(data&0xFFFF); else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else if(line[x]=='S'){ x++; if(line[x]=='P'){ return 0x0F000000|(data&0x00FFFFFF); }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else if(line[x]=='%'){ x++; if(line[x]=='R'){ @@ -441,34 +441,34 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context if(line[x]==')'){ return 0x20080000|(r0&0xFF)<<8|(r1&0xFF); }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else - return 0xFFFFFFFF; + return -2; }else if(strncmp(line,"HALT ",5)==0||strncmp(line,"HALT\0",5)==0){ x=4; while(line[x]==' ')x++; if(line[x]==0) return 0x20050000; else - return 0xFFFFFFFF; + return -2; }else if(strncmp(line,"RET ",4)==0||strncmp(line,"RET\0",4)==0){ return 0x10000000; }else if(*line==':'){ - return 0xFFFFFFF0; + return -1; }else if(strncmp(line,"PUSH ",5)==0){ x=4; while(line[x]==' ')x++; @@ -479,10 +479,10 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context if(line[x]>='0'&&line[x]<='7') return 0x20060000|(line[x]-'0')<<8; else - return 0xFFFFFFFF; + return -2; } }else - return 0xFFFFFFFF; + return -2; }else if(strncmp(line,"POP ",4)==0){ x=3; while(line[x]==' ')x++; @@ -493,12 +493,29 @@ uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context if(line[x]>='0'&&line[x]<='7') return 0x20070000|(line[x]-'0')<<8; else - return 0xFFFFFFFF; + return -2 ; } }else - return 0xFFFFFFFF; + return -2; + }else if(strncmp(line,"DDW ",4)==0){ + x=4; + while(line[x]==' ')x++; + if(line[x]=='$'){ + x++; + int64_t imm=parse_immediate(line+x,assembler_context); + if(imm<0){ + switch(imm){ + default: + return -2; + case -2: + return -3; + } + }else + return imm; + }else + return -2; } - return 0xFFFFFFFF; + return -2;//TODO: figure out which statemnt falls through to this } } diff --git a/assembly.h b/assembly.h index 52b61cf..b639794 100644 --- a/assembly.h +++ b/assembly.h @@ -13,7 +13,7 @@ struct assembler_context_t{ uint32_t current_location; }; -uint32_t assemble_line(char *line, struct assembler_context_t *assembler_context); +int64_t assemble_line(char *line, struct assembler_context_t *assembler_context); int assembler_context_process(char* line,struct assembler_context_t *tofree); struct assembler_context_t *malloc_assembler_context(); diff --git a/main.c b/main.c index 25534b7..ae6bcda 100644 --- a/main.c +++ b/main.c @@ -92,7 +92,7 @@ int main(int argc, char* argd[] ){ int linec=0; if (rom != NULL) { char line [1000]; - uint32_t opcode; + int64_t opcode; uint32_t addr=0; //--------------- Build assembler context -------------------- @@ -126,16 +126,16 @@ int main(int argc, char* argd[] ){ opcode=assemble_line(line,assembler_context); - if(opcode>=0xFFFFFFF1){ - if(opcode==0xFFFFFFFE) + if(opcode<-1){ + if(opcode==-3) printf("Error label not found %s:%d\n", infile,linec+1); else - printf("Error assembling %s:%d\n", infile,linec+1); + printf("Error assembling (0x%lx)%s:%d\n",(uint64_t)opcode, infile,linec+1); fclose(rom); free_assembler_context(assembler_context); free_simdata(simdata); return 1; - }else if(opcode==0xFFFFFFF0){ + }else if(opcode==-1){ ;/*it was empty, comment or label*/ }else{ simdata->RAM[addr]=(0xFF000000&opcode)>>24; diff --git a/test.asm b/test.asm index 4a3a04d..ce71671 100644 --- a/test.asm +++ b/test.asm @@ -49,3 +49,5 @@ MOV $0xcccd,%R1l MOV $0x3f4c,%R1h MOV %R1,(%R0) HALT +:TR_LOW +DDW $0xDEADBEEF