Beginner

String Length

Find the length of a null-terminated string in memory.

Write a program to find the length of a null-terminated string stored in memory starting at location x5000. Store the length (number of characters, not including the null terminator) in memory location x6000.

How to Think About This Problem:

Step 1: Understand what you have
• A string starting at x5000

• Each character is at consecutive memory addresses: x5000, x5001, x5002...

• The string ends with a null character (x00)

Step 2: What do you need?
• Count how many characters before the null

• Store that count at x6000

Step 3: Break it into pieces
1. Set up a pointer to the start of the string
2. Set up a counter (starts at 0)
3. Load the character at the pointer
4. If it's null, we're done
5. If not null, increment counter, move pointer forward, repeat

Step 4: Map to LC-3
• R1 = pointer (address), starts at x5000

• R2 = counter, starts at 0

• Use LDR R0, R1, #0 to load character at address R1

• Use BRz to check for null (null = 0, so BRz triggers)

Useful patterns:

LD R1, PTR        ; Load x5000 into R1
LDR R0, R1, #0   ; Load character at address R1
STI R2, RESULT    ; Store R2 to address stored at RESULT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15