Skip to content

Commit

Permalink
minor bugfixing
Browse files Browse the repository at this point in the history
  • Loading branch information
s-mv committed Jul 19, 2024
1 parent 04b2bba commit 2f02ff3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 16 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
example

out/*
!out
out/!.gitkeep
12 changes: 7 additions & 5 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@

char *readfile(const char *fname);

int main() {
int main(int argc, char **argv) {
smvm vm;
char *content = readfile("test.asmv");
char *content;
char *output;
if (argc > 1) content = readfile(argv[1]);
else content = readfile("test.asmv");
smvm_init(&vm);
smvm_assemble(&vm, content);
// smvm_execute(&vm); // temporarily commented out
smvm_disassemble(&vm, output);
printf("OUTPUT\n---\n%s\n---\n", output);
smvm_execute(&vm); // temporarily commented out
// smvm_disassemble(&vm, output);
printf("\n---\nOUTPUT\n---\n---\n");
free(content);
free(output);
smvm_free(&vm);
Expand Down
2 changes: 1 addition & 1 deletion examples/star.asmv
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ halt
inc ra
mov rb 1
.colloop
mov rc rb
mov rc rb
.rowloop
puts "*"
dec rc
Expand Down
Empty file added out/.gitkeep
Empty file.
14 changes: 8 additions & 6 deletions smvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ static void dsmv_disassemble();
/*** vm - disassembler (dsmv) - implementation ***/

static void dsmv_init(dsmv *ds) {
listmv_init(&ds->bytecode, sizeof(u8));
listmv_init(&ds->code, sizeof(char));
ds->index = 0;
}
Expand Down Expand Up @@ -437,6 +436,7 @@ void smvm_assemble(smvm *vm, char *code) {
asmv_init(&assembler);
assembler.code = code;
asmv_assemble(&assembler);
if (vm->bytecode.data != NULL) listmv_free(&vm->bytecode);
vm->bytecode = assembler.bytecode; // ownership to vm
asmv_free(&assembler);
}
Expand Down Expand Up @@ -506,7 +506,7 @@ void smvm_disassemble(smvm *vm, char *code) {
dsmv_init(&dis);
dis.bytecode = vm->bytecode;
dsmv_disassemble();
code = malloc(dis.code.len);
// code = malloc(dis.code.len);
strncpy(code, (char *)dis.code.data, dis.code.len);
dsmv_free(&dis);
}
Expand Down Expand Up @@ -604,16 +604,17 @@ static asmv_inst asmv_lex_inst(asmv *as) {
// ignore comments
if (current == ';' || current == '#')
while (asmv_next(as) != '\n');
while (isspace(asmv_current(as))) asmv_skip(as);
current = asmv_current(as);
if (current == '\0') return ((asmv_inst){.eof = true});
// label
if (current == '.') {
asmv_skip(as);
offset = 0;
while (isalnum(as->code[as->index])) asmv_skip(as);
while (isalnum(as->code[as->index + offset])) offset++;
char eof = '\0';
listmv_init(&inst.str, sizeof(char));
listmv_push_array(&inst.str, as->code, as->index);
listmv_push_array(&inst.str, as->code + as->index, offset);
listmv_push(&inst.str, &eof);
inst.label = true;
inst.index = as->index;
Expand Down Expand Up @@ -742,7 +743,7 @@ static asmv_inst asmv_lex_inst(asmv *as) {
op.mode = mode_immediate;
char eof = '\0';
listmv_init(&op.data.str, sizeof(char));
listmv_push_array(&op.data.str, as->code, offset);
listmv_push_array(&op.data.str, as->code + as->index, offset);
listmv_push(&op.data.str, &eof);
as->code += offset;
} else {
Expand Down Expand Up @@ -993,7 +994,7 @@ void je_fn(smvm *vm) {
mov_mem((u8 *)&vm->registers[reg_ip], (u8 *)vm->pointers[2], vm->widths[2]);
}
void jne_fn(smvm *vm) {
u64 left, right;
i64 left = 0, right = 0;
mov_mem((u8 *)&left, (u8 *)vm->pointers[0], vm->widths[0]);
mov_mem((u8 *)&right, (u8 *)vm->pointers[1], vm->widths[1]);
if (left == right) {
Expand Down Expand Up @@ -1043,6 +1044,7 @@ void puts_fn(smvm *vm) {
while (str[len] != '\0') len++;
len++;
printf("%s", str);
fflush(stdout);
smvm_ip_inc(vm, len);
}

Expand Down
8 changes: 6 additions & 2 deletions test.asmv
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
puts "Hello, world!\n"
halt
call .hello
halt

.hello
puts "Hello, world!\n"
ret

0 comments on commit 2f02ff3

Please sign in to comment.