mvbg

VM_PUSH_CONST

VM_PUSH_CONST is a fundamental GBVM instruction used to push an immediate (constant) value onto the top of the VM stack.

Purpose: The GBVM is a stack-based virtual machine, and many operations require values to be present on the stack before they can be executed. VM_PUSH_CONST is essential for:

This instruction is one of the most basic ways to get data onto the stack for subsequent processing by other GBVM instructions, especially within RPN (Reverse Polish Notation) expressions.

Syntax:

VM_PUSH_CONST VAL

Usage Example: Performing a Simple Calculation with Constants

Imagine you want to calculate 5 + 3 and store the result in a variable VAR_RESULT. You would push the constants onto the stack and then use an RPN operation.

; In your script:

; Variable to store the result
VAR_RESULT:
  .R_INT8 0 ; Initialize to 0

; Push the constant values onto the stack
VM_PUSH_CONST 5
VM_PUSH_CONST 3

; Perform the addition using RPN
VM_RPN
  .R_OPERATOR + ; Add the two values (5 and 3) currently on the stack
  .R_INT8       ; Specify the result as an 8-bit integer
VM_STOP

; The result (8) is now on top of the stack.
; Store the result into VAR_RESULT
VM_SET_INT8 VAR_RESULT, 0 ; The 0 here is a placeholder, as VM_SET_INT8 takes the value from stack

; ... continue script ...

In this example:

  1. VM_PUSH_CONST 5 places the value 5 on the stack.
  2. VM_PUSH_CONST 3 places the value 3 on top of 5.
  3. The VM_RPN block then uses these values. .R_OPERATOR + takes the top two values (3 and 5), adds them, and pushes the result (8) back onto the stack.
  4. VM_SET_INT8 VAR_RESULT, 0 (the 0 is a placeholder for the value that will be taken from the stack) then takes the 8 from the stack and stores it into VAR_RESULT.

Analogy to other programming languages: This is analogous to directly using a literal number in an expression:

In assembly language, it’s like an LDI (Load Immediate) instruction that puts a constant value into a register or onto the stack. It’s the most direct way to introduce fixed data into your script’s execution flow.