5.53 · exercise

Return to sender

A struct bigger than 16 bytes does not fit in x0 and x1, so it comes back another way. The caller, the code making the call, reserves room for the struct in its own frame and passes that address in x8. The callee, the subroutine being called, writes the struct there. The caller picks the address and the callee delivers to it.

Write describe(n), which fills in this struct for the n in x0:

struct profile {
    long square;   // n * n
    int  sign;     // -1, 0 or 1
    int  odd;      // 1 if n is odd, else 0
    int  last;     // the last decimal digit of n, never negative
};

Work out each field's offset first. A long starts at a multiple of 8, and the whole struct is padded out to a multiple of 8 as well, so its 20 bytes of fields take 24. Store each field at its own width: an x register for the long, a w register for each int.

Then finish main: grow its frame to hold the struct, point x8 at it, call describe, and load the fields into x2, w3, w4 and w5 for the printf that is already there. The starter's input prints:

7: square 49, sign 1, odd 1, last digit 7
-12: square 144, sign -1, odd 0, last digit 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 0, negative numbers, a square too big for 32 bits, and no input at all
  • the struct travels through memory: the source uses x8 and str

specification

stdin7 -12
stdoutprints the right output
exitexits with the right code
sourceuses x8
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