A number base converter
The problem
Write a program that reads a decimal number and prints it in binary and in hexadecimal, using the shift, mask and division instructions of the 8086.
Requirements
- Read a number of up to 5 digits by multiplying by 10 and adding each digit
- Print the number in binary, 16 digits, by shifting bits into the carry flag
- Print the number in hexadecimal, 4 digits, by taking 4 bits at a time
- Use a procedure that prints one digit from 0 to 15
Technologies
- 8086 assembly
- Shifts and masks
- Procedures
- MUL and DIV
Step by step
- Read the number into AX with the multiply-by-10 method, and keep it in BX.
- Print the 16 bits with SHL BX, 1 and ADC DL, 0, in a loop of 16.
- Write a procedure that turns a value from 0 to 15 into the characters 0 to 9 and A to F and prints it.
- Print the four hex digits by rotating BX left by 4 bits each time and masking the low 4 bits.
- Test with 0, 1, 255, 4096 and 65535, and check each result with a calculator.
Expected result
A program that prints the correct binary and hexadecimal form of any number from 0 to 65535.
How it is evaluated
- The binary output has exactly 16 digits
- The hexadecimal output is correct for all test numbers
- The printing code for one digit is a separate procedure
Ways to improve it
- Leave out leading zeros
- Print the octal form as well
- Read a hexadecimal number and print it in decimal