A number is even when its lowest bit is 0. The TEST instruction checks bits without changing anything, and a conditional jump uses the result.
What to build
Read a digit and print E if it is even, or O if it is odd.
Requirements
- Use TEST and a conditional jump.
- Print one letter and nothing else.
- 0 counts as even.
Input
One digit, then Enter.
Output
E or O.
Constraints
The input is one digit from 0 to 9.
Example
Input:
4
Output:
E
Why it works
TEST does an AND with the mask 1 and only sets the flags, so ZF is 1 exactly when bit 0 is clear. This works on the character code itself, because 30h has bit 0 equal to 0, so '0' to '9' keep the parity of the numbers 0 to 9.
Examples
4
E
7
O
Hints
Hint 1
Read the digit with AH = 1. The code of a digit has the same lowest bit as the digit itself, since 30h is even.
Hint 2
TEST AL, 1 sets ZF = 1 when the lowest bit is 0, that is, when the number is even.
Hint 3
JZ (or JE) jumps when ZF = 1. Print `E` on one path and `O` on the other, and do not forget the JMP that skips the second path.
Solution
Show one correct solution (try it yourself first)
; Even or odd?
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH, 1
INT 21H
TEST AL, 1 ; ZF = 1 when bit 0 is 0
JZ EVEN
MOV DL, 'O'
JMP SHOW
EVEN:
MOV DL, 'E'
SHOW:
MOV AH, 2
INT 21H
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
▶ Open this solution in the compiler, then press Visualize to watch it run step by step