SUB d, s computes d − s and keeps the result in d. When the first number is not smaller than the second, the result is never negative and the carry flag stays 0.
What to build
Read two digits, a and then b, and print a − b.
Requirements
- Turn both keys into numbers.
- Subtract the second from the first with SUB.
- Print the result as a digit.
Input
Two digits, then Enter. The first digit is never smaller than the second.
Output
One digit: the difference.
Constraints
0 ≤ b ≤ a ≤ 9.
Example
Input:
95
Output:
4
Why it works
Both characters have the same offset 30h in the ASCII table, so subtracting the characters directly would give the same difference. The program still converts them, as a habit, because the same code is needed for addition, where the offsets add up.
Examples
95
4
33
0
Hints
Hint 1
Read each digit with AH = 1 and subtract '0' to get its value.
Hint 2
SUB AL, BL puts AL − BL in AL, and sets the flags.
Hint 3
Add '0' to the result, move it to DL, and print it with AH = 2.
Solution
Show one correct solution (try it yourself first)
; Difference of two digits
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH, 1
INT 21H
SUB AL, '0'
MOV BL, AL ; a
INT 21H
SUB AL, '0' ; b
SUB BL, AL ; a - b
ADD BL, '0'
MOV DL, BL
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