Syscall 2: Firmware, home

DriverChapter 8: Scarcity

D29LINE EDITOR

The task

Deliver each typed line when $0D arrives, with every backspace applied.

Readers type their searches at a terminal by the door. The catalog should see each finished line, not every key, and readers make mistakes.

Collect the typed characters. $08 (backspace) removes the last character still in the line, and does nothing when the line is empty. When $0D arrives, deliver the line to the host, then $0A, and start a new line.

A line never holds more than 20 characters, and this card has 24 cells of RAM.

The board

The line card of LINE EDITORThe line card: registers A B C, 22 lines of ROM, 24 cells of RAM, a 16 byte receive buffer, a stack 8 deep. It talks to the line and to a host.LINETHE FAR ENDRXTXCARDREGISTERSA B CROM22 LINESRAM24 CELLSRX 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

A count that drops below zero wraps around to 65535, and the next character would go to a RAM cell that does not exist.

Hints

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

Hint 1Show

Store characters in RAM with a count in B, as in Length Prefix. A backspace only has to take 1 off the count.

Hint 2Show

Check for $0D and $08 before storing anything. A backspace with the count already at 0 must leave it at 0.

Hint 3Show

NEXT: IN A, JEQ A, $0D, SEND, JEQ A, $08, BACK, MOV [B], A, ADD B, 1, JMP NEXT. BACK: JEQ B, 0, NEXT, SUB B, 1, JMP NEXT. SEND: deliver cells 0 to B, then $0A.

Solution

Reveal the Full SolutionThe complete program for LINE EDITOR. 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.

START:  MOV B, 0
NEXT:   IN A
        JEQ A, $0D, SEND
        JEQ A, $08, BACK
        MOV [B], A
        ADD B, 1
        JMP NEXT
BACK:   JEQ B, 0, NEXT
        SUB B, 1
        JMP NEXT
SEND:   MOV C, 0
EMIT:   JEQ C, B, EOL
        MOV A, [C]
        HOUT A
        ADD C, 1
        JMP EMIT
EOL:    HOUT $0A
TICKS
672
at par
LINES
17
at par