-
Notifications
You must be signed in to change notification settings - Fork 296
/
SYNTAX
204 lines (140 loc) · 5.68 KB
/
SYNTAX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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