Simple calculator program on virtual machine.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.travis.yml
LICENSE
README.md
makefile

README.md

calc.vm

Simple calculator program with virtual machine.

Build Status

build

$ make all

example

#define PUSH(i) PSH, i
#define PUSHN(i, d) PSH_NUM, i, d

// main program
const int main_code[] = {
    // add two int
    PUSH(7), PUSH(3), ADD, POP, // 7 + 3
    // sub int & num
    PUSH(19), PUSHN(10, 5), SUB, POP, // 19 - 10.5
    // mul two num
    PUSHN(12, 5), PUSHN(3, 9), MUL, POP, // 12.5 * 3.9
    // multiple
    PUSH(4), PUSHN(5, 6), ADD, PUSH(9), MUL, PUSHN(25, 2), SUB, POP, // (4 + 5.6) * 9 - 25.2
    PUSHN(11, 5), PUSHN(4, 8), SUB, PUSH(13), DIV, PUSH(7), MUL, POP, // (11.5 - 4.8) / 13 *7
    // exit
    HLT
};

// init vm
calc_vm_init(main_code);

// run vm & set result
int ret = calc_vm_run();

bytecode

debug

PSH, 7, PSH, 3, ADD, POP
------------------------------

offset    opcode      descript
------------------------------------------
0000     [ PSH ]     push (int) 7 to stack
0002     [ PSH ]     push (int) 3 to stack
0004     [ ADD ]     add 7 and 3, push 10
0005     [ POP ]     pop 10 and print
=> 10
opcode          descipt
----------------------------
HLT = 0         exit
ADD = 1         + add two value from stack and push
SUB = 2         - sub ..
MUL = 3         * mul ..
DIV = 4         / div ..
PSH = 5         push (int) to stack
PSH_NUM = 6     push (num) to stack
POP = 7         pop value and print