5.58 · exercise

Count the lines

Count the lines and the bytes on standard input without scanf or getchar: ask the operating system for the bytes yourself with the read system call. With the stdin box as given the program prints lines: 3, bytes: 34.

read is service 63. Put the file descriptor, the number that names an open file, in x0 (0 is standard input), the buffer address in x1, the most bytes you want in x2, and 63 in x8, then run svc 0. The answer in x0 is how many bytes actually arrived, which can be fewer than you asked for. An answer of 0 means the input is finished.

So read into the 64-byte buf in a loop: add each answer to the byte count, and count the newline bytes (value 10) among the bytes that read just delivered. Only those: whatever sits past them in buf is left over from an earlier read. A line counts when its newline arrives, so text whose last line has no newline has one line fewer than it looks.

The stdin box stands in for running the program with < file on a server: once its text runs out, read answers 0. If a run ever sits waiting for more input instead, Ctrl-D in the console's input line ends it.

What is checked

  • the count line for the text in the stdin box, and for more inputs you cannot see (short, empty, and longer than the buffer)
  • the program exits cleanly
  • the input comes through system calls (svc present, no getchar)

specification

stdinfirst line second line third line
stdoutprints the right output
exitexits with the right code
sourceuses svc
sourcedoes not use scanf or getchar
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