A one-digit calculator
The problem
Write a program that reads two digits and an operator (+, -, * or /), and prints the result, so that it uses input, comparisons and jumps, and the arithmetic instructions.
Requirements
- Read the first digit, the operator and the second digit with INT 21h service 1
- Choose the operation with CMP and conditional jumps
- Print a result of one or two digits (for example 7 times 8 is 56) without a leading zero
- Print the quotient and the remainder for a division, and an error message for a division by 0
Technologies
- 8086 assembly
- INT 21h
- MUL, DIV and AAM
- Conditional jumps
Step by step
- Write the frame and read three keys, keeping them in three registers.
- Subtract '0' from the digits, and compare the operator with each of the four characters.
- Write one block of code for each operation, ending with a jump to a shared printing block.
- Write the printing block with AAM, skipping a tens digit of 0.
- Test with at least ten inputs, including 0, 9, subtraction below 0 and a division by 0.
Expected result
A program that prints the right answer for every pair of digits and operator, and a clear message for division by zero.
How it is evaluated
- All four operations give the correct answer
- Results with two digits are printed without a leading zero
- A division by zero is caught before DIV runs
Ways to improve it
- Accept numbers with several digits
- Print negative results with a minus sign
- Keep asking for a new calculation until the user types q