5.7 · exercise

Change for a twenty

A customer pays with a twenty-dollar bill, 2000 cents. The starter reads the price in cents, from 0 to 2000. Work out the change, then split it into as few coins as possible: as many quarters (25 cents) as fit, then dimes (10), then nickels (5), then pennies (1). Leave the results in change_r, quarters_r, dimes_r, nickels_r and pennies_r; the starter prints them.

AArch64 has no remainder instruction. sdiv gives how many whole times one number fits in another, and msub multiplies two registers and subtracts the product from a third, which is exactly the leftover:

sdiv    w9, w12, w10        // w9 = w12 / w10, fraction dropped
msub    w13, w9, w10, w12   // w13 = w12 - w9 * w10, the remainder

For a price of 1541 the change is 459 cents: 18 quarters make 450, which leaves 9 cents, and 9 cents is 0 dimes, 1 nickel and 4 pennies.

What is checked

  • with input 1541 the output is change in cents: 459, then 25c x 18, 10c x 0, 5c x 1 and 1c x 4, one per line
  • the program exits cleanly
  • the leftovers come from msub
  • the program also runs on prices you do not see, including one that needs no change at all and one that needs every kind of coin

specification

stdin1541
stdoutprints the right output
exitexits with the right code
sourceuses msub
hiddenright output and exit code on 5 more inputs you do not see

We run your program on the input above and on the hidden ones, and compare what it does. Nothing is matched against a stored solution.

loading editor...

regfile

N clearZ clearC clearV clear

x0–x30 are the integer registers.

X0arg00x0000000000000000
X1arg10x0000000000000000
X2arg20x0000000000000000
X3arg30x0000000000000000
X4arg40x0000000000000000
X5arg50x0000000000000000
X6arg60x0000000000000000
X7arg70x0000000000000000
X8ind0x0000000000000000
X90x0000000000000000
X100x0000000000000000
X110x0000000000000000
X120x0000000000000000
X130x0000000000000000
X140x0000000000000000
X150x0000000000000000
X16ip00x0000000000000000
X17ip10x0000000000000000
X18pr0x0000000000000000
X190x0000000000000000
X200x0000000000000000
X210x0000000000000000
X220x0000000000000000
X230x0000000000000000
X240x0000000000000000
X250x0000000000000000
X260x0000000000000000
X270x0000000000000000
X280x0000000000000000
X29fp0x0000000000000000
X30lr0x0000000000000000
SP0x0000000080000000
PC0x0000000000400000
console

Output prints here as your program runs.

Press step or run under the editor, or feed stdin from the box below.

not assembled

Open in playground