Giter Club home page Giter Club logo

mvm's Introduction

MVM Tools

All mvm Tools

  • mvm: A VM, just like jvm.
  • masm: A Compiler that compiles the VM's Assembly language.
  • msm: Assembly language for the VM.
  • demasm: Disassembler for the bytecode.

MVM

A Virtual Machine capable of running bytecode, just like jvm. It is used to run programs generated by masm.

Features:

  • Stack size: 942
  • Turing Complete
  • Own asm language

> mvm.exe -i [input.mbc]

To see a list of all Flags type:

> mvm.exe -h

MASM

The Compiler that compiles msm to .mbc (Minimalistic byte code) files.

> masm.exe [input.vsm] [output.sbc]

DEMASM

The Disassembler for the bytecode generated by the masm compiler.

> demasm.exe [input.sbc]

MSM

Assembly language for the Virtual Machine.
msm has no registers and is there for completely stack based.
Also the maximum amount of operands is only one.

NOTE: For code examples see ./examples/.

ASM Instructions:

Instruction Arguments Description
push value Pushes a value on to the stack.
dup addr Duplicates the value at the given stack addr.
swap addr Swaps the top value and the value at the given addr.
drop NONE Removes the top value from the stack.
plusi stack: a, b Adds the top two values on the stack. (for integers)
minusi stack: a, b Subtracts the top two values on the stack. (for integers)
multi stack: a, b Multiplies the top two values on the stack. (for integers)
divi stack: a, b Divides the top two values on the stack. (for integers)
plusf stack: a, b Adds the top two values on the stack. (for floats)
minusf stack: a, b Subtracts the top two values on the stack. (for floats)
multf stack: a, b Multiplies the top two values on the stack. (for floats)
divf stack: a, b Divides the top two values on the stack. (for floats)
equal stack: a, b Pushes ZERO on the stack if the top two values are not equal, else it pushes ONE.
not stack: a Reverses the top value on the stack. e.g. 0 to 1 and 1 to 0 (for integers, 0-1)
geeqf a, b Pushes ZERO on the stack if the top value on the stack is grater or equal to the second value. (for floats)
geeqi a, b Pushes ZERO on the stack if the top value on the stack is grater or equal to the second value. (for integers)
leeqf a, b Pushes ZERO on the stack if the top value on the stack is less or equal to the second value. (for floats)
leeqi a, b Pushes ZERO on the stack if the top value on the stack is less or equal to the second value. (for integers)
jmp label or addr Jumps to the the given label or addr.
jmpif label or addr Jumps to the the given label or addr if the top value on the stack is ZERO.
call label or addr Jumps to the the given label or addr and pushes the addr from the instruction after the current instruction on to the stack.
ret stack: addr Jumps to the the given addr (top value on the stack).
int interruptAddr stack: args Generates a software interrupt and calls one of the interrupt functions pointed to by the given interruptAddr, the args are parsed from the stack.
hlt NONE Stops the execution.

Label definition:

You can define a label by writing the name followed by a colon. You are also able to write one instruction on the same line as the label definition as shown below.

;; Fibonacci example
%include "../msmlib/stdlib.mlb"

jmp main

; Iterations:
%define I 30

; Define a label:
main:
    push 0
    push 1
    push I
; Define a label with instruction on same line:
loop: swap 2
      dup 0
      call println_u64
      dup 1
      plusi
      swap 1
      swap 2
      push 1
      minusi
      dup 0
      push 0
      equal
      not
      jmpif loop
hlt

Call definition:

In msm calls are just like label jmps with the addition that you have to kep track of the return addr that is pushed one the stack by the call instruction.

%include "../msmlib/stdlib.mlb"

jmp main

%define A 10
%define B 5

;; Function that adds to nums togeter ;;
add:
    swap 2 ; Swap the return addr to the bottom.
    plusf
    swap 1
    ret
;; ---------------------------------- ;;

main:
    push A
    push B
    call add ; Calls the functions
    ncall println_u64 ; Prints the result
    hlt

Preprocessor directives:

Instruction Arguments Description
define name value Defines a constant with name name and value value
include path Includes a masm lib located at the given path.

In msm all directives start with a percent sign as shown below.

include:

%include "../msmlib/stdlib.mlb"

define:

%define NUMBER 420

The value can also be a string.

%define STRING "Some string.\n"

Software Tnterrupts:

Interrupt name Address args Description
print_char 0 ascii_code Prints the given ascii_code value as char to stdout.
print_f64 1 f64_value Prints the given f64_value value to stdout.
print_i64 2 i64_value Prints the given i64_value value to stdout.
print_u64 3 u64_value Prints the given u64_value value to stdout.
print_ptr 4 ptr Prints the given ptr value to stdout.
alloc 5 size Allocates a block of memory, returning a pointer to the beginning of the block.
free 6 ptr Deallocates a space previously allocated by alloc, using the given 'ptr' value.
mem_dump 7 ptr size Dumps the memory, starting from the given ptr up to ptr + size.
write 8 ptr str_size Writes a memory string to stdout.
readline 9 NONE Reads a line from stdin to the stack in reverse.

In msm interrupts are used as shown below. All args are parsed over the stack.

push 420
int 2 ; print_u64

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.