5.52 · exercise

Two answers, one call

A function hands back one value in x0, but a division has two answers. divmod(a, b, &q, &r) gets both out: the caller (the code making the call) passes the addresses of two of its own variables, and divmod writes the quotient and the remainder into them. It returns 0 in w0 when it worked, and -1 when b is 0, in which case it writes nothing.

The starter's main reads pairs of numbers into its frame until the input runs out, and prints each result from the frame slots q_s and r_s. Write divmod, and fill in the call in main: a in x0, b in x1, and the addresses of the two slots in x2 and x3 (add x2, fp, q_s puts an address in a register).

Use sdiv, which rounds toward zero the way C does, so -17 / 5 is -3 remainder -2. The remainder is a - q * b, one msub. Check b first: dividing by zero does not stop an ARMv8 program, sdiv quietly answers 0.

main exits with the number of pairs that had no answer, so a script running the program can tell something went wrong.

The starter's input prints:

17 / 5 = 3 remainder 2
100 / 7 = 14 remainder 2

What is checked

  • the two lines above
  • the program exits with status 0
  • the same checks on other inputs the checker keeps hidden, including negative numbers, a divisor of 0 (the exit status must count them), and no input at all
  • sdiv does the division and str writes the answers

specification

stdin17 5 100 7
stdoutprints the right output
exitexits with the right code
sourceuses sdiv
sourceuses str
hiddenright output and exit code on 4 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