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
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