Syscall 2: Firmware, home

DriverChapter 7: Frames and loops

D22READOUT

The task

Deliver each received byte to the host in decimal digits, then a space.

The weighing head under the grain silo sends each load as one byte, from 0 to 255. The display in the office shows characters, so the number has to be written out.

For every received byte, deliver its value in decimal with the characters '0' to '9' and no leading zeros, then a space ($20). Zero is the single character '0'.

The board

The line card of READOUTThe line card: registers A B C, 28 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 B CROM28 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

JLT compares signed numbers, but a byte from the line is never above 255, so the compares here are safe.

Hints

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

Hint 1Show

Count how many times you can subtract 100, then how many times 10. What is left is the ones digit, which always goes out.

Hint 2Show

Skip a hundreds digit of 0. Skip a tens digit of 0 too, but only when the hundreds digit was skipped as well: 105 still needs its 0.

Hint 3Show

HUND: JLT A, 100, TENS, SUB A, 100, ADD B, 1, JMP HUND. A digit goes out as B plus '0'. Keep the hundreds count somewhere when you clear B for the tens.

Solution

Reveal the Full SolutionThe complete program for READOUT. 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 A
        MOV B, 0
HUND:   JLT A, 100, H2
        SUB A, 100
        ADD B, 1
        JMP HUND
H2:     MOV C, B
        JEQ B, 0, TENS0
        ADD B, '0'
        HOUT B
TENS0:  MOV B, 0
TENS:   JLT A, 10, T2
        SUB A, 10
        ADD B, 1
        JMP TENS
T2:     ADD C, B
        JEQ C, 0, ONES
        ADD B, '0'
        HOUT B
ONES:   ADD A, '0'
        HOUT A
        HOUT $20
TICKS
327
at par
LINES
22
at par