From ee162a70ed4d7bffaf3d869edfe0aef0ca672872 Mon Sep 17 00:00:00 2001 From: Timerix Date: Wed, 12 Mar 2025 12:57:42 +0500 Subject: [PATCH] jump --- .vscode/launch.json | 2 +- TODO.md | 7 ++++++ examples/conditional_jump.tasm | 32 ++++++++++++++++++++++++++ examples/loop.tasm | 9 ++------ examples/stdout.tasm | 2 +- src/instructions/impl/JMP.c | 40 +++++++++++++++++++++++++++++++++ src/instructions/instructions.c | 8 +++---- src/instructions/instructions.h | 4 ++-- 8 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 examples/conditional_jump.tasm create mode 100644 src/instructions/impl/JMP.c diff --git a/.vscode/launch.json b/.vscode/launch.json index cf83850..2cecc3d 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", "../examples/s.tasm", "o.bin", "--debug" ], + "args": [ "-c", "../examples/conditional_jump.tasm", "o.bin", "--debug", "-i", "o.bin" ], "cwd": "${workspaceFolder}/bin", "preLaunchTask": "build_exec_dbg", "stopAtEntry": false, diff --git a/TODO.md b/TODO.md index 01ab787..d977d80 100644 --- a/TODO.md +++ b/TODO.md @@ -1,2 +1,9 @@ # TODO List +- add negative number arguments support +- change section binary format: + 1. code + 2. exit instruction with code ERR_U_FORGOT_TO_CALL_EXIT + 3. data +- add padding to compilation - arguments validation for each instruction +- VM debug mode diff --git a/examples/conditional_jump.tasm b/examples/conditional_jump.tasm new file mode 100644 index 0000000..6d3937e --- /dev/null +++ b/examples/conditional_jump.tasm @@ -0,0 +1,32 @@ +/* +Example of behavior change depending on some condition +*/ + +.main: +movc ax 1 +movc bx 2 +gt cx ax bx +jnz @true cx +jz @false cx + +.true: +const8 true.msg "true\n" +movc cx @true.msg +movc dx #true.msg +jmp @print + +.false +const8 false.msg "false\n" +movc cx @false.msg +movc dx #false.msg +jmp @print + +.print: +movc ax 1 +movc bx 1 +sys +jmp @end + +.end: +movc ax 0 +exit diff --git a/examples/loop.tasm b/examples/loop.tasm index 9050643..ac10ed7 100644 --- a/examples/loop.tasm +++ b/examples/loop.tasm @@ -1,12 +1,7 @@ /* -Example of graphical application +Example of self-repeating code section */ .main: - -le -gt -eq -jif -jel +//TODO loop example exit diff --git a/examples/stdout.tasm b/examples/stdout.tasm index 03d5ce0..f3595ea 100644 --- a/examples/stdout.tasm +++ b/examples/stdout.tasm @@ -4,7 +4,7 @@ .data: // named array of 8-bit values -const8 msg "Hello, World :3\n\0" +const8 msg "Hello, World" .main: movc ax 1; // sys_write diff --git a/src/instructions/impl/JMP.c b/src/instructions/impl/JMP.c new file mode 100644 index 0000000..9121806 --- /dev/null +++ b/src/instructions/impl/JMP.c @@ -0,0 +1,40 @@ +#include "impl_macros.h" + +// JUMP [destination address] +i32 JMP_impl(VM* vm){ + u32 dst_addr = 0; + readVar(dst_addr); + + vm->current_pos = dst_addr; + + return sizeof(dst_addr); +} + + +// JNZ [destination address] [condition register] +i32 JNZ_impl(VM* vm){ + u32 dst_addr = 0; + readVar(dst_addr); + u8 cond_register_i = 0; + readRegisterVar(cond_register_i); + + if(vm->registers.array[cond_register_i].u32v0 != 0){ + vm->current_pos = dst_addr; + } + + return sizeof(dst_addr) + sizeof(cond_register_i); +} + +// JZ [destination address] [condition register] +i32 JZ_impl(VM* vm){ + u32 dst_addr = 0; + readVar(dst_addr); + u8 cond_register_i = 0; + readRegisterVar(cond_register_i); + + if(vm->registers.array[cond_register_i].u32v0 == 0){ + vm->current_pos = dst_addr; + } + + return sizeof(dst_addr) + sizeof(cond_register_i); +} diff --git a/src/instructions/instructions.c b/src/instructions/instructions.c index 866505c..f299dc5 100644 --- a/src/instructions/instructions.c +++ b/src/instructions/instructions.c @@ -27,8 +27,8 @@ i32 XOR_impl(VM* vm); i32 AND_impl(VM* vm); i32 JMP_impl(VM* vm); -i32 JIF_impl(VM* vm); -i32 JEL_impl(VM* vm); +i32 JNZ_impl(VM* vm); +i32 JZ_impl(VM* vm); Array_declare(Instruction); @@ -59,8 +59,8 @@ static const Array_Instruction instructions_array = ARRAY(Instruction, { Instruction_construct(AND), Instruction_construct(JMP), - Instruction_construct(JIF), - Instruction_construct(JEL), + Instruction_construct(JNZ), + Instruction_construct(JZ), }); const Instruction* Instruction_getByOpcode(Opcode opcode){ diff --git a/src/instructions/instructions.h b/src/instructions/instructions.h index 972d6bb..5ec7b72 100644 --- a/src/instructions/instructions.h +++ b/src/instructions/instructions.h @@ -32,8 +32,8 @@ typedef enum __attribute__((__packed__)) Opcode { Opcode_AND, Opcode_JMP, - Opcode_JIF, - Opcode_JEL, + Opcode_JNZ, + Opcode_JZ, } Opcode; typedef struct Instruction {