5.59 · exercise

High score keeper

An arcade machine keeps its best score in a file called best.dat, so the record outlives any one game. Standard input gives a count and then that many scores. For each score, look at the file: if it does not exist yet, the score becomes the first record; if the score is higher than the saved one, it replaces it; otherwise the record stands. At the end, print the record from the file.

With the stdin box as given the program prints:

first score: 120
95 does not beat 120
new best: 180 (was 120)
180 does not beat 180
high score: 180

Talk to the file with system calls (requests the operating system carries out for your program): put the service number in x8, the arguments in x0 to x3, and run svc 0; the answer comes back in x0.

  • openat (56): x0 = AT_FDCWD (the current folder), x1 = the file name, x2 = the flags, x3 = the permission mode. The flags are bits, written in octal: 0 opens for reading, and 01101 asks for writing (01), creating the file when it is missing (0100), and emptying it when it is not (01000). A mode of 0644 lets the owner write it and everyone read it. A negative answer means the open failed; otherwise the answer is a file descriptor, the number you pass to the other calls.
  • read (63) and write (64): x0 = the descriptor, x1 = the address of the memory to read into or write from, x2 = how many bytes.
  • close (57): x0 = the descriptor.

The file holds the score as 8 raw bytes, exactly as the number sits in memory in saved, so one 8-byte read or write moves the whole thing. Scores can be larger than a 32-bit register holds.

What is checked

  • every line for the scores in the stdin box, and for more score lists you cannot see
  • the program exits cleanly
  • the file work goes through system calls (svc present)

specification

stdin4 120 95 180 180
stdoutprints the right output
exitexits with the right code
sourceuses svc
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