String Comparison

Syntax

Syntax: a$ op b$ where op is =, <>, <, >, <=, or >=
Source: pb_cg_emit_string_comparison_expr (pb_codegen.c:4930)

Description

Compares two strings lexicographically (dictionary order) using case-sensitive comparison. String comparison is the foundation of sorting, searching, conditional branching, and data validation.

The compiler generates a two-stage comparison:

  1. Prefix comparison: The first min(lenL, lenR) bytes are compared using memcmp. If the prefixes differ, the memcmp result determines the ordering.
  2. Length comparison: If the prefixes are identical (one string is a prefix of the other), the comparison falls back to length: the shorter string is considered "less than" the longer string.

All six relational operators are supported: = (equal), <> (not equal), < (less than), > (greater than), <= (less or equal), >= (greater or equal).

Parameters

ParameterDescription
a$Left-hand STRING operand. Can be a literal, variable, or expression.
b$Right-hand STRING operand. Can be a literal, variable, or expression.

Return Value

A boolean (LONG) result: -1 (TRUE) or 0 (FALSE) when used in an IF condition or boolean expression.

Example

FUNCTION PBMAIN() AS LONG
    LOCAL a$, b$ AS STRING
    
    a$ = "Apple"
    b$ = "Banana"
    
    ' Equality test
    IF a$ = b$ THEN
        PRINT "Equal"
    ELSE
        PRINT "Not equal"            ' prints this
    END IF
    
    ' Not equal test
    IF a$ <> b$ THEN
        PRINT "Different"            ' prints this
    END IF
    
    ' Less than (lexicographic order)
    IF a$ < b$ THEN
        PRINT "Apple comes before Banana"  ' prints this (A < B)
    END IF
    
    ' Greater than
    IF b$ > a$ THEN
        PRINT "Banana comes after Apple"   ' prints this
    END IF
    
    ' Prefix comparison: "Hello" vs "Hello World"
    a$ = "Hello"
    b$ = "Hello World"
    IF a$ < b$ THEN
        PRINT "Shorter string is less"     ' prints this
    END IF
    
    ' Case sensitivity: 'a' > 'A' in ASCII
    a$ = "apple"
    b$ = "Apple"
    IF a$ > b$ THEN
        PRINT "Lowercase is greater"       ' prints this (97 > 65)
    END IF
    
    ' Practical: sorting in IF/ELSEIF chain
    LOCAL name$ AS STRING
    name$ = "Charlie"
    
    IF name$ < "M" THEN
        PRINT "A-M group"            ' prints this
    ELSE
        PRINT "N-Z group"
    END IF
    
    ' Case-insensitive comparison using UCASE$
    a$ = "PowerBASIC"
    b$ = "powerbasic"
    IF UCASE$(a$) = UCASE$(b$) THEN
        PRINT "Case-insensitive match"     ' prints this
    END IF
    
    FUNCTION = 0
END FUNCTION

Generated Assembly

Source: pb_cg_emit_string_comparison_expr (pb_codegen.c:4930)
; Compare first min(lenL, lenR) bytes with memcmp
mov    rcx, [rbp - left_ptr]
mov    rdx, [rbp - right_ptr]
mov    r8, min_len
call   [__imp_memcmp]
movsxd rax, eax                        ; sign-extend 32-bit result
cmp    rax, 0
jne    __pb_str_cmp_ready_N            ; different -> use memcmp result
; Prefix equal, compare lengths:
mov    rax, [rbp - left_len]
cmp    rax, [rbp - right_len]
; 0 if equal, -1 if left shorter, 1 if left longer
; Then comparison jump: je/jne/jl/jg/jle/jge __pb_str_true_N

Notes