C8 · Lesson 10 · Arrays and strings in assembly
Walking through memory one element at a time
Most of what programs do is loop over data: add up a list of readings, find the largest, copy a buffer, count the characters in a message. In assembly that means keeping an address in a register, loading from it, and moving it along by the size of one element each time, until a counter runs out or a zero byte marks the end. The details that trip people up are all about sizes and signs: words are four bytes apart, characters one; a byte loaded with lb is sign-extended and with lbu it isn't; and comparing addresses or sizes needs the unsigned branches.
- A loop that measures the length of the string "hello, world": it loads one byte at a time with
lbu, stops at the terminating zero, and counts. - Step it: t0 walks along the string one byte per pass, and a0 counts. The loop ends after 12 characters.
- The string is stored as 13 bytes in memory, the last one 0. Watch the memory view as t0 moves.
Predict
The byte 0xE9 (é in Latin-1) is loaded with lb. What value does the register hold?
−23: lb treats the byte as a signed number and copies its top bit into the upper 24 bits, giving 0xFFFFFFE9. lbu would give 233 (0x000000E9). Text and raw bytes should use lbu, or a character above 127 turns negative.
Name it
Element addresses
Element i of an array starting at base with elements of s bytes is at
so for words (s = 4) a compiler uses slli t1, i, 2 and an add. Two styles of loop:
- Indexing: keep i, recompute base + 4i each pass.
- Pointer walking: keep a pointer,
addi p, p, 4each pass, and stop when it reaches an end pointerbase + 4n. One instruction fewer per pass; compilers prefer it.
Strings
A C string is a sequence of bytes ending in a zero byte (.asciz adds it). Its length is found by walking until the zero. Each character is one byte, so the pointer moves by 1.
Sizes and signs
| Load | Size | Fill |
|---|---|---|
lw | 4 bytes | none needed |
lh / lhu | 2 bytes | sign / zeros |
lb / lbu | 1 byte | sign / zeros |
Stores (sw, sh, sb) just write the low bytes. Branches come in signed (blt, bge) and unsigned (bltu, bgeu) forms; addresses, sizes and counts are unsigned.
Multiplying by a constant
Shifts and adds are cheaper than a multiply on small processors: is slli t0, x, 3, slli t1, x, 1, add.
Worked example
An array of words starts at 0x1000. Where is element 7, and how does a loop reach it by pointer walking?
Show the solution
- Element 7's address.
- By pointer walking. A walking pointer starts at 0x1000 and adds 4 each pass, so after 7 passes it holds 0x101C.
Answer element 7 is at 0x101C, which the walking pointer reaches after 7 passes.
Worked example
Write the inner loop that sums n words starting at the address in a0, with n in a1.
Show the solution
- Clear the sum and make the end pointer.
li t2, 0, thenslli t1, a1, 2andadd t1, a0, t1. - Write the loop. It stops when the pointer reaches the end pointer.
loop:
beq a0, t1, done
lw t0, 0(a0)
add t2, t2, t0
addi a0, a0, 4
j loop
done:
- Count. Four instructions do the work, plus the jump.
Answer after the setup, the loop is beq a0, t1, done, lw t0, 0(a0), add t2, t2, t0, addi a0, a0, 4 and j loop, with the sum in t2.
Worked example
A string-length loop is lbu t0, 0(a0), beqz t0, done, addi a0, a0, 1, j loop. How many instructions does it execute on a 12-character string?
Show the solution
- Each character. Each character costs all four instructions: 48.
- The terminating zero. It costs the load and the branch, 2 more:
- Test at the bottom instead. Load, increment, branch back if nonzero: that saves the jump.
- Library versions. They go further and test four bytes at once with word loads.
Answer 50 instructions, ; testing at the bottom brings it to 39.
Check
An array of 8-byte elements starts at 1000. What is the address of element 3?
Check
The byte AD is loaded with lb. What signed value does the register hold?
Check
Add the five words of the array vals and leave the sum in a0.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
With n in a0, put 10 × n in a1 using shifts and adds (no multiply).
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
Count the characters in the string text (not counting its terminating zero) and leave the count in a0.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
A loop compares an address in t0 against an end address in t1. Which branch should it use?
Check
The array vals holds the words 1, 2, 3, and so on. With its address in a0 and a count n in a1, put the sum of the first n words into a2.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
A string of 12 characters is measured by a loop of four instructions per character, plus two for the terminating zero.
(a)
How many instructions run?
(b)
And with the test moved to the bottom (load, increment, branch back), plus 3 instructions around it?
Explain why
Why do compilers prefer walking a pointer through an array to recomputing base + 4i on every pass?
Indexing needs a shift and an add on every pass to turn the index into an address, on top of incrementing the index. A pointer that already holds the current element's address only needs one add of the element size each pass, and the loop can stop by comparing the pointer against a precomputed end address instead of comparing a separate counter. The work done per element shrinks by a couple of instructions, which in a tight loop is a large fraction of the total.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.