Read a number from 0 to 255 in decimal and print it as 8 binary digits.
Input
One line with a whole number from 0 to 255 (1 to 3 digits, no leading zeros except for 0 itself).
Output
The number as exactly 8 binary digits, leading zeros included.
Examples
Example 1 · Input
45
Expected output
00101101
Example 2 · Input
0
Expected output
00000000
Hints
Hint 1
Read the digits until Enter, keeping value = value × 10 + digit (MUL by 10, or shifts: x × 10 = x × 8 + x × 2).
Hint 2
Then shift the byte left 8 times; after each SHL the bit that fell out is in CF.
Hint 3
MOV DL, '0' then ADC DL, 0 turns CF into the character '0' or '1'.
Solution
Show one correct solution (try it yourself first)
; Decimal to binary
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV BL, 0 ; the value
READ:
MOV AH, 1
INT 21H
CMP AL, 0DH
JE CONVERT
SUB AL, '0'
MOV BH, AL ; the new digit
MOV AL, BL
MOV CL, 10
MUL CL ; AX = value x 10
ADD AL, BH
MOV BL, AL
JMP READ
CONVERT:
MOV CX, 8
BIT:
MOV DL, '0'
SHL BL, 1 ; the top bit goes into CF
ADC DL, 0
MOV AH, 2
INT 21H
LOOP BIT
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN