a$ op b$ where op is =, <>, <, >, <=, or >=pb_cg_emit_string_comparison_expr (pb_codegen.c:4930)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:
min(lenL, lenR) bytes are compared using memcmp. If the prefixes differ, the memcmp result determines the ordering.All six relational operators are supported: = (equal), <> (not equal), < (less than), > (greater than), <= (less or equal), >= (greater or equal).
| Parameter | Description |
|---|---|
a$ | Left-hand STRING operand. Can be a literal, variable, or expression. |
b$ | Right-hand STRING operand. Can be a literal, variable, or expression. |
A boolean (LONG) result: -1 (TRUE) or 0 (FALSE) when used in an IF condition or boolean expression.
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
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
memcmp compares raw byte values. Uppercase 'A' (65) is less than lowercase 'a' (97). Use UCASE$() or LCASE$() for case-insensitive comparison.VAL() first.memcmp function returns a 32-bit signed integer. The compiler sign-extends it to 64-bit (movsxd) before using it.je/jne/jl/jg/jle/jge) depends on which relational operator was used in the source. The comparison result in RAX is 0 for equal, negative if left < right, positive if left > right.memcmp.