Syscall 2: Firmware, home

DriverChapter 9: Special nodes

D33EXPAND

The task

Deliver the bytes each header and the bytes after it stand for.

The print shop's scanner squeezes each page into a stream of headers. A header H from 0 to 127 means: copy the next H plus 1 bytes as they are.

A header H from 129 to 255 means: repeat the next byte 257 minus H times. A header of 128 means nothing; skip it and read the next header.

Deliver the expanded bytes to the host, in order.

The board

The line card of EXPANDThe line card: registers A B, 18 lines of ROM, no RAM, a 16 byte receive buffer, a stack 8 deep. It talks to the line and to a host.LINETHE FAR ENDRXTXCARDREGISTERSA BROM18 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

No count can be zero here: literals come in groups of 1 to 128 and runs repeat 2 to 128 times, so DJN never wraps around.

Hints

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

Hint 1Show

Read a header into B. Below 128 it counts literal bytes, after adding 1. Above 128 it counts repeats, as 257 minus B.

Hint 2Show

For literals, receive and deliver one byte at a time in a DJN loop. For a run, receive the byte once and deliver it in a DJN loop.

Hint 3Show

TOP: IN B, JEQ B, 128, TOP, JGT B, 128, RUN, ADD B, 1. LIT: IN A, HOUT A, DJN B, LIT, JMP TOP. RUN: MOV A, 257, SUB A, B, MOV B, A, IN A. REP: HOUT A, DJN B, REP.

Solution

Reveal the Full SolutionThe complete program for EXPAND. 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:    IN B
        JEQ B, 128, TOP
        JGT B, 128, RUN
        ADD B, 1
LIT:    IN A
        HOUT A
        DJN B, LIT
        JMP TOP
RUN:    MOV A, 257
        SUB A, B
        MOV B, A
        IN A
REP:    HOUT A
        DJN B, REP
TICKS
426
at par
LINES
14
at par