C810 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

Element addresses

Element i of an array starting at base with elements of s bytes is at

address=base+s⋅i

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, 4 each pass, and stop when it reaches an end pointer base + 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

LoadSizeFill
lw4 bytesnone needed
lh / lhu2 bytessign / zeros
lb / lbu1 bytesign / 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: 10x=8x+2x 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
  1. Element 7's address. addr=0x1000+4×7=0x101C
  2. 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
  1. Clear the sum and make the end pointer. li t2, 0, then slli t1, a1, 2 and add t1, a0, t1.
  2. 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:
  1. 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
  1. Each character. Each character costs all four instructions: 48.
  2. The terminating zero. It costs the load and the branch, 2 more: N=4n+2=50
  3. Test at the bottom instead. Load, increment, branch back if nonzero: that saves the jump. N=3n+3=39
  4. Library versions. They go further and test four bytes at once with word loads.

Answer 50 instructions, 4n+2; 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?

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.