Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.7 kB
1
Indexable
Never
section .data
    prompt_msg db 'Enter a decimal number (0-255): $'
    output_msg db 'You entered: $'
    binary_msg db 'Binary representation: $'
    newline db 0Ah, 0Dh, '$'

section .bss
    user_input resb 3  ; Buffer for user input
    number resb 1      ; Buffer for storing the entered number
    binary resb 8      ; Buffer for binary representation (8 bits)

section .text
    global _start

_start:
    ; Prompt the user to enter a decimal number
    mov eax, 4        ; System call number for sys_write
    mov ebx, 1        ; File descriptor 1 is stdout
    mov ecx, prompt_msg
    mov edx, 30        ; Length of the prompt message
    int 0x80           ; Call kernel

    ; Read user input
    mov eax, 3        ; System call number for sys_read
    mov ebx, 0        ; File descriptor 0 is stdin
    mov ecx, user_input
    mov edx, 3        ; Read up to 3 characters
    int 0x80           ; Call kernel

    ; Convert the user input to a decimal number
    mov eax, user_input
    sub eax, '0'
    mov [number], al

    ; Print the entered number
    mov eax, 4        ; System call number for sys_write
    mov ebx, 1        ; File descriptor 1 is stdout
    mov ecx, output_msg
    mov edx, 17        ; Length of the output message
    int 0x80           ; Call kernel

    mov eax, 4        ; System call number for sys_write
    mov ebx, 1        ; File descriptor 1 is stdout
    mov ecx, number
    mov edx, 1        ; Length of the number
    int 0x80           ; Call kernel

    ; Print a newline character
    mov eax, 4        ; System call number for sys_write
    mov ebx, 1        ; File descriptor 1 is stdout
    mov ecx, newline
    mov edx, 3        ; Length of the newline character
    int 0x80           ; Call kernel

    ; Convert the entered number to binary representation
    mov eax, [number]
    mov ebx, binary
    call decimal_to_binary

    ; Print the binary representation
    mov eax, 4        ; System call number for sys_write
    mov ebx, 1        ; File descriptor 1 is stdout
    mov ecx, binary_msg
    mov edx, 23        ; Length of the binary message
    int 0x80           ; Call kernel

    mov eax, 4        ; System call number for sys_write
    mov ebx, 1        ; File descriptor 1 is stdout
    mov ecx, binary
    mov edx, 8        ; Length of the binary representation
    int 0x80           ; Call kernel

    ; Exit the program
    mov eax, 1        ; System call number for sys_exit
    xor ebx, ebx      ; Exit code 0
    int 0x80           ; Call kernel

decimal_to_binary:
    ; Convert decimal to binary and store the result in the buffer
    mov ecx, 8
    movzx ebx, ax
convert_loop:
    shr ebx, 1
    adc [ebx+ecx-1], '0'
    loop convert_loop
    ret
Leave a Comment