Extracted code that performs a jump into its own routine
Checks if the operand given is a well defined program address, then performs the jump.
This commit is contained in:
@@ -123,18 +123,14 @@ err_t vm_execute(vm_t *vm)
|
|||||||
err_t err = vm_pop_word(vm, &ret);
|
err_t err = vm_pop_word(vm, &ret);
|
||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
else if (ret.as_word >= vm->program.max)
|
return vm_jump(vm, ret.as_word);
|
||||||
return ERR_INVALID_PROGRAM_ADDRESS;
|
|
||||||
prog->ptr = ret.as_word;
|
|
||||||
}
|
}
|
||||||
else if (instruction.opcode == OP_JUMP_REGISTER)
|
else if (instruction.opcode == OP_JUMP_REGISTER)
|
||||||
{
|
{
|
||||||
if (instruction.operand.as_byte >= 8)
|
if (instruction.operand.as_byte >= 8)
|
||||||
return ERR_INVALID_REGISTER_WORD;
|
return ERR_INVALID_REGISTER_WORD;
|
||||||
word addr = vm->registers.reg[instruction.operand.as_byte];
|
word addr = vm->registers.reg[instruction.operand.as_byte];
|
||||||
if (addr >= vm->program.max)
|
return vm_jump(vm, addr);
|
||||||
return ERR_INVALID_PROGRAM_ADDRESS;
|
|
||||||
prog->ptr = addr;
|
|
||||||
}
|
}
|
||||||
else if (instruction.opcode >= OP_PRINT_CHAR &&
|
else if (instruction.opcode >= OP_PRINT_CHAR &&
|
||||||
instruction.opcode <= OP_PRINT_WORD)
|
instruction.opcode <= OP_PRINT_WORD)
|
||||||
@@ -367,6 +363,15 @@ void vm_print_all(vm_t *vm, FILE *fp)
|
|||||||
fp);
|
fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err_t vm_jump(vm_t *vm, word w)
|
||||||
|
{
|
||||||
|
printf("vm_jump: w=%lu\n", w);
|
||||||
|
if (w >= vm->program.max)
|
||||||
|
return ERR_INVALID_PROGRAM_ADDRESS;
|
||||||
|
vm->program.ptr = w;
|
||||||
|
return ERR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
err_t vm_push_byte(vm_t *vm, data_t b)
|
err_t vm_push_byte(vm_t *vm, data_t b)
|
||||||
{
|
{
|
||||||
if (vm->stack.ptr >= vm->stack.max)
|
if (vm->stack.ptr >= vm->stack.max)
|
||||||
|
|||||||
@@ -71,6 +71,8 @@ void vm_print_program(vm_t *, FILE *);
|
|||||||
void vm_print_all(vm_t *, FILE *);
|
void vm_print_all(vm_t *, FILE *);
|
||||||
|
|
||||||
// Execution routines
|
// Execution routines
|
||||||
|
err_t vm_jump(vm_t *, word);
|
||||||
|
|
||||||
err_t vm_pop_byte(vm_t *, data_t *);
|
err_t vm_pop_byte(vm_t *, data_t *);
|
||||||
err_t vm_pop_hword(vm_t *, data_t *);
|
err_t vm_pop_hword(vm_t *, data_t *);
|
||||||
err_t vm_pop_word(vm_t *, data_t *);
|
err_t vm_pop_word(vm_t *, data_t *);
|
||||||
|
|||||||
Reference in New Issue
Block a user