Syscall 2: Firmware, home

DriverChapter 10: Meshes

D37FLETCHER

The task

Transmit each host message, then its two Fletcher check bytes.

A sum check cannot see two bytes that swap places. The Fletcher check keeps a second sum that adds up the first after every byte, so the order of the bytes counts.

For each host message, transmit the message, then two check bytes. Start with S1 and S2 at 0. For each byte, add it to S1, then add S1 to S2, each time keeping the remainder after dividing by 255. Transmit S2, then S1.

The board

The line card of FLETCHERThe line card: registers A B C, 18 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 CROM18 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

A byte of 255 adds nothing to S1, because 255 leaves a remainder of 0. The check is named for John Fletcher, who designed it.

Hints

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

Hint 1Show

Both sums stay below 255, so the remainder needs no division: after each add, subtract 255 once if the sum reached 255.

Hint 2Show

Keep S1 in B and S2 in C. JLT B, 255, SUM2 skips the subtraction; the sums never pass 510, so the signed compare is safe.

Hint 3Show

NEXT: HIN A, JEQ A, -1, DONE, OUT A, ADD B, A, JLT B, 255, SUM2, SUB B, 255. SUM2: ADD C, B, JLT C, 255, NEXT, SUB C, 255, JMP NEXT. DONE: OUT C, OUT B, then clear B and C.

Solution

Reveal the Full SolutionThe complete program for FLETCHER. 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
        MOV C, 0
NEXT:   HIN A
        JEQ A, -1, DONE
        OUT A
        ADD B, A
        JLT B, 255, SUM2
        SUB B, 255
SUM2:   ADD C, B
        JLT C, 255, NEXT
        SUB C, 255
        JMP NEXT
DONE:   OUT C
        OUT B
TICKS
100
at par
LINES
14
at par