mvbg

VM_JUMP

VM_JUMP is a fundamental GBVM instruction that unconditionally transfers control of execution to a specified label (address) within the same memory bank.

Purpose: VM_JUMP is the GBVM equivalent of a goto statement in other programming languages. It allows you to alter the sequential flow of your script, enabling:

Unlike VM_CALL, VM_JUMP does not save the return address on the stack. This means that once a VM_JUMP occurs, execution will continue from the new label, and there’s no automatic way to return to the point from which the jump originated. Therefore, it’s typically used for control flow within a single routine or for permanent transitions.

Syntax:

VM_JUMP LABEL

Usage Example: Creating an Infinite Game Loop

A common use case for VM_JUMP is to create the main game loop, which continuously updates the game state, handles input, and renders the screen.

; Main Game Loop
GAME_LOOP:
  ; Handle player input
  VM_CALL HANDLE_INPUT

  ; Update game logic (e.g., character movement, enemy AI)
  VM_CALL UPDATE_GAME_STATE

  ; Render the game screen
  VM_CALL RENDER_SCREEN

  ; Wait for the next frame (VBlank)
  VM_IDLE 1 ; Or a more sophisticated VM_IDLE loop

  VM_JUMP GAME_LOOP ; Jump back to the beginning of the loop

; --- Subroutines (defined elsewhere) ---
HANDLE_INPUT:
  ; ... input handling logic ...
  VM_RET

UPDATE_GAME_STATE:
  ; ... game state update logic ...
  VM_RET

RENDER_SCREEN:
  ; ... rendering logic ...
  VM_RET

In this example, VM_JUMP GAME_LOOP at the end of the GAME_LOOP block creates an infinite loop. After all the input, game state, and rendering routines are executed for one frame, the script jumps back to the GAME_LOOP label to start the process again for the next frame. This forms the heart of many Game Boy games.

Analogy to other programming languages: VM_JUMP is directly analogous to a goto statement in C/C++ or assembly language. While goto is often discouraged in modern high-level programming for readability and maintainability reasons (as structured loops and conditionals are preferred), it is a fundamental control flow primitive in low-level and virtual machine environments like GBVM. It provides a direct, unconditional transfer of control.