Skip to content

Instruction Set

Adam Mathlay edited this page Sep 8, 2024 · 6 revisions

Instruction Set

This page provides a detailed reference for the MVM (Micro Virtual Machine) byte-code instruction set. The VM directly executes the instructions listed here.

Overview

MVM employs a simple, human-readable assembly language. Each instruction is represented by a mnemonic (a short, descriptive word), followed by zero or more arguments. Instructions are generally executed sequentially, unless a control flow instruction alters the execution order.

Instruction Format:

<instruction_name> <argument_1> <argument_2> ...

Example:

MOV G1 R4      // Move the value in register G1 to register R4
ADD G1 G2      // Add the values in G1 and G2, store the result in R4
               // (Most results are automaticly stored in R4)
JMP 10         // Jump to line 10 in the code

Instruction Categories:

To make it easier to understand and navigate, group the instructions into categories based on their functionality:

Data Transfer Instructions:

Instruction Description Argument 1 Argument 2
MOV Moves a value from one register(argument 1) to another (argument 2) source: Register destnination: Register
LIT Loads a Literal(A normal 32 BIT integer) into a register(argument 1) register: Register literal: Int

Arithmetic Instructions:

Instruction Description Arguments
ADD Add the values in the two Argument registers and store the result in the destination register. Argument 1 Register, Argument 2 Register
SUB Subtract the value in the second Argument register from the first and store the result. Argument 1 Register, Argument 2 Register
MUL Multiply the values in the two Argument registers and store the result. Argument 1 Register, Argument 2 Register
DIV Divide the value in the first Argument register by the second and store the result (quotient). Argument 1 Register, Argument 2 Register
MOD Calculate the remainder (modulus) of the division and store the result. Argument 1 Register, Argument 2 Register
EQ Check if the values in two registers are equal. Set R4 to 0 if equal, 1 if not. Argument 1 Register, Argument 2 Register

Stack Operations:

Instruction Description Arguments
PUSH Push a value from a register onto the stack. Source Register
POP Pop a value from the stack and store it in the destination register. Destination Register
PEEK Copy the value at the top of the stack to the destination register. Destination Register

Control Flow Instructions:

Instruction Description Arguments
JMP Unconditionally jump to the specified line number in the program. Target Instruction Line Number , N/A
JZ Jump to the specified line number if the value in the test register is zero. Target Instruction Line Number, Test Register
JNZ Jump to the specified line number if the value in the test register is not zero. Target Instruction Line Number, Test Register

Memory Access Instructions:

Instruction Description Arguments
LOAD Load a value from the specified memory address into the destination register. Memory Address, Destination Register
STORE Store the value from the source register into the specified memory address. Source Register, Memory Address

Bitwise Instructions:

Instruction Description Arguments
AND Perform a bitwise AND operation on the values in two registers and store the result in R4. Argument 1 Register, Argument 2 Register
OR Perform a bitwise OR operation on the values in two registers and store the result in R4. Argument 1 Register, Argument 2 Register
XOR Perform a bitwise XOR operation on the values in two registers and store the result in R4. Argument 1 Register, Argument 2 Register
NOT Perform a bitwise NOT operation on the value in the Argument register and store the result in R4. Argument Register, N/A
SHL Shift the bits in the Argument register to the left by the specified amount and store the result in R4. Argument Register, Shift Amount
SHR Shift the bits in the Argument register to the right by the specified amount and store the result in R4. Argument Register, Shift Amount

System Calls:

Instruction Description Arguments
SYSCALL Execute the system call specified by the system call number in S1, using the arguments in S2, S3, and S4. System Call Number (S1), Argument 1 (S2), Argument 2 (S3), Argument 3 (S4)

I/O Instructions:

Instruction Description Arguments
PRINTS Print the value at the top of the stack to the console. N/A

Argument Types:

  • Registers: The MVM uses the following register types:
    • G1-G4: General-purpose registers (used for data manipulation).
    • S1-S4: System call registers (used for system call arguments).
    • R1-R4: Return registers (used for storing results of operations and system calls).
  • Memory Addresses: Memory addresses are represented as decimal integers.
  • Literal Values: Literal values are also represented as decimal integers.

Additional Notes:

  • Line Numbering: Line numbers are implicit and start at 1.
  • Case Sensitivity: Instructions and register names are not case-sensitive (e.g., MOV, mov, and Mov are not equivalent).
  • Comments: Are not yet implemented

Examples: TODO. Later!!!!! Im you can see how it works!

Clone this wiki locally