GLOBAL Declaration
Syntax:
GLOBAL name AS typeGLOBAL 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
| Parameter | Description |
|---|---|
name | The identifier name of the variable. Must be unique at the global scope and must not conflict with any procedure name. |
AS type | The data type. Supported types include LONG, DWORD, INTEGER, BYTE, WORD, SINGLE, DOUBLE, QUAD, CUR, EXT, STRING, and user-defined TYPEs. |
subscripts | Optional. 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
- Global variables are initialized to zero (numeric) or empty string before
PBMAINis called. - If a procedure declares a
LOCALorSTATICvariable with the same name as a global, the local variable shadows (hides) the global within that procedure. - Global variables increase the working set size of the executable. Use them only when data truly needs program-wide visibility.
- For DLL projects, exported global variables require special handling and may need the
EXPORTmodifier. - PBXA64 does not support
THREAD GLOBALor thread-local global storage in the current version.