5.27 · exercise

the forgetful helper

warning

This program is broken. Find the bug and fix it so the checks pass.

main keeps a running total of 100 in x19, asks a helper to double a bonus of 20, and adds the result. The total should be 140. Run it: the program says something else entirely.

The helper works. The arithmetic works. The bug is a broken promise: AAPCS64 says x19 through x28 belong to the caller, so a subroutine that touches them must save and restore them. Scratch work belongs in x9 through x15, which the caller must assume are gone after any bl.

Fix the helper so it stops trampling its caller's registers.

what is checked

  • the correct total is printed
  • the program exits cleanly
  • the helper is still called (bl present)

Specification

args
stdoutprints the expected output
exitexits with the expected code
sourceuses bl

Checked by running your program against expected behavior, never by matching a stored solution.

loading editor...
Open in playground