yylex();
writeint(PUSH, outfp);
writeint(atoi(yytext), outfp);
- current_offset++; /* we're pushing to values to the file, so update
- current_offset accordingly
- */
break;
case T_POP:
- yylex();
writeint(POP, outfp);
- writeint(atoi(yytext), outfp);
break;
case T_DPRINT:
writeint(l->offset, outfp);
break;
+ case T_PEEK:
+ t = yylex();
+ if (t == WORD) {
+ l = search_label(yytext);
+ if (l == NULL)
+ fail("the label %s doesn't exists", yytext);
+ val = l->offset;
+ } else if (t == NUMBER) {
+ val = atoi(yytext);
+ }
+
+ writeint(PEEK, outfp);
+ writeint(val, outfp);
+ break;
+
+ case T_POKE:
+ t = yylex();
+ if (t == WORD) {
+ l = search_label(yytext);
+ if (l == NULL)
+ fail("the label %s doesn't exists", yytext);
+
+ printf("%s:%d", l->name, l->offset);
+ val = l->offset;
+ } else if (t == NUMBER) {
+ val = atoi(yytext);
+ }
+
+ writeint(POKE, outfp);
+ writeint(val, outfp);
+ break;
+
+
case T_DB:
yylex();
val = atoi(yytext);
writeint(val, outfp);
break;
-
-
default:
fail("unexpected %s in input at line %d", yytext, yylineno);
break;
"jmp" { return T_JMP; }
"db" { return T_DB; }
"times" { return T_TIMES; }
+"peek" { return T_PEEK; }
+"poke" { return T_POKE; }
[ \t\n]+ /* ignore whitespace */
[0-9]+ { return NUMBER; }
#include "vm.h"
+enum modes {RUN_MODE, DUMP_MODE};
+
int filelength(FILE *fp) {
if (fp == NULL)
return -1;
}
int main(int argc, char **argv) {
- if (argc != 2) {
+ if (argc != 2 && argc != 3) {
puts("vm file.bin");
+ puts("vm -d file.bin");
exit(0);
}
- FILE *fp = fopen(argv[1], "r");
+ FILE *fp;
+ int mode;
+
+ if (strcmp(argv[1], "-d") == 0)
+ mode = DUMP_MODE;
+ else
+ mode = RUN_MODE;
+
+ if (mode == DUMP_MODE)
+ fp = fopen(argv[2], "r");
+ else
+ fp = fopen(argv[1], "r");
+
if (fp == NULL)
error("unable to find the requested file");
load_file(v, fp);
printf("vm launched, memsize: %d, stacksize: %d\n", v->memsize, v->stacksize);
- run(v);
+
+ if (mode == DUMP_MODE)
+ dump_mem(v);
+ else
+ run(v);
}
T_JMP,
T_DB,
T_TIMES,
+ T_PEEK,
+ T_POKE,
};
fread(v->mem, sizeof(int), v->memsize, fd);
}
+int dump_mem(struct vm *v)
+{
+ /* display the contents of the memory of a vm */
+ int offset;
+ for(offset = 0; offset < v->memsize - 3; offset += 4) {
+ printf("%xh:\t%d\t%d\t%d\t%d\n", offset, v->mem[offset], v->mem[offset+1],
+ v->mem[offset+2], v->mem[offset+3]);
+ }
+
+ for (; offset < v->memsize; offset++) {
+ printf("%d\t", v->mem[offset]);
+ }
+}
+
int run(struct vm *v)
{
if (v == NULL)
return -1;
int *ip = v->mem;
+ int *addr; /* a temporary address */
while(ip < v->mem + v->memsize) {
switch(*ip) {
case NOP:
break;
case RET:
- /* get ip from the stack. it must be the thing just before
- * the top of the stack - it implies that programs must return
- * one and only one return value
- */
if (v->callstackoffset < 0)
fail("call stack underflow");
- ip = v->callstack[v->callstackoffset];
+ ip = (int *) v->callstack[v->callstackoffset];
v->callstackoffset--;
break;
ip = v->mem + (*ip) - 1; /* because the pointer gets automatically incremented */
break;
+ case PEEK:
+ ip++;
+ v->stackoffset++;
+ if (v->stackoffset >= v->stacksize)
+ fail("peek: stack overflow");
+
+ v->stack[v->stackoffset] = *(v->mem + *ip);
+ break;
+
+ case POKE:
+ ip++;
+ if (v->stackoffset < 0)
+ fail("poke: stack underflow");
+
+ addr = v->mem + *ip;
+ *addr = v->stack[v->stackoffset];
+ break;
+
}
ip++;
#include <stdlib.h>
#include <stdio.h>
-enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, JMP, RET, DPRINT, INC, DEC};
+enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, JMP, RET, DPRINT, INC, DEC, PEEK, POKE};
struct vm {
int *mem;