Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.6 kB
1
Indexable
Never
ORG 100h ; Emu8086's default origin

MOV AH, 09h ; DOS function to print a string
MOV DX, OFFSET prompt_msg ; Address of the prompt message
INT 21h ; Call DOS

MOV AH, 0 ; Get a single key from the user
INT 16h ; Call BIOS

MOV AH, 02h ; BIOS function to print character
MOV DL, AL ; Print the entered character
INT 21h ; Call DOS

CALL input_and_print_binary ; Call the subroutine

MOV AH, 4Ch ; DOS function to exit
INT 21h ; Call DOS

input_and_print_binary PROC
    CALL get_one_decimal_digit
    MOV digit[2], AL ; Store the first digit

    CALL get_one_decimal_digit
    MOV digit[1], AL ; Store the second digit

    CALL get_one_decimal_digit
    MOV digit[0], AL ; Store the third digit

    CALL output_value_to_display ; Print the entered decimal number

    MOV CX, 3
    MOV SI, 0
PRINT_BINARY_LOOP:
    CALL output_binary_digit ; Print the binary representation
    INC SI
    LOOP PRINT_BINARY_LOOP

    RET
input_and_print_binary ENDP

output_value_to_display PROC
    MOV CX, 0

    MOV SI, 0
    MOV AL, digit[SI]
    MOV BL, 100
    MUL BL
    ADD CL, AL

    INC SI

    MOV AL, digit[SI]
    MOV BL, 10
    MUL BL
    ADD CL, AL

    MOV AL, digit[SI]
    ADD CL, AL

    MOV AH, 02h ; BIOS function to print character
    MOV DL, CL ; Print the character
    INT 21h ; Call DOS

    RET
output_value_to_display ENDP

output_binary_digit PROC
    MOV AL, digit[SI]
    CALL binary_conversion
    ADD AL, '0'
    MOV AH, 02h ; BIOS function to print character
    INT 21h ; Call DOS

    RET
output_binary_digit ENDP

binary_conversion PROC
    MOV CX, 8 ; 8 bits

BINARY_CONVERSION_LOOP:
    SHL AL, 1 ; Shift left
    JNC ZERO_BIT ; Jump to ZERO_BIT if no carry

    MOV DL, '1' ; Print '1'
    MOV AH, 02h ; BIOS function to print character
    INT 21h ; Call DOS
    JMP NEXT_BIT ; Jump to NEXT_BIT

ZERO_BIT:
    MOV DL, '0' ; Print '0'
    MOV AH, 02h ; BIOS function to print character
    INT 21h ; Call DOS

NEXT_BIT:
    LOOP BINARY_CONVERSION_LOOP ; Loop until CX becomes zero

    RET
binary_conversion ENDP

get_one_decimal_digit PROC
    MOV AH, 01h ; BIOS function to read character
    INT 21h ; Call DOS

    CMP AL, '0' ; Compare with '0'
    JB AGAIN_INPUT ; Jump to AGAIN_INPUT if less than '0'

    CMP AL, '9' ; Compare with '9'
    JA AGAIN_INPUT ; Jump to AGAIN_INPUT if greater than '9'

    SUB AL, '0' ; Convert character to integer
    MOV digit[0], AL ; Store the digit in the array

    RET

AGAIN_INPUT:
    JMP get_one_decimal_digit ; Repeat the input process
get_one_decimal_digit ENDP

prompt_msg DB "Enter a decimal number: $"
digit DB 3 DUP(0)

TIMES 510 - ($ - $$) DB 0 ; Fill the rest of t
Leave a Comment