The preprocessor section of the SYNTAX file has been rewritten to match the other sections in style, and include both %include and %define directives. #27
204 lines
5.7 KiB
Text
204 lines
5.7 KiB
Text
This virtual machine loosely follows traditional Intel x86 assembly syntax.
|
|
|
|
//////////////////////////////////////////////////
|
|
// Table of Contents /////////////////////////////
|
|
//////////////////////////////////////////////////
|
|
0. VALUES
|
|
1. REGISTERS
|
|
2. MEMORY
|
|
3. LABELS
|
|
4. PREPROCESSOR
|
|
5. INSTRUCTION LISTING
|
|
I. Memory
|
|
II. Stack
|
|
III. Calling Conventions
|
|
IV. Arithmetic Operators
|
|
V. Binary Operators
|
|
VI. Comparison
|
|
VII. Control Flow Manipulation
|
|
VIII. Input / Output
|
|
|
|
//////////////////////////////////////////////////
|
|
// 0. VALUES /////////////////////////////////////
|
|
//////////////////////////////////////////////////
|
|
|
|
Values can be specified in decimal, hexadecimal, or binary. The only difference between Intel syntax assembly and TVM
|
|
syntax is the delimiter between the value and the base specifier. By default, values without a base specifier are assumed
|
|
to be in decimal. Any value prepended with "0x" is assumed to be in hexadecimal.
|
|
|
|
Values can also be specified using base identifiers. To specify the value "32," I can use 0x20 for hexadecimal, 20|h for hexadecimal
|
|
using a base identifier, or 100000|b for binary using a base identifer.
|
|
|
|
//////////////////////////////////////////////////
|
|
// 1. REGISTERS //////////////////////////////////
|
|
//////////////////////////////////////////////////
|
|
|
|
TVM has 17 registers, modeled after x86 registers.
|
|
Register names are written lower-case.
|
|
|
|
(EAX - EDX, General Purpose)
|
|
EAX
|
|
EBX
|
|
ECX
|
|
EDX
|
|
|
|
ESI
|
|
EDI
|
|
|
|
ESP - Stack pointer, points to the top of the stack
|
|
EBP - Base pointer, points to the base of the stack
|
|
|
|
EIP - Instruction pointer, this is modified with the jump commands, never directly
|
|
|
|
R08 - R15, General Purpose
|
|
|
|
//////////////////////////////////////////////////
|
|
// 2. MEMORY /////////////////////////////////////
|
|
//////////////////////////////////////////////////
|
|
|
|
Memory addresses are specified using brackets, in units of four bytes. Programs running within the virtual machine have their own
|
|
address space, so no positive address within the address space is off limits.
|
|
|
|
It is important to note that certain areas of memory are used for vital program function, such as the memory used by the stack.
|
|
Overwriting such parts of memory will likely cause the program to crash. As such, it is recommended that no absolute addressing be used.
|
|
Instead, address memory relatively.
|
|
|
|
To specify the 256th word in the address space, you can use [256], [100|h], [0x100], or [100000000|b]. Any syntax that's
|
|
valid when specifying a value is valid when specifying an address.
|
|
|
|
//////////////////////////////////////////////////
|
|
// 3. LABELS /////////////////////////////////////
|
|
//////////////////////////////////////////////////
|
|
|
|
Labels are specified by appending a colon to an identifier. Local labels are not yet supported.
|
|
|
|
Labels must be specified at the beginning of a line or on their own line.
|
|
|
|
//////////////////////////////////////////////////
|
|
// 4. PREPROCESSOR ///////////////////////////////
|
|
//////////////////////////////////////////////////
|
|
|
|
TVM's preprocessor works similarly to many C compilers and uses the prefix "%".
|
|
|
|
// I. Include //
|
|
|
|
%include filename
|
|
Pastes all of the contents of filename into the source code before interpretting it.
|
|
|
|
// II. Define //
|
|
|
|
%define identifier value
|
|
Define a constant so that all instances of the string "identifier" will be replaced by "value".
|
|
|
|
//////////////////////////////////////////////////
|
|
// 5. INSTRUCTION LISTING ////////////////////////
|
|
//////////////////////////////////////////////////
|
|
|
|
Instructions listed are displayed in complete usage form, with example arguments, enclosed in square brackets.
|
|
The square brackets are not to be used in actual TVM programs.
|
|
|
|
// I. Memory //
|
|
|
|
[mov arg0, arg1]
|
|
Moves value specified from arg1 to arg0
|
|
|
|
// II. Stack //
|
|
|
|
[push arg]
|
|
Pushes arg onto the stack
|
|
|
|
[pop arg]
|
|
Pops a value from the stack, storing it in arg
|
|
|
|
[pushf]
|
|
Pushes the FLAGS register to the stack
|
|
|
|
[popf arg]
|
|
Pops the flag register to arg
|
|
|
|
// III. Calling Conventions //
|
|
|
|
[call address]
|
|
Push the current address to the stack and jump to the subroutine specified
|
|
|
|
[ret]
|
|
Pop the previous address from the stack to the instruction pointer to return control to the caller
|
|
|
|
// IV. Arithmetic Operators //
|
|
|
|
[inc arg]
|
|
Increments arg
|
|
|
|
[dec arg]
|
|
Decrements arg
|
|
|
|
[add arg0, arg1]
|
|
Adds arg1 to arg0, storing the result in arg0
|
|
|
|
[sub arg0, arg1]
|
|
Subtracts arg1 from arg0, storing the result in arg0
|
|
|
|
[mul arg0, arg1]
|
|
Multiplies arg1 and arg0, storing the result in arg0
|
|
|
|
[div arg0, arg1]
|
|
Divides arg0 by arg1, storing the quotient in arg0
|
|
|
|
[mod arg0, arg1]
|
|
Same as the '%' (modulus) operator in C. Calculates arg0 mod arg1 and stores the result in the remainder register.
|
|
|
|
[rem arg]
|
|
Retrieves the value stored in the remainder register, storing it in arg
|
|
|
|
// V. Binary Operators //
|
|
|
|
[not arg]
|
|
Calculates the binary NOT of arg, storing it in arg
|
|
|
|
[xor arg0, arg1]
|
|
Calculates the binary XOR of arg0 and arg1, storing the result in arg0
|
|
|
|
[or arg0, arg1]
|
|
Calculates the binary OR of arg0 and arg1, storing the result in arg0
|
|
|
|
[and arg0, arg1]
|
|
Calculates the binary AND of arg0 and arg1, storing the result in arg0
|
|
|
|
[shl arg0, arg1]
|
|
Shift arg0 left by arg1 places
|
|
|
|
[shr arg0, arg1]
|
|
Shifts arg0 right by arg1 places
|
|
|
|
// VI. Comparison //
|
|
|
|
[cmp arg0, arg1]
|
|
Compares arg0 and arg1, storing the result in the FLAGS register
|
|
|
|
// VII. Control Flow Manipulation //
|
|
|
|
[jmp address]
|
|
Jumps to an address or label
|
|
|
|
[je address]
|
|
Jump if equal
|
|
|
|
[jne address]
|
|
Jump if not equal
|
|
|
|
[jg address]
|
|
Jump if greater
|
|
|
|
[jge address]
|
|
Jump if equal or greater
|
|
|
|
[jl address]
|
|
Jump if lesser
|
|
|
|
[jle address]
|
|
Jump if lesser or equal
|
|
|
|
// VIII. Input / Output //
|
|
|
|
[prn arg]
|
|
Print an integer
|