# hvm assembly lanugage reference
hvm assembly language is a pseudo assembly language of hvm, which can be used to generate hvm bytecode, which is more readable and maintainable than hvm bytecode. Some programming languages on hvm are compiled first to generate hvm assembly and then generate hvm bytecode
# Features
- An instruction occupies one line, unless it is a string of lines
- String needs to support escaping
- In addition to the content in the string, the content of the current line after the semicolon is optional'L' + line number +';' + current line comment
- There can be several blanks at the beginning of a line of instructions, and there can be several spaces between the instruction name and the instruction parameters
- .local varname declares the variable name
- Declare constant syntax
.begin_const
"asd"
"Hello"
" world!"
.end_const
- Declare upvalues syntax
.begin_upvalue
1 0
.end_upvalue
- Declare code segment syntax
.begin_code
loadk %0 const "asd" ;L12; %0表示slot 0
closure %1 subroutine_2
move %2 %1
loadk %3 const "Hello"
loadk %4 const " world!"
call %2 3 1
return %0 1
.end_code
- The number of .upvalues declares the number of upvalues
- .func function name maxstacksize params_count use_vararg declares the start of proto, and the number of return values of proto parameter input, etc.
- Loadk, setglobal, return, move and other instructions correspond to the corresponding Lua bytecode instructions, followed by the corresponding parameters
- .end_func ends a proto declaration
- A "do end" block generates a single return instruction
# Instruction list
MOVE Copy a value between registers
LOADK Load a constant into a register
LOADBOOL Load a boolean into a register
LOADNIL Load nil values into a range of registers
GETUPVAL Read an upvalue into a register
GETTABLE Read a table element into a register
SETUPVAL Write a register value into an upvalue
SETTABLE Write a register value into a table element
NEWTABLE Create a new table
SELF Prepare an object method for calling
ADD Addition operator
SUB Subtraction operator
MUL Multiplication operator
DIV Division operator
MOD Modulus (remainder) operator
POW Exponentiation operator
UNM Unary minus operator
NOT Logical NOT operator
LEN Length operator
CONCAT Concatenate a range of registers
JMP Unconditional jump
EQ Equality test
LT Less than test
LE Less than or equal to test
TEST Boolean test, with conditional jump
TESTSET Boolean test, with conditional jump and assignment
CALL Call a closure
TAILCALL Perform a tail call
RETURN Return from function call
FORLOOP Iterate a numeric for loop
FORPREP Initialization for a numeric for loop
TFORLOOP Iterate a generic for loop
SETLIST Set a range of array elements for a table
CLOSURE Create a closure of a function prototype
VARARG Assign vararg function arguments to registers