diff --git a/assemble.c b/assemble.c index c7d0d91..3d7b94b 100644 --- a/assemble.c +++ b/assemble.c @@ -43,7 +43,7 @@ int main(int argc, char* argv[]){ generateBaseMacros(macroTable); - int lineNumber = 0; + int lineNumber = -1; ParseLabelsMacros(file,labelTable,macroTable,&lineNumber); @@ -89,6 +89,10 @@ void assemble(FILE* file,FILE* bin,Label** labelTable,Macro** macroTable){ /* note that fgets don't strip the terminating \n, checking its presence would allow to handle lines longer that sizeof(line) */ + if(line[1] == '\n' || line[0] == '#'){ + continue; + } + line[strlen(line)-2] = '\0'; //get rid of \n character at end of line token = strtok(line," "); @@ -104,6 +108,8 @@ void assemble(FILE* file,FILE* bin,Label** labelTable,Macro** macroTable){ } else if(strcmp(token,"def") == 0){ macroDef = 1; continue; + } else if(strcmp(token,"label") == 0){ + continue; } else if(macroDef == 1){ if(strcmp(token,"end") == 0){ macroDef = 0; @@ -114,7 +120,7 @@ void assemble(FILE* file,FILE* bin,Label** labelTable,Macro** macroTable){ opcode = getOpcode(token); InstructionType instrType = getInstructionType(opcode); - if(opcode == 17 && strcmp(token,"label") != 0){ // if it is not a base instruction, it is a macro or label + if(opcode == 17){ // if it is not a base instruction or a label, it is a macro macroInstr.instruction32 = 0; macro = getMacro(macroTable,token); for(int i = 0; i < macro->argSize; i++){ diff --git a/labelFuncs.c b/labelFuncs.c index 8d2a1e8..f8e3299 100644 --- a/labelFuncs.c +++ b/labelFuncs.c @@ -64,6 +64,11 @@ void ParseLabelsMacros(FILE* file,Label** labelTable,Macro** macroTable,int* lin uint8_t constArg8; uint16_t constArg16; while(fgets(line,sizeof(line),file)){ + + if(line[1] == '\n' || line[0] == '#'){ + continue; + } + line[strlen(line)-2] = '\0'; token = strtok(line," "); @@ -107,8 +112,7 @@ void ParseLabelsMacros(FILE* file,Label** labelTable,Macro** macroTable,int* lin token = strtok(NULL," "); Label* label = initLabel(token,*lineNumber); insertLabel(label,labelTable); - (*lineNumber)++; - } else if(strcmp(token,"include") == 0){ + } else if(strcmp(token,"inc") == 0){ token = strtok(NULL," "); header = fopen(token,"r"); ParseLabelsMacros(header,labelTable,macroTable,lineNumber); diff --git a/run.c b/run.c index 2052d10..475cce7 100644 --- a/run.c +++ b/run.c @@ -42,15 +42,24 @@ int main(int argc,char* argv[]){ if(opcode == UINT8_MAX)break; executeInstruction(opcode,registers,pc,sp,memory,programBegin); *pc+=1; - printf("Registers: "); - for(int i =0;i<32;i++) - printf("%u,",registers[i]); - printf("\nStack: "); - for(int i =0;i<10;i++){ - printf("%u,",memory[1000+i]); - } - getchar(); + //printf("Registers: "); + //for(int i =0;i<32;i++) + // printf("%u,",registers[i]); + //printf("\nStack: "); + //for(int i =0;i<10;i++){ + // printf("%u,",memory[1000+i]); + //} + //getchar(); } + printf("Registers: "); + for(int i =0;i<32;i++){ + printf("%u,",registers[i]); + } + printf("\nStack: "); + for(int i =0;i<10;i++){ + printf("%u,",memory[1000+i]); + } + printf("\n"); return 0; } @@ -60,31 +69,38 @@ void executeInstruction(uint8_t opcode,uint32_t registers[],uint32_t* pc, uint32 uint8_t rA = memory[*pc+programBegin] >> 8; uint8_t rB = memory[*pc+programBegin] >> 16; uint8_t rC = memory[*pc+programBegin] >> 24; - printf("Program Counter: %u\n",*pc); + //printf("Program Counter: %u\n",*pc); uint8_t imm8 = rC; uint16_t imm16 = ((uint16_t)rC << 8) | rB; uint32_t trash = 0; switch(opcode){ case NOP: + //printf("nop\n"); break; case ADD: registers[rA] = (uint32_t)registers[rB] + (uint32_t)registers[rC]; + //printf("add %d %d %d\n",rA,rB,rC); break; case SUB: registers[rA] = (uint32_t)registers[rB] - (uint32_t)registers[rC]; + //printf("sub %d %d %d\n",rA,rB,rC); break; case AND: registers[rA] = registers[rB] & registers[rC]; + //printf("and %d %d %d\n",rA,rB,rC); break; case ORR: registers[rA] = registers[rB] | registers[rC]; + //printf("orr %d %d %d\n",rA,rB,rC); break; case XOR: registers[rA] = registers[rB] ^ registers[rC]; + //printf("xor %d %d %d\n",rA,rB,rC); break; case NOT: registers[rA] = ~registers[rB]; + //printf("not %d %d\n",rA,rB); break; case LSH: if(registers[rC] >= 32 && registers[rC] <= -32){ //signed - unsigned comparison is weird @@ -94,6 +110,7 @@ void executeInstruction(uint8_t opcode,uint32_t registers[],uint32_t* pc, uint32 } else { registers[rA] = registers[rB] << (int32_t)registers[rC]; } + //printf("lsh %d %d %d\n",rA,rB,rC); break; case ASH: if(registers[rC] >= 32 || registers[rC] <= -32){ @@ -103,6 +120,7 @@ void executeInstruction(uint8_t opcode,uint32_t registers[],uint32_t* pc, uint32 } else { registers[rA] = registers[rB] << registers[rC]; } + //printf("ash %d %d %d\n",rA,rB,rC); break; case TCU: if(registers[rB] == registers[rC]){ @@ -112,6 +130,7 @@ void executeInstruction(uint8_t opcode,uint32_t registers[],uint32_t* pc, uint32 } else { registers[rA] = -1; } + //printf("tcu %d %d %d\n",rA,rB,rC); break; case TCS: if(registers[rB] == registers[rC]){ @@ -121,24 +140,31 @@ void executeInstruction(uint8_t opcode,uint32_t registers[],uint32_t* pc, uint32 } else { registers[rA] = -1; } + //printf("tcs %d %d %d\n",rA,rB,rC); break; case SET: registers[rA] = ((int16_t)imm16); + //printf("set %d %d\n",rA,rB); break; case MOV: registers[rA] = registers[rB]; + //printf("mov %d %d\n",rA,rB); break; case LDW: registers[rA] = memory[registers[rB]]; + //printf("ldw %d %d\n",rA,rB); break; case STW: memory[registers[rA]] = registers[rB]; + //printf("stw %d %d\n",rA,rB); break; case LDB: registers[rA] = (uint8_t)(memory[registers[rB]]); + //printf("ldb %d %d\n",rA,rB); break; case STB: memory[registers[rA]] = (uint8_t)(registers[rB]); + //printf("stb %d %d\n",rA,rB); break; } return; diff --git a/test.c b/test.c index 57e7a4d..17011b9 100644 --- a/test.c +++ b/test.c @@ -4,57 +4,20 @@ #include #include "error.h" -#include "macroFuncs.c" int main(int argc, char* argv[]){ printf("hi1\n"); - Macro* macroTable[macroTableSize]; - for(int i = 0; i < macroTableSize;i++) - macroTable[i] = NULL; - generateBaseMacros(macroTable); - - Macro* add5 = (Macro*)malloc(sizeof(Macro)); - add5->name = "add5"; - add5->args = (uint8_t*)malloc(sizeof(uint8_t)); - add5->args[0] = 0; - add5->argSize = 1; - add5->instruction.instruction32 = 0; - add5->instrType = OP_RA; - add5->submacros = initVec(); - add5->link = NULL; - - appendToVec(add5->submacros,copyMacro(getMacro(macroTable,"set"))); - appendToVec(add5->submacros,copyMacro(getMacro(macroTable,"add"))); - - add5->args[0] = RA; - add5->submacros->arr[0]->instruction.instruction32 += (asm_AT << 8); - add5->submacros->arr[0]->instruction.instruction32 += (5 << 16); - add5->submacros->arr[0]->args[0] = NONE; - add5->submacros->arr[0]->args[1] = NONE; - add5->submacros->arr[1]->instruction.instruction32 += (asm_AT << 24); - add5->submacros->arr[1]->args[0] = RA; - add5->submacros->arr[1]->args[1] = RA; - add5->submacros->arr[1]->args[2] = NONE; - - - FILE* bin; - bin = fopen("hello","wb"); - - int input = atoi(argv[1]); - - Instruction instr; - instr.instruction32 = 0; - instr.opra.ra = input; - fputc(0x0b,bin); - fputc(input,bin); - fputc(atoi(argv[2]),bin); - fputc(0x0,bin); - writeMacro(add5,instr,bin); + char line[256]; - fclose(bin); + FILE* file = fopen("hi.txt", "r"); + while(fgets(line, sizeof(line),file)){ + printf("|%d|\n",line[1] == '\n'); + printf("[%c]",line[0]); + printf("%s",line); + } printf("hi2\n");