Syscall 2: Firmware, home

DriverChapter 9: Special nodes

D34RUN LENGTH

The task

Transmit each host message as pairs of a run length and a byte.

Weather charts are mostly blank paper, so long runs of the same byte are common. Sending each run once, with its length, saves most of the air time.

For each host message, transmit every run of equal bytes as two bytes: the length of the run, then the byte. After the message, transmit $00 to mark its end. Messages are at most 64 bytes long.

The board

The line card of RUN LENGTHThe line card: registers A B C, 20 lines of ROM, no RAM, a 16 byte receive buffer, a stack 8 deep. It talks to the line and to a host.LINENO PEERRXTXCARDREGISTERSA B CROM20 LINESRAMNONERX BUFFER16 BYTESSTACK8 DEEPUNITSNONEHOSTMESSAGES
The line card this ticket gives you, with its registers, memory, buffers, and units. On a narrow screen, scroll the board sideways to see all of it.

Key idea

Nothing goes out for a run until a different byte, or the end of the message, shows that the run is over.

Hints

Each hint goes a step further than the one before. Open them one at a time.

Hint 1Show

Keep the byte of the current run in C and its length in B. Compare each new byte with C.

Hint 2Show

A different byte ends the run: transmit B and C, then start a new run with the new byte. The end of the message ends the last run too.

Hint 3Show

Read the first byte on its own: it starts the first run. If it is -1, the message is empty and only $00 goes out.

Solution

Reveal the Full SolutionThe complete program for RUN LENGTH. Open it only when you want the answer.

The reference program

The game's own solution to this ticket. It passes every test and scores exactly par on every benchmark.

TOP:    HIN C
        JEQ C, -1, END
        MOV B, 1
NEXT:   HIN A
        JEQ A, -1, LAST
        JNE A, C, FLUSH
        ADD B, 1
        JMP NEXT
FLUSH:  OUT B
        OUT C
        MOV C, A
        MOV B, 1
        JMP NEXT
LAST:   OUT B
        OUT C
END:    OUT 0
TICKS
502
at par
LINES
16
at par