Memory, Arrays and Loads¶
A pointer contains an address; a load reads bytes at that address. Walk a C array while keeping the length, element width and accumulation width separate.
Course examples on GitHub
Before you start¶
Complete branches and loops. You can use a zero-length guard and trace a loop. Work in a fresh lesson directory on the Pi.
Outcome: sum valid array elements without reading an empty array, and distinguish zero-extension from sign-extension.
Address, element and byte count¶
For this native 64-bit target, a pointer occupies eight bytes. An int32_t element occupies four. Advancing a pointer to the next element therefore adds 4, not 1 or 8.
| Form | Effect |
|---|---|
ldr w2, [x0] |
Read four bytes, zero-extend into x2 |
ldrsw x2, [x0] |
Read four bytes, sign-extend into x2 |
ldr x2, [x0] |
Read eight bytes |
str w2, [x0] |
Write four bytes |
[x0], #4 |
Access the current address, then advance by four |
Assembly receives no automatic array bounds. A nonzero length requires a readable array of that many elements. Null is accepted only when the length is zero, because no access occurs. The code does not validate arbitrary pointers.
Build the memory lab¶
This routine sums signed 32-bit elements into a 64-bit accumulator. The caller limits the length to 1024, which also keeps the mathematical sum within int64_t.
Read the loop as a contract¶
At loop entry, x0 points to the next unread element and x1 is the remaining count. x2 holds the sum of elements already read. Each iteration consumes exactly one element. The zero guard is what makes an empty range safe, not a special property of address zero.
Changing ldrsw to ldr w3 makes the negative test fail: −1 becomes 4294967295 in the accumulator. Changing the stride to eight skips elements and can overrun the array. Do not intentionally run an out-of-bounds experiment; inspect the address calculation on paper or in GDB.
For stores, prove the output range is writable and large enough before entering the loop. Readability, writability and alignment are distinct obligations. These examples use naturally aligned C arrays, not device registers or packed structures.
Check your understanding¶
What happens if you double the byte stride without changing the input length? Why is null plus zero length acceptable here?
Compare your reasoning
The routine would skip elements and eventually read outside the array. Null with zero length is never dereferenced because the branch goes directly to the return path.
Continue the course¶
Previous: Flags, Branches and Loops · Course roadmap · Next: Addresses and Relocations