From 65972fcbbe594d7e748be82021f35f8caef19c5a Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Mon, 9 Mar 2020 15:11:32 -0400 Subject: [PATCH] Change instruction encodings to avoid null instructions Lots of null bytes in a row are a common thing, so the instructions available can't start with a null pair of bytes anymore. Signed-off-by: Alek Ratzloff --- src/libvm/examples/deadbeef.asm | 2 +- src/libvm/src/inst.rs | 52 +++++++++++++-------------- src/libvm/src/obj/assemble/session.rs | 1 - src/libvm/src/obj/disassemble.rs | 2 +- src/main.rs | 2 +- vm.md | 51 +++++++++++++------------- 6 files changed, 54 insertions(+), 56 deletions(-) diff --git a/src/libvm/examples/deadbeef.asm b/src/libvm/examples/deadbeef.asm index 8f624ea..fe1f953 100644 --- a/src/libvm/examples/deadbeef.asm +++ b/src/libvm/examples/deadbeef.asm @@ -21,7 +21,7 @@ jnz end mov %status, $1 end: - div %r0, $0 + int $0, $0 halt .export main diff --git a/src/libvm/src/inst.rs b/src/libvm/src/inst.rs index 41c831e..e03a812 100644 --- a/src/libvm/src/inst.rs +++ b/src/libvm/src/inst.rs @@ -23,32 +23,32 @@ macro_rules! instructions { pub type InstOp = u16; instructions! { - ADD = 0x0000, - SUB = 0x0001, - MUL = 0x0002, - DIV = 0x0003, - IDIV = 0x0004, - MOD = 0x0005, - AND = 0x0006, - OR = 0x0007, - XOR = 0x0008, - SHL = 0x0009, - SHR = 0x000a, - INEG = 0x000b, - INV = 0x000c, - NOT = 0x000d, - CMPEQ = 0x1000, - CMPLT = 0x1001, - JMP = 0x1002, - JZ = 0x1003, - JNZ = 0x1004, - CALL = 0x2000, - RET = 0x2001, - PUSH = 0x2002, - POP = 0x2003, - INT = 0x2004, - IRET = 0x2005, - MOV = 0x3000, + ADD = 0x1000, + SUB = 0x1001, + MUL = 0x1002, + DIV = 0x1003, + IDIV = 0x1004, + MOD = 0x1005, + AND = 0x1006, + OR = 0x1007, + XOR = 0x1008, + SHL = 0x1009, + SHR = 0x100a, + INEG = 0x100b, + INV = 0x100c, + NOT = 0x100d, + CMPEQ = 0x2000, + CMPLT = 0x2001, + JMP = 0x2002, + JZ = 0x2003, + JNZ = 0x2004, + CALL = 0x3000, + RET = 0x3001, + PUSH = 0x3002, + POP = 0x3003, + INT = 0x3004, + IRET = 0x3005, + MOV = 0x4000, HALT = 0xF000, NOP = 0xF001, DUMP = 0xF002, diff --git a/src/libvm/src/obj/assemble/session.rs b/src/libvm/src/obj/assemble/session.rs index c749105..dd1c7c0 100644 --- a/src/libvm/src/obj/assemble/session.rs +++ b/src/libvm/src/obj/assemble/session.rs @@ -1,5 +1,4 @@ use crate::{ - addr::Addr, obj::{ assemble::{ Asm, diff --git a/src/libvm/src/obj/disassemble.rs b/src/libvm/src/obj/disassemble.rs index 180db83..6f7a3a0 100644 --- a/src/libvm/src/obj/disassemble.rs +++ b/src/libvm/src/obj/disassemble.rs @@ -1,4 +1,4 @@ -use crate::{mem::MemCursor, obj::obj::*, inst::Inst}; +use crate::{mem::MemCursor, obj::obj::*}; use prettytable::{Table, row, cell}; use std::io::{self, Write as Write}; diff --git a/src/main.rs b/src/main.rs index af49e32..c628bd8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ extern crate libvm as vm; use structopt::StructOpt; use snafu::Snafu; -use std::{fs, io::{stdin, stdout, Write, Read}, path::{Path, PathBuf}, process}; +use std::{fs, io::{stdout, Write}, path::{Path, PathBuf}, process}; const DEFAULT_MAX_MEM: usize = 64 * 1024 * 1024; diff --git a/vm.md b/vm.md index 33cff11..50f5477 100644 --- a/vm.md +++ b/vm.md @@ -128,43 +128,43 @@ Arithmetic instructions store their result in the first register specified. Over wrapping around to 0. * Add - * Opcode: 0x0000 + * Opcode: 0x1000 * Params: Destination, source * Sub - * Opcode: 0x0001 + * Opcode: 0x1001 * Params: Destination, source * Mul - * Opcode: 0x0002 + * Opcode: 0x1002 * Params: Destination, source * Div - * Opcode: 0x0003 + * Opcode: 0x1003 * Params: Destination, source * Mod - * Opcode: 0x0004 + * Opcode: 0x1004 * Params: Destination, source * And - * Opcode: 0x0005 + * Opcode: 0x1005 * Params: Destination, source * Or - * Opcode: 0x0006 + * Opcode: 0x1006 * Params: Destination, source * Xor - * Opcode: 0x0007 + * Opcode: 0x1007 * Params: Destination, source * Shl - * Opcode: 0x0008 + * Opcode: 0x1008 * Params: Destination, source * Shr - * Opcode: 0x0009 + * Opcode: 0x1009 * Params: Destination, source * INeg - * Opcode: 0x000a + * Opcode: 0x100a * Params: Destination, source * Inv - * Opcode: 0x000b + * Opcode: 0x100b * Params: Destination, source * Not - * Opcode: 0x000c + * Opcode: 0x100c * Params: Destination, source ### TODO @@ -176,25 +176,25 @@ wrapping around to 0. ## Control flow * CmpEq - * Opcode: 0x1000 + * Opcode: 0x2000 * Params: Source, source * CmpLt - * Opcode: 0x1001 + * Opcode: 0x2001 * Params: Source, source * Jmp - * Opcode: 0x1002 + * Opcode: 0x2002 * Params: Source * Jz - * Opcode: 0x1003 + * Opcode: 0x2003 * Params: Source * Jnz - * Opcode: 0x1004 + * Opcode: 0x2004 * Params: Source ## Functions * Call - * Opcode: 0x2000 + * Opcode: 0x3000 * Params: Source * When this instruction is executed, these actions occur: * Push the current stack frame pointer @@ -202,26 +202,26 @@ wrapping around to 0. * Update the IP (i.e., jump) to the value at the given source. * Update the frame pointer to the current stack pointer - 16 * Ret - * Opcode: 0x2001 + * Opcode: 0x3001 * When this instruction is executed, these actions occur: * Update the stack pointer to the current frame pointer + 16. * Pop the IP of the next instruction. * Pop the old stack frame. * Restore the last three values in an undefined order * Push - * Opcode: 0x2002 + * Opcode: 0x3002 * Params: Source * When this instruction is executed, these actions occur: * Set the value in memory at the current stack pointer to the source value. * Increment the stack pointer by the size of value at the source. * Pop - * Opcode: 0x2003 + * Opcode: 0x3003 * Params: Dest * When this instruction is executed, these actions occur: * Decrement the stack pointer by the size of value at the destination. * Copy the value at the stack pointer into the destination. * Int - * Opcode: 0x2004 + * Opcode: 0x3004 * Params: Source, Source * When this instruction is executed, these actions occur: * Push the current stack frame pointer @@ -235,7 +235,7 @@ wrapping around to 0. * Update the R1 register to the value in the second parameter * Update the frame pointer to the current stack pointer - 48 * IRet - * Opcode: 0x2005 + * Opcode: 0x3005 * When this instruction is executed, these actions occur: * Update the stack pointer to the current frame pointer + 48 * Pop the old R1 value @@ -249,7 +249,7 @@ wrapping around to 0. ## Data movement * Mov - * Opcode: 0x3000 + * Opcode: 0x4000 * Params: Source, Dest ## Miscellaneous @@ -410,7 +410,6 @@ A VM must provide support for the following meta-values: # General TODO -* Interrupts * MMIO regions * Paging? * Determine how address sizes are determined