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

PurposePatternMeaning
Read an elementldr w4, [x0, x3]Load a 4-byte value at base + byte offset.
Advanceadd x3, x3, 4Move offset to the next int.
Compare valuescmp w4, w5Set flags for a following branch.
Choose a larger signed valueb.le labelBranch when the first signed value is less than or equal to the second.
Return an intmov w0, w5Put 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

  1. Set total and offset to zero.
  2. Use an index or remaining-count test to decide whether another element exists.
  3. Load the current element from base + offset.
  4. Add it to total, then advance the offset by 4.
  5. After the loop, move total to w0 and 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

  1. Load element 0 as current maximum.
  2. Advance to element 1 and track how many items were handled.
  3. Load the next item and compare it with the maximum.
  4. Replace the maximum only if the new value is greater using a signed comparison.
  5. 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_asm

This builds problems.s and runs the tests for sumArray and max.

C functions

make test
./test

This 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

Index used as byte offset
Offset 1 reaches the second byte, not the second int.
Labels collide
Labels are file-wide; give the two functions different loop and end labels.
Maximum starts at 0
This breaks all-negative arrays. Seed it from the first item.
Pointer overwritten
Writing w0 changes matching x0; preserve the base until loads finish.

Check Your Understanding

  1. Why is an array address in x0, but an element loaded into w4?
  2. What byte offset selects array[5]?
  3. Why should max use 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.