jump
This commit is contained in:
parent
b9fa669fd1
commit
ee162a70ed
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", "../examples/s.tasm", "o.bin", "--debug" ],
|
"args": [ "-c", "../examples/conditional_jump.tasm", "o.bin", "--debug", "-i", "o.bin" ],
|
||||||
"cwd": "${workspaceFolder}/bin",
|
"cwd": "${workspaceFolder}/bin",
|
||||||
"preLaunchTask": "build_exec_dbg",
|
"preLaunchTask": "build_exec_dbg",
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
|
|||||||
7
TODO.md
7
TODO.md
@ -1,2 +1,9 @@
|
|||||||
# TODO List
|
# 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
|
- arguments validation for each instruction
|
||||||
|
- VM debug mode
|
||||||
|
|||||||
32
examples/conditional_jump.tasm
Normal file
32
examples/conditional_jump.tasm
Normal file
@ -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
|
||||||
@ -1,12 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
Example of graphical application
|
Example of self-repeating code section
|
||||||
*/
|
*/
|
||||||
|
|
||||||
.main:
|
.main:
|
||||||
|
//TODO loop example
|
||||||
le
|
|
||||||
gt
|
|
||||||
eq
|
|
||||||
jif
|
|
||||||
jel
|
|
||||||
exit
|
exit
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
.data:
|
.data:
|
||||||
// named array of 8-bit values
|
// named array of 8-bit values
|
||||||
const8 msg "Hello, World :3\n\0"
|
const8 msg "Hello, World"
|
||||||
|
|
||||||
.main:
|
.main:
|
||||||
movc ax 1; // sys_write
|
movc ax 1; // sys_write
|
||||||
|
|||||||
40
src/instructions/impl/JMP.c
Normal file
40
src/instructions/impl/JMP.c
Normal file
@ -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);
|
||||||
|
}
|
||||||
@ -27,8 +27,8 @@ i32 XOR_impl(VM* vm);
|
|||||||
i32 AND_impl(VM* vm);
|
i32 AND_impl(VM* vm);
|
||||||
|
|
||||||
i32 JMP_impl(VM* vm);
|
i32 JMP_impl(VM* vm);
|
||||||
i32 JIF_impl(VM* vm);
|
i32 JNZ_impl(VM* vm);
|
||||||
i32 JEL_impl(VM* vm);
|
i32 JZ_impl(VM* vm);
|
||||||
|
|
||||||
|
|
||||||
Array_declare(Instruction);
|
Array_declare(Instruction);
|
||||||
@ -59,8 +59,8 @@ static const Array_Instruction instructions_array = ARRAY(Instruction, {
|
|||||||
Instruction_construct(AND),
|
Instruction_construct(AND),
|
||||||
|
|
||||||
Instruction_construct(JMP),
|
Instruction_construct(JMP),
|
||||||
Instruction_construct(JIF),
|
Instruction_construct(JNZ),
|
||||||
Instruction_construct(JEL),
|
Instruction_construct(JZ),
|
||||||
});
|
});
|
||||||
|
|
||||||
const Instruction* Instruction_getByOpcode(Opcode opcode){
|
const Instruction* Instruction_getByOpcode(Opcode opcode){
|
||||||
|
|||||||
@ -32,8 +32,8 @@ typedef enum __attribute__((__packed__)) Opcode {
|
|||||||
Opcode_AND,
|
Opcode_AND,
|
||||||
|
|
||||||
Opcode_JMP,
|
Opcode_JMP,
|
||||||
Opcode_JIF,
|
Opcode_JNZ,
|
||||||
Opcode_JEL,
|
Opcode_JZ,
|
||||||
} Opcode;
|
} Opcode;
|
||||||
|
|
||||||
typedef struct Instruction {
|
typedef struct Instruction {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user