From eb33736430b2dd7e88d953983c611c1402619498 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Tue, 27 Dec 2016 04:48:25 +0100 Subject: [PATCH] Implemented on-the-fly register interpretation --- debug.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------- disasm.c | 2 +- main.c | 1 + scripts/process_disasm.sh | 12 ------------ 4 files changed, 58 insertions(+), 33 deletions(-) delete mode 100755 scripts/process_disasm.sh diff --git a/debug.c b/debug.c index bc6dad1..4e1d37a 100644 --- a/debug.c +++ b/debug.c @@ -118,6 +118,7 @@ void query() fprintf(stderr,"Loaded state as %s",filename); break; case 'w': + //FIXME ALREADY sscanf(input,"w %d %d\n",®,&value); fprintf(stderr,"Setting register %d to %d",reg,value); regs[reg] = value; @@ -152,72 +153,107 @@ uint16_t fetch_debug() return value; } -void print_instruction(uint16_t opcode, uint16_t arg1, uint16_t arg2, uint16_t arg3) +void translate_args(uint16_t a1, uint16_t a2, uint16_t a3, char *arg1, char *arg2, char *arg3) { + if(a1 > MAX_INT) + { + sprintf(arg1,"r%d",a1-(MAX_INT+1)); + } + else + { + sprintf(arg1,"%d",a1); + } + + if(a2 > MAX_INT) + { + sprintf(arg2,"r%d",a2-(MAX_INT+1)); + } + else + { + sprintf(arg2,"%d",a2); + } + + if(a3 > MAX_INT) + { + sprintf(arg3,"r%d",a3-(MAX_INT+1)); + } + else + { + sprintf(arg3,"%d",a3); + } + +} + +void print_instruction(uint16_t opcode, uint16_t a1, uint16_t a2, uint16_t a3) +{ + char arg1[10]; + char arg2[10]; + char arg3[10]; + translate_args(a1,a2,a3,arg1,arg2,arg3); switch(opcode) { case HALT: fprintf(stderr,"HALT\n"); break; case MOV: - fprintf(stderr,"MOV %d %d\n",arg1,arg2); + fprintf(stderr,"MOV %s %s\n",arg1,arg2); break; case PUSH: - fprintf(stderr,"PUSH %d\n",arg1); + fprintf(stderr,"PUSH %s\n",arg1); break; case POP: - fprintf(stderr,"POP %d\n",arg1); + fprintf(stderr,"POP %s\n",arg1); break; case TEQ: - fprintf(stderr,"TEQ %d %d %d\n",arg1,arg2,arg3); + fprintf(stderr,"TEQ %s %s %s\n",arg1,arg2,arg3); break; case TGT: - fprintf(stderr,"TGT %d %d %d\n",arg1,arg2,arg3); + fprintf(stderr,"TGT %s %s %s\n",arg1,arg2,arg3); break; case JMP: - fprintf(stderr,"JMP %d\n",arg1); + fprintf(stderr,"JMP %s\n",arg1); break; case JNZ: - fprintf(stderr,"JNZ %d %d\n",arg1,arg2); + fprintf(stderr,"JNZ %s %s\n",arg1,arg2); break; case JZ: - fprintf(stderr,"JZ %d %d\n",arg1,arg2); + fprintf(stderr,"JZ %s %s\n",arg1,arg2); break; case ADD: - fprintf(stderr,"ADD %d %d %d\n",arg1,arg2,arg3); + fprintf(stderr,"ADD %s %s %s\n",arg1,arg2,arg3); break; case MUL: - fprintf(stderr,"MUL %d %d %d\n",arg1,arg2,arg3); + fprintf(stderr,"MUL %s %s %s\n",arg1,arg2,arg3); break; case MOD: - fprintf(stderr,"MOD %d %d %d\n",arg1,arg2,arg3); + fprintf(stderr,"MOD %s %s %s\n",arg1,arg2,arg3); break; case AND: - fprintf(stderr,"AND %d %d %d\n",arg1,arg2,arg3); + fprintf(stderr,"AND %s %s %s\n",arg1,arg2,arg3); break; case OR: - fprintf(stderr,"OR %d %d %d\n",arg1,arg2,arg3); + fprintf(stderr,"OR %s %s %s\n",arg1,arg2,arg3); break; case NOT: - fprintf(stderr,"NOT %d %d\n",arg1,arg2); + fprintf(stderr,"NOT %s %s\n",arg1,arg2); break; case LOAD: - fprintf(stderr,"LOAD %d %d\n",arg1,arg2); + fprintf(stderr,"LOAD %s %s\n",arg1,arg2); break; case STOR: - fprintf(stderr,"STOR %d %d\n",arg1,arg2); + fprintf(stderr,"STOR %s %s\n",arg1,arg2); break; case CALL: - fprintf(stderr,"CALL %d\n",arg1); + fprintf(stderr,"CALL %s\n",arg1); break; case RET: fprintf(stderr,"RET\n"); break; case OUT: - fprintf(stderr,"OUT %d\n",arg1); + fprintf(stderr,"OUT %s\n",arg1); break; case IN: - fprintf(stderr,"IN %d\n",arg1); + fprintf(stderr,"IN %s\n",arg1); break; case NOP: fprintf(stderr,"NOP\n"); diff --git a/disasm.c b/disasm.c index 8a6b84f..03b255d 100644 --- a/disasm.c +++ b/disasm.c @@ -11,7 +11,7 @@ void disassemble_program(uint32_t length) { break; } - printf("%d: ",pc); + fprintf(stderr,"%d: ",pc); uint16_t opcode = fetch(); decode_instruction(opcode,&arg1,&arg2,&arg3); print_instruction(opcode,arg1,arg2,arg3); diff --git a/main.c b/main.c index 73cd479..4e93d9c 100644 --- a/main.c +++ b/main.c @@ -29,6 +29,7 @@ void startvm(char *program) void disasm(char *program) { + stderr = stdout; disassemble_program(load_program(program)); } diff --git a/scripts/process_disasm.sh b/scripts/process_disasm.sh deleted file mode 100755 index 15f5044..0000000 --- a/scripts/process_disasm.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -PROGRAM="$1" - -sed -i "s/32768/r0/g" "$PROGRAM" -sed -i "s/32769/r1/g" "$PROGRAM" -sed -i "s/32770/r2/g" "$PROGRAM" -sed -i "s/32771/r3/g" "$PROGRAM" -sed -i "s/32772/r4/g" "$PROGRAM" -sed -i "s/32773/r5/g" "$PROGRAM" -sed -i "s/32774/r6/g" "$PROGRAM" -sed -i "s/32775/r7/g" "$PROGRAM" -- libgit2 0.22.2