8088 Assembly, IBM PC DOS, 25 bytes
Unassembled:
BA 0110 MOV DX, OFFSET GB ; point DX to 'Good','Bad' string
D1 EE SHR SI, 1 ; point SI to DOS PSP (80H)
02 04 ADD AL, [SI] ; add input string length to AL, set parity flag
7B 02 JNP DISP ; if odd parity, input was 'Bad' so jump to display 'Good'
02 D0 ADD DL, AL ; otherwise add string length as offset for 'Bad' string
DISP:
B4 09 MOV AH, 9 ; DOS display string function
CD 21 INT 21H ; call DOS API, write string to console
C3 RET ; return to DOS
GB DB 'Good$','Bad$'
Explanation:
Looks at the length of input string (plus leading space) that DOS stores at memory address 80H
, and adds it to AL
(initially 0
by DOS). If there is an odd number of 1
bits in the binary representation of the string length, the CPU parity flag is set to odd, and vice-versa. So input string ' Bad'
length 4
(0000 0100
), is odd parity and input string ' Good'
is 5
(0000 0101
) is even parity.
DX
is initially set to point to the string 'Good$Bad$'
, and if parity is even (meaning input was ' Good'
) advance the string pointer by that length (5
) so it now points to 'Bad$'
. If parity is odd, do nothing since it already points to 'Good$'
. Then use DOS API to display a $
terminated string to console.
Example:
Download and test GOODBAD.COM or build from xxd
dump:
0000000: ba10 01d1 ee02 047b 0202 d0b4 09cd 21c3 .......{......!.
0000010: 476f 6f64 2442 6164 24 Good$Bad$