Browse Source

Add overflow check, closes #1

master
parent
commit
712fe17c5e
No known key found for this signature in database GPG Key ID: DA34C790D267C164
5 changed files with 26 additions and 32 deletions
  1. +4
    -4
      README.md
  2. +4
    -4
      oidASM/count_to_100.oidasm
  3. +1
    -6
      oidASM/fibonacci.oidasm
  4. +7
    -16
      oidASM/guessing_game.oidasm
  5. +10
    -2
      src/oidavm.zig

+ 4
- 4
README.md View File

@@ -26,8 +26,8 @@ These can target the entire memory space of the VM. There are six possible opcod

These can target all addresses on the current page, which represents one 16th of the full memory. There are 144 possible opcodes in this category.

- `0x11 incby` *Increment By*: Adds value of address in memory to ACC. Overflow gets silently truncated to 65535.
- `0x12 minus` *Minus*: Substracts value of address in memory from ACC. Underflow gets silently truncated to 0.
- `0x11 incby` *Increment By*: Adds value of address in memory to ACC. Overflow gets truncated to 65535; if no overflow occurs the next instruction is skipped.
- `0x12 minus` *Minus*: Substracts value of address in memory from ACC. Underflow gets truncated to 0; if no overflow occurs the next instruction is skipped.
- `0x20 fetch` *Fetch*: Copies memory value at address into ACC.
- `0x21 write` *Write*: Overwrites memory at address with copy of ACC.
- `0x30 jmpto` *Jump To*: Unconditionally continues execution at address.
@@ -44,8 +44,8 @@ These can't target memory and take no arguments, so they are either used for I/O
- `0xf013 outhx` *Output, Hexadecimal*: Writes the content of ACC to stderr, as a hexadecimal number.
- `0xf020 inacc` *Input To ACC*: Awaits one word of input from user and writes it into ACC.
- `0xf030 rando` *Randomize ACC*: Writes a random value (backed by the default PRNG) into ACC.
- `0xf040 augmt` *Augment ACC*: Increases ACC by one. Overflow gets silently truncated to 65535.
- `0xf041 dimin` *Diminish ACC*: Diminishes ACC by one. Underflow gets silently truncated to 0.
- `0xf040 augmt` *Augment ACC*: Increases ACC by one. Overflow gets truncated to 65535; if no overflow occurs the next instruction is skipped.
- `0xf041 dimin` *Diminish ACC*: Diminishes ACC by one. Underflow gets truncated to 0; if no underflow occurs the next instruction is skipped.
- `0xf042 shfl4` *Shift Left Four*: Shifts the value of ACC four bytes to the left.
- `0xf043 shfr4` *Shift Right Four*: Shifts the value of ACC four bytes to the right.
- `0xf044 shfl1` *Shift Left One*: Shifts the value of ACC one byte to the left.


+ 4
- 4
oidASM/count_to_100.oidasm View File

@@ -1,19 +1,19 @@
@entry :start

@page 0
$ninetynine 63
$target 64
$one 1
$counter 0

:start
incby $one
noopr
write $counter
minus $ninetynine
jmpez :loop
minus $target
jmpto :loop
fetch $counter
outnm
outlf
cease

:loop
fetch $counter


+ 1
- 6
oidASM/fibonacci.oidasm View File

@@ -4,20 +4,15 @@
$old 1
$new 1
$oldtmp 0
$maxval_minus_one fffe
$backup 0

:start
fetch $old
write $oldtmp
incby $new
write $backup
minus $maxval_minus_one
jmpez :loop
cease
jmpto :loop

:loop
fetch $backup
write $old
fetch $oldtmp
write $new


+ 7
- 16
oidASM/guessing_game.oidasm View File

@@ -1,37 +1,28 @@
@entry :init
@page 0
$backup 0
$val 0
$val_minus_one 0

# Generate random word and shift it right by eight bits, no one wants to guess in 0-4096
# Also, if a zero is rolled, start over.
:init
rando
shfr4
shfr4
write $val
dimin
write $val_minus_one
pgjmp :start
jmpez :init
jmpto :start

:start
inacc
write $backup
minus $val
jmpez :maybe_yay
pgjmp :too_low
jmpez :yay_point
pgjmp :too_big

# Either correct or too low
:maybe_yay
fetch $backup
minus $val_minus_one
jmpez :too_low_point
# PageAndJump to yay
:yay_point
pgjmp :yay

# PageAndJump to toolow
:too_low_point
pgjmp :too_low

# Input value was bigger than $val
@page 1
$plus 2b


+ 10
- 2
src/oidavm.zig View File

@@ -102,9 +102,11 @@ pub const OidaVm = struct {
this.accumulator = 65535;
} else {
this.accumulator += this.memory[address];
this.instruction_ptr += 1;
},
.Minus => if (this.accumulator >= this.memory[address]) {
this.accumulator -= this.memory[address];
this.instruction_ptr += 1;
} else {
this.accumulator = 0;
},
@@ -156,10 +158,16 @@ pub const OidaVm = struct {
this.accumulator = this.rng.int(u16);
},
.Augment => {
if (this.accumulator < 65535) this.accumulator += 1;
if (this.accumulator < 65535) {
this.accumulator += 1;
this.instruction_ptr += 1;
}
},
.Diminish => {
if (this.accumulator > 0) this.accumulator -= 1;
if (this.accumulator > 0) {
this.accumulator -= 1;
this.instruction_ptr += 1;
}
},
.ShiftLeftFour => {
this.accumulator = this.accumulator << 4;


Loading…
Cancel
Save