Syscall 2: Firmware, home

DriverChapter 7: Frames and loops

D21EVEN PARITY

The task

Transmit each host byte with bit 7 set to make its count of ones even.

The exchange's teleprinters use seven bits for each character and spend the eighth on parity, so a single flipped bit turns into a character that cannot be right.

Every host byte is a character from $00 to $7F. Transmit each one with bit 7 ($80) set when its low seven bits hold an odd number of ones, so every byte on the line has an even number. The end of a message sends nothing.

The board

The line card of EVEN PARITYThe line card: registers A B C D, 16 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 C DROM16 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

SHL B, 7 moves a 1 in bit 0 up to bit 7, which is $80, and OR sets it in the character.

Hints

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

Hint 1Show

Count the ones: look at the lowest bit, shift the byte right by 1, and look again, seven times. Keep a copy of the character to transmit.

Hint 2Show

Only the lowest bit of the count matters. XOR B, A folds the lowest bit of A into the lowest bit of B; the other bits of B can be cleared afterward with AND B, 1.

Hint 3Show

MOV C, A, MOV B, 0, MOV D, 7. BIT: XOR B, A, SHR A, 1, DJN D, BIT. Then AND B, 1, SHL B, 7, OR C, B, OUT C.

Solution

Reveal the Full SolutionThe complete program for EVEN PARITY. 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:    HIN A
        JEQ A, -1, TOP
        MOV C, A
        MOV B, 0
        MOV D, 7
BIT:    XOR B, A
        SHR A, 1
        DJN D, BIT
        AND B, 1
        SHL B, 7
        OR C, B
        OUT C
TICKS
407
at par
LINES
12
at par