GLOBAL Declaration

Syntax:
GLOBAL name AS type
GLOBAL name() AS type [subscripts]

Description

The GLOBAL statement declares variables that are visible and accessible from all procedures throughout the entire program. Global variables exist for the lifetime of the program and provide a mechanism for sharing data between different functions and subroutines without passing parameters.

Global variables must be declared in the main source file, outside of any FUNCTION or SUB block. They are typically placed near the top of the file, after any #COMPILE or #INCLUDE directives but before any procedure definitions.

While global variables are convenient, they should be used sparingly. Excessive use of globals can lead to code that is difficult to debug and maintain due to unexpected side effects.

Parameters

ParameterDescription
nameThe identifier name of the variable. Must be unique at the global scope and must not conflict with any procedure name.
AS typeThe data type. Supported types include LONG, DWORD, INTEGER, BYTE, WORD, SINGLE, DOUBLE, QUAD, CUR, EXT, STRING, and user-defined TYPEs.
subscriptsOptional. Specifies array bounds for global arrays. Same syntax as LOCAL arrays.

Example

#COMPILE EXE

GLOBAL g_AppName AS STRING
GLOBAL g_MaxRetries AS LONG
GLOBAL g_Config(1 TO 10) AS STRING

SUB InitializeApp()
    g_AppName = "PBXA64 Application"
    g_MaxRetries = 3
    g_Config(1) = "OptionA"
    g_Config(2) = "OptionB"
END SUB

FUNCTION GetAppInfo() AS STRING
    FUNCTION = g_AppName + " (Retries: " + STR$(g_MaxRetries) + ")"
END FUNCTION

FUNCTION PBMAIN() AS LONG
    InitializeApp()
    PRINT GetAppInfo()

    FUNCTION = 0
END FUNCTION

Generated Assembly

Data Section Placement

; GLOBAL variables are placed in the .data section of the PE binary:
section .data
    g_AppName      db 256 dup(0)      ; string buffer
    g_MaxRetries   dq 0               ; initialized to 0
    g_Config       dq 10 dup(0)       ; array of 10 string descriptors

Accessing GLOBAL Variables

; Read: x = g_MaxRetries
mov    rax, [g_MaxRetries]            ; direct memory access

; Write: g_MaxRetries = 3
mov    rax, 3
mov    [g_MaxRetries], rax            ; store to global memory

; String assignment: g_AppName = "PBXA64"
lea    rdi, [g_AppName]
lea    rsi, [__pb_str_const_1]
mov    rcx, 256
call   [__imp_memcpy]                 ; copy string to global buffer

; Array element: g_Config(1) = "OptionA"
; Calculates offset for index 1 and stores string descriptor

Notes