Advanced
String Processing
Working with strings: length, comparison, and manipulation.
Strings in LC-3
Strings are stored as sequences of ASCII values in consecutive memory locations, terminated by a null character (0x0000). The .STRINGZ directive creates these.
String Length
Walk through the string until you hit the null terminator, counting characters:
.ORIG x3000
LEA R0, STR
PUTS ; Print the string
; Compute length
LEA R1, STR ; R1 = pointer
AND R2, R2, #0 ; R2 = length counter
STRLEN_LOOP
LDR R3, R1, #0 ; Load current char
BRz STRLEN_DONE ; If null, done
ADD R2, R2, #1 ; length++
ADD R1, R1, #1 ; pointer++
BR STRLEN_LOOP
STRLEN_DONE
; R2 now holds the string length
HALT
STR .STRINGZ "Hello"
.ENDCharacter Case Conversion
ASCII uppercase letters are 0x41-0x5A (65-90).
ASCII lowercase letters are 0x61-0x7A (97-122).
The difference is exactly 32 (0x20).
Since LC-3 doesn't have OR, we use the De Morgan trick or just add/subtract 32.
.ORIG x3000
; Convert uppercase to lowercase
; 'A' (65) + 32 = 'a' (97)
LEA R1, STR
LOOP
LDR R0, R1, #0
BRz DONE
; Add 32 to make lowercase
ADD R0, R0, #15
ADD R0, R0, #15
ADD R0, R0, #2 ; +32
OUT
ADD R1, R1, #1
BR LOOP
DONE HALT
STR .STRINGZ "HELLO"
.ENDExercise
Write a program that reverses a string. Load the string "ABCDE", reverse it in-place, then print it. Hint: find the length first, then swap characters from the ends toward the middle.
1
R0
x00000
R1
x00000
R2
x00000
R3
x00000
R4
x00000
R5
x00000
R6
x00000
R7
x00000
PCx3000
CC
NZP