Syscall 2: Firmware, home

DriverChapter 4: Stars and hubs

D7LENGTH PREFIX

The task

Transmit each host message as a length byte, then the message.

The logger at the far end reads a length byte first, then exactly that many bytes, so it never has to guess where a reading ends.

For each host message, transmit one byte holding the number of bytes in the message, then the message itself. An empty message is the single byte $00. Messages are at most 12 bytes long.

The host hands over the message one byte at a time, so the length is not known until the message has ended. This card has 16 cells of RAM to hold the message until then.

The board

The line card of LENGTH PREFIXThe line card: registers A B C, 18 lines of ROM, 16 cells of RAM, a 16 byte receive buffer, a stack 8 deep. It talks to the line and to a host.LINENO PEERRXTXCARDREGISTERSA B CROM18 LINESRAM16 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

MOV [B], A stores A in the RAM cell numbered by B, and MOV A, [C] reads one back. Only MOV can reach RAM.

Hints

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

Hint 1Show

Store each byte of the message in RAM as it arrives, counting as you go. When HIN gives -1, the count is the length.

Hint 2Show

Keep the count in B and use it as the address: MOV [B], A, then ADD B, 1. Transmit B, then walk a second register from 0 up to B, transmitting each stored byte.

Hint 3Show

START: MOV B, 0. READ: HIN A, JEQ A, -1, SEND, MOV [B], A, ADD B, 1, JMP READ. SEND: OUT B, MOV C, 0. COPY: JEQ C, B, START, MOV A, [C], OUT A, ADD C, 1, JMP COPY.

Solution

Reveal the Full SolutionThe complete program for LENGTH PREFIX. 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
READ:   HIN A
        JEQ A, -1, SEND
        MOV [B], A
        ADD B, 1
        JMP READ
SEND:   OUT B
        MOV C, 0
COPY:   JEQ C, B, START
        MOV A, [C]
        OUT A
        ADD C, 1
        JMP COPY
TICKS
261
at par
LINES
13
at par