diff --git a/.vscode/launch.json b/.vscode/launch.json index 968b3b1..cf83850 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,7 +7,7 @@ "request": "launch", "program": "${workspaceFolder}/bin/tcpu", "windows": { "program": "${workspaceFolder}/bin/tcpu.exe" }, - "args": [ "-c", "s.tasm", "o.bin" ], + "args": [ "-c", "../examples/s.tasm", "o.bin", "--debug" ], "cwd": "${workspaceFolder}/bin", "preLaunchTask": "build_exec_dbg", "stopAtEntry": false, diff --git a/examples/s.tasm b/examples/s.tasm new file mode 100644 index 0000000..9c86ae8 --- /dev/null +++ b/examples/s.tasm @@ -0,0 +1,14 @@ +/* +"hello world" program in my assembly language +*/ + +// named data array +c8 msg "Hello, World :3\0" + +push ax 1; // sys_write +push bx 1; // stdout +push cx @msg; // address of msg data +push dx #msg; // size of msg data +sys +push ax 0 +exit diff --git a/src/compiler/Compiler.c b/src/compiler/Compiler.c index d15aa5b..26d9b48 100644 --- a/src/compiler/Compiler.c +++ b/src/compiler/Compiler.c @@ -61,7 +61,7 @@ static bool compileFile(Compiler* cmp, FILE* f){ return true; } -bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name, bool debug){ +bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name, bool debug_log){ FILE* f = fopen(source_file_name, "rb"); if(f == NULL) returnError("ERROR: can't open file '%s'", source_file_name); @@ -94,14 +94,14 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name, returnError("ERROR: can't open file '%s'", out_file_name); } - if(debug){ + if(debug_log){ printf("----------------------------------[%s]---------------------------------\n", source_file_name); fputs(cmp->code, stdout); fputc('\n', stdout); } bool success = Compiler_lex(cmp); - if(debug){ + if(debug_log){ printf("------------------------------------[lines]-----------------------------------\n"); for(u32 i = 0; i < cmp->line_lengths.len; i++){ printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]); diff --git a/src/main.c b/src/main.c index d6c3658..f8dba23 100644 --- a/src/main.c +++ b/src/main.c @@ -5,7 +5,7 @@ #define arg_is(STR) (strcmp(argv[argi], STR) == 0) -i32 compileSources(cstr source_file, cstr out_file); +i32 compileSources(cstr source_file, cstr out_file, bool debug_log); i32 bootFromImage(cstr image_file); i32 main(const i32 argc, cstr* argv){ @@ -21,6 +21,8 @@ i32 main(const i32 argc, cstr* argv){ cstr NULLABLE(out_file) = NULL; cstr NULLABLE(source_file) = NULL; + bool debug_log = false; + for(i32 argi = 1; argi < argc; argi++){ if(arg_is("-h") || arg_is("--help")){ printf( @@ -28,6 +30,7 @@ i32 main(const i32 argc, cstr* argv){ "-op, --opcodes Show list of all instructions.\n" "-i, --image [FILE] Boot VM using image file.\n" "-c, --compile [SOURCE_FILE] [OUT_FILE] Compile assembly source files to machine code.\n" + "-d, --debug Enable debug log.\n" ); return 0; } @@ -72,6 +75,9 @@ i32 main(const i32 argc, cstr* argv){ } out_file = argv[argi]; } + else if(arg_is("-d") || arg_is("--debug")){ + debug_log = true; + } else { printfe("ERROR: unknown argument '%s'\n", argv[argi]); return 1; @@ -80,7 +86,7 @@ i32 main(const i32 argc, cstr* argv){ i32 exit_code = 0; if(compile){ - exit_code = compileSources(source_file, out_file); + exit_code = compileSources(source_file, out_file, debug_log); } if(exit_code == 0 && boot){ exit_code = bootFromImage(image_file); @@ -131,10 +137,10 @@ i32 bootFromImage(cstr image_file){ return exit_code; } -i32 compileSources(cstr source_file, cstr out_file){ +i32 compileSources(cstr source_file, cstr out_file, bool debug_log){ Compiler cmp; Compiler_init(&cmp); - bool success = Compiler_compile(&cmp, source_file, out_file, true); + bool success = Compiler_compile(&cmp, source_file, out_file, debug_log); if(!success){ if(cmp.error_message){ printfe("COMPILER ERROR: %s\n", cmp.error_message);