CSSE 132 In-Progress Homework Help
Homework 3 ARM Tips: Array Scans
Use these hints while working on Homework 3. They explain the approach and testing process without providing the final homework code.
Bring These Ideas Forward from HW02
For both ARM functions, x0 is the base address of an int array and w1 is the element count. Array values are 32-bit ints, so load them with a w register; addresses and byte offsets use x registers. One int takes 4 bytes.
Loop recipe: initialize state, check whether another element exists, load the current value, update state, advance to the next element, and branch back. Keep an index (0, 1, 2, ...) distinct from a byte offset (0, 4, 8, ...).
Useful ARM64 Building Blocks
| Purpose | Pattern | Meaning |
|---|---|---|
| Read an element | ldr w4, [x0, x3] | Load a 4-byte value at base + byte offset. |
| Advance | add x3, x3, 4 | Move offset to the next int. |
| Compare values | cmp w4, w5 | Set flags for a following branch. |
| Choose a larger signed value | b.le label | Branch when the first signed value is less than or equal to the second. |
Return an int | mov w0, w5 | Put the result where the C test expects it. |
Use signed branches for max: HW03 tests negative integers, so an unsigned comparison can produce a surprising answer.
sumArray: Plan Before Code
What this problem is testing
Scanning every element exactly once, adding each value to an accumulator, and returning the accumulator.
Before you write code
Make a register plan on paper. Keep the base in x0, length in w1, byte offset in an x register, current element in a w register, and total in another w register. Do not use w0 for the total until return time: matching x0 holds the base address.
Suggested approach
- Set total and offset to zero.
- Use an index or remaining-count test to decide whether another element exists.
- Load the current element from base + offset.
- Add it to total, then advance the offset by 4.
- After the loop, move total to
w0and return.
Small analogous trace
For {4, -1, 7}, totals should be 0 → 4 → 3 → 10, while offsets are 0 → 4 → 8. Trace this before trusting a test run.
max: Update Only When Needed
What this problem is testing
Keeping a “best value so far” while scanning an array, including negative values.
Before you write code
Initialize the current maximum from the first array element, then compare the remaining elements. Starting it at zero fails for an all-negative array. The homework guarantees a positive length, so reading the first element is valid.
Suggested approach
- Load element 0 as current maximum.
- Advance to element 1 and track how many items were handled.
- Load the next item and compare it with the maximum.
- Replace the maximum only if the new value is greater using a signed comparison.
- After all elements are handled, return the maximum in
w0.
Small analogous trace
For {-8, -3, -12}, the maximum changes -8 → -3 → -3. If your approach returns 0, it was not initialized from the first item or it treats values as unsigned.
Build and Test the Right Part
Assembly functions
make test_asm
./test_asmThis builds problems.s and runs the tests for sumArray and max.
C functions
make test
./testThis separately builds problems.c. A C test failure does not diagnose your ARM code.
Use make clean and rebuild after changing assembly if needed. An assembler error points to ARM syntax; a linker error usually means a required function name is missing or does not match; a failed Unity assertion means the program ran but returned the wrong value.
Segmentation fault: inspect every memory access. Confirm the base is an x register, the offset is measured in bytes, and the loop stops before length × 4.
Common Failure Modes
Offset 1 reaches the second byte, not the second
int.Labels are file-wide; give the two functions different loop and end labels.
This breaks all-negative arrays. Seed it from the first item.
Writing
w0 changes matching x0; preserve the base until loads finish.Check Your Understanding
- Why is an array address in
x0, but an element loaded intow4? - What byte offset selects
array[5]? - Why should
maxuse a signed branch?
Answers
Addresses are 64-bit while each int is 32-bit. The offset is 5 × 4 = 20 bytes. Signed comparison correctly orders negative int values.