Fixes bugs in assembler, supervm and testcode.

This commit is contained in:
Felix Queißner 2016-05-21 13:34:14 +02:00
parent 11d27e0039
commit f7bc2624a0
3 changed files with 51 additions and 81 deletions

View file

@ -32,7 +32,7 @@ namespace supervm_asm
for(int i = 0; i < code.Length; i++) for(int i = 0; i < code.Length; i++)
{ {
var bits = BitConverter.GetBytes(code[i]); var bits = BitConverter.GetBytes(code[i]);
if(BitConverter.IsLittleEndian) if(BitConverter.IsLittleEndian == false)
{ {
bits = bits.Reverse().ToArray(); bits = bits.Reverse().ToArray();
} }
@ -49,13 +49,14 @@ namespace supervm_asm
var portions = new [] var portions = new []
{ {
new { Start = 0, Length = 32, Color = ConsoleColor.Red }, new { Start = 0, Length = 32, Color = ConsoleColor.Red },
new { Start = 32, Length = 2, Color = ConsoleColor.Blue }, new { Start = 32, Length = 2, Color = ConsoleColor.DarkGreen },
new { Start = 34, Length = 1, Color = ConsoleColor.Green }, new { Start = 34, Length = 1, Color = ConsoleColor.Green },
new { Start = 35, Length = 16, Color = ConsoleColor.Magenta }, new { Start = 35, Length = 16, Color = ConsoleColor.Magenta },
new { Start = 51, Length = 8, Color = ConsoleColor.Yellow }, new { Start = 51, Length = 6, Color = ConsoleColor.Yellow },
new { Start = 59, Length = 1, Color = ConsoleColor.DarkCyan }, new { Start = 57, Length = 1, Color = ConsoleColor.DarkCyan },
new { Start = 60, Length = 2, Color = ConsoleColor.Cyan }, new { Start = 58, Length = 2, Color = ConsoleColor.Cyan },
new { Start = 62, Length = 2, Color = ConsoleColor.DarkBlue }, new { Start = 60, Length = 2, Color = ConsoleColor.DarkBlue },
new { Start = 62, Length = 2, Color = ConsoleColor.Blue },
}; };
var fg = Console.ForegroundColor; var fg = Console.ForegroundColor;

View file

@ -1,8 +1,8 @@
 push 0 ; Some comment  push 0 ; Some comment
loop: loop:
dup dup
[f:yes] sub 10 [r:discard] [i0:arg] sub 10 [f:yes] [r:discard]
[ex(z)=1] jmp @end [ex(z)=1] jmp @end
add 1 [i0:arg] add 1
jmp @loop jmp @loop
end: end:

View file

@ -3,77 +3,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
Instruction code[] =
{
// { VM_EXEC_X, VM_EXEC_X, VM_INPUT_ZERO, VM_INPUT_ZERO, VM_CMD_COPY, 0, VM_FLAG_NO, VM_OUTPUT_DISCARD, 0 },
{ // push 0
VM_EXEC_X,
VM_EXEC_X,
VM_INPUT_ARG,
VM_INPUT_ZERO,
VM_CMD_COPY,
0,
VM_FLAG_NO,
VM_OUTPUT_PUSH,
0
},
{ // dup
VM_EXEC_X,
VM_EXEC_X,
VM_INPUT_PEEK,
VM_INPUT_ZERO,
VM_CMD_COPY,
0,
VM_FLAG_NO,
VM_OUTPUT_PUSH,
0
},
{ // cmp 10
VM_EXEC_X,
VM_EXEC_X,
VM_INPUT_ARG,
VM_INPUT_POP,
VM_CMD_MATH,
VM_MATH_SUB,
VM_FLAG_YES,
VM_OUTPUT_DISCARD,
10
},
{ // jmp 7
VM_EXEC_1,
VM_EXEC_X,
VM_INPUT_ARG,
VM_INPUT_ZERO,
VM_CMD_COPY,
0,
VM_FLAG_NO,
VM_OUTPUT_JUMP,
7
},
{ // add 1
VM_EXEC_X,
VM_EXEC_X,
VM_INPUT_ARG,
VM_INPUT_POP,
VM_CMD_MATH,
0,
VM_FLAG_NO,
VM_OUTPUT_PUSH,
1
},
{ // jmp 1
VM_EXEC_X,
VM_EXEC_X,
VM_INPUT_ARG,
VM_INPUT_ZERO,
VM_CMD_COPY,
0,
VM_FLAG_NO,
VM_OUTPUT_JUMP,
1
},
};
void dump_proc(Process *process) void dump_proc(Process *process)
{ {
if(process->flags & VM_FLAG_Z) if(process->flags & VM_FLAG_Z)
@ -86,6 +15,11 @@ void dump_proc(Process *process)
else else
printf(" "); printf(" ");
printf(
"%2d %2d ",
process->codePointer,
process->basePointer);
printf("[ "); printf("[ ");
for(int i = 1; i <= process->stackPointer; i++) { for(int i = 1; i <= process->stackPointer; i++) {
printf("%d ", process->stack[i]); printf("%d ", process->stack[i]);
@ -103,7 +37,42 @@ void vm_assert(int assertion, const char *msg)
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
Module module = { code, 6 }; if(argc < 2) {
printf("Usage: vm [binary]\n");
printf(" [binary]: The file to be executed.\n");
return 1;
}
Module module = { 0 };
{ // Read module
FILE *f = fopen(argv[1], "rb");
fseek(f, 0, SEEK_END);
size_t len = ftell(f);
fseek(f, 0, SEEK_SET);
module.code = malloc(len);
module.length = len / sizeof(Instruction);
char *ptr = (char*)module.code;
for(int p = 0; p < len; p += fread(&ptr[p], 1, sizeof(Instruction), f));
fclose(f);
}
printf("Loaded module %s with %d instructions.\n", argv[1], module.length);
for(int i = 0; i < module.length; i++)
{
Instruction c = module.code[i];
printf(
"%3d: %10d %1d %1d %5d %3d %1d %1d %1d %1d\n",
i,
c.argument,
c.output,
c.flags,
c.cmdinfo,
c.command,
c.input1,
c.input0,
c.execN,
c.execZ);
}
Process process = { &module, 0, 0, 0, 0 }; Process process = { &module, 0, 0, 0, 0 };
Process *p = &process; Process *p = &process;