example
This commit is contained in:
parent
f710aa4199
commit
5c9197436f
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@ -7,7 +7,7 @@
|
|||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/bin/tcpu",
|
"program": "${workspaceFolder}/bin/tcpu",
|
||||||
"windows": { "program": "${workspaceFolder}/bin/tcpu.exe" },
|
"windows": { "program": "${workspaceFolder}/bin/tcpu.exe" },
|
||||||
"args": [ "-c", "s.tasm", "o.bin" ],
|
"args": [ "-c", "../examples/s.tasm", "o.bin", "--debug" ],
|
||||||
"cwd": "${workspaceFolder}/bin",
|
"cwd": "${workspaceFolder}/bin",
|
||||||
"preLaunchTask": "build_exec_dbg",
|
"preLaunchTask": "build_exec_dbg",
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
|
|||||||
14
examples/s.tasm
Normal file
14
examples/s.tasm
Normal file
@ -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
|
||||||
@ -61,7 +61,7 @@ static bool compileFile(Compiler* cmp, FILE* f){
|
|||||||
return true;
|
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");
|
FILE* f = fopen(source_file_name, "rb");
|
||||||
if(f == NULL)
|
if(f == NULL)
|
||||||
returnError("ERROR: can't open file '%s'", source_file_name);
|
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);
|
returnError("ERROR: can't open file '%s'", out_file_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(debug){
|
if(debug_log){
|
||||||
printf("----------------------------------[%s]---------------------------------\n", source_file_name);
|
printf("----------------------------------[%s]---------------------------------\n", source_file_name);
|
||||||
fputs(cmp->code, stdout);
|
fputs(cmp->code, stdout);
|
||||||
fputc('\n', stdout);
|
fputc('\n', stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool success = Compiler_lex(cmp);
|
bool success = Compiler_lex(cmp);
|
||||||
if(debug){
|
if(debug_log){
|
||||||
printf("------------------------------------[lines]-----------------------------------\n");
|
printf("------------------------------------[lines]-----------------------------------\n");
|
||||||
for(u32 i = 0; i < cmp->line_lengths.len; i++){
|
for(u32 i = 0; i < cmp->line_lengths.len; i++){
|
||||||
printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]);
|
printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]);
|
||||||
|
|||||||
14
src/main.c
14
src/main.c
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#define arg_is(STR) (strcmp(argv[argi], STR) == 0)
|
#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 bootFromImage(cstr image_file);
|
||||||
|
|
||||||
i32 main(const i32 argc, cstr* argv){
|
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(out_file) = NULL;
|
||||||
cstr NULLABLE(source_file) = NULL;
|
cstr NULLABLE(source_file) = NULL;
|
||||||
|
|
||||||
|
bool debug_log = false;
|
||||||
|
|
||||||
for(i32 argi = 1; argi < argc; argi++){
|
for(i32 argi = 1; argi < argc; argi++){
|
||||||
if(arg_is("-h") || arg_is("--help")){
|
if(arg_is("-h") || arg_is("--help")){
|
||||||
printf(
|
printf(
|
||||||
@ -28,6 +30,7 @@ i32 main(const i32 argc, cstr* argv){
|
|||||||
"-op, --opcodes Show list of all instructions.\n"
|
"-op, --opcodes Show list of all instructions.\n"
|
||||||
"-i, --image [FILE] Boot VM using image file.\n"
|
"-i, --image [FILE] Boot VM using image file.\n"
|
||||||
"-c, --compile [SOURCE_FILE] [OUT_FILE] Compile assembly source files to machine code.\n"
|
"-c, --compile [SOURCE_FILE] [OUT_FILE] Compile assembly source files to machine code.\n"
|
||||||
|
"-d, --debug Enable debug log.\n"
|
||||||
);
|
);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -72,6 +75,9 @@ i32 main(const i32 argc, cstr* argv){
|
|||||||
}
|
}
|
||||||
out_file = argv[argi];
|
out_file = argv[argi];
|
||||||
}
|
}
|
||||||
|
else if(arg_is("-d") || arg_is("--debug")){
|
||||||
|
debug_log = true;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
printfe("ERROR: unknown argument '%s'\n", argv[argi]);
|
printfe("ERROR: unknown argument '%s'\n", argv[argi]);
|
||||||
return 1;
|
return 1;
|
||||||
@ -80,7 +86,7 @@ i32 main(const i32 argc, cstr* argv){
|
|||||||
|
|
||||||
i32 exit_code = 0;
|
i32 exit_code = 0;
|
||||||
if(compile){
|
if(compile){
|
||||||
exit_code = compileSources(source_file, out_file);
|
exit_code = compileSources(source_file, out_file, debug_log);
|
||||||
}
|
}
|
||||||
if(exit_code == 0 && boot){
|
if(exit_code == 0 && boot){
|
||||||
exit_code = bootFromImage(image_file);
|
exit_code = bootFromImage(image_file);
|
||||||
@ -131,10 +137,10 @@ i32 bootFromImage(cstr image_file){
|
|||||||
return exit_code;
|
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 cmp;
|
||||||
Compiler_init(&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(!success){
|
||||||
if(cmp.error_message){
|
if(cmp.error_message){
|
||||||
printfe("COMPILER ERROR: %s\n", cmp.error_message);
|
printfe("COMPILER ERROR: %s\n", cmp.error_message);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user