String Concatenation (+)

Syntax

Syntax: result$ = a$ + b$ [+ c$ + ...]
Source: pb_cg_emit_string_concat_to_heap_temp (pb_codegen.c:3648)

Description

Concatenates two or more strings using the + operator. The compiler generates a two-phase process: first it computes the total length of all operands, then it allocates a heap buffer of that size, copies each operand sequentially with memcpy, and NUL-terminates the result.

String concatenation is a fundamental operation for building output messages, constructing file paths, composing SQL queries, generating HTML/XML, and assembling log entries. The + operator can chain any number of string operands in a single expression.

The result string is allocated on the heap. The string descriptor of the target variable is updated to point to this heap buffer, and the old buffer (if any) is freed. Each + operation produces a new allocation; for heavy concatenation in loops, consider building with incremental copies or pre-allocating a buffer.

Parameters

ParameterDescription
a$, b$, c$STRING operands to concatenate. Can be string literals, variables, function return values, or any string expression.

Return Value

A new STRING containing the concatenation of all operands in left-to-right order.

Example

FUNCTION PBMAIN() AS LONG
    LOCAL a$, b$, c$, result$ AS STRING
    
    ' Basic concatenation of two strings
    a$ = "Hello"
    b$ = " World"
    result$ = a$ + b$            ' result$ = "Hello World"
    PRINT result$
    
    ' Multiple concatenation
    result$ = "The " + "quick " + "brown " + "fox"
    PRINT result$               ' "The quick brown fox"
    
    ' Mixing literals and variables
    a$ = "Power"
    b$ = "BASIC"
    result$ = a$ + " " + b$ + " Compiler"
    PRINT result$               ' "Power BASIC Compiler"
    
    ' Building a file path
    LOCAL folder$, file$ AS STRING
    folder$ = "C:\Data\Reports"
    file$ = "summary.txt"
    result$ = folder$ + "\" + file$
    PRINT "Path: "; result$     ' "C:\Data\Reports\summary.txt"
    
    ' Numeric-to-string concatenation with STR$
    LOCAL x AS LONG
    x = 42
    result$ = "Value = " + STR$(x)
    PRINT result$               ' "Value = 42"
    
    ' Concatenation with string functions
    a$ = "hello"
    result$ = UCASE$(LEFT$(a$, 1)) + LCASE$(MID$(a$, 2))
    PRINT result$               ' "Hello" (proper case)
    
    ' Append to existing string
    a$ = "Line 1"
    a$ = a$ + CHR$(13) + CHR$(10) + "Line 2"
    PRINT a$
    
    FUNCTION = 0
END FUNCTION

Generated Assembly

Source: pb_cg_emit_string_concat_to_heap_temp (pb_codegen.c:3648)

Phase 1 - Compute Total Length

xor    rax, rax
; For each part: get address -> RAX, length -> R10
mov    [rbp - ptr_offset_i], rax       ; save part pointer
mov    [rbp - len_offset_i], r10       ; save part length
add    [rbp - total_bytes], r10        ; total += len

Phase 2 - Allocate + Copy

mov    rcx, [rbp - total_bytes]
add    rcx, 8                          ; + padding for NUL
call   [__imp_malloc]                  ; allocate buffer
mov    [rbp - out_ptr], rax
; For each part:
mov    rcx, [rbp - out_ptr]
add    rcx, [rbp - write_pos]          ; dest = base + pos
mov    rdx, [rbp - ptr_offset_i]       ; src = part data
mov    r8, [rbp - len_offset_i]        ; count = part length
call   [__imp_memcpy]                  ; copy
add    [rbp - write_pos], r8           ; pos += len
; NUL-terminate:
mov    r11, [rbp - out_ptr]
add    r11, [rbp - total_bytes]
mov    byte [r11], 0

Notes