Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.2 kB
1
Indexable
Never
section .data

prompt: db "Enter a binary number (period to end): "

hex_digits: db "0123456789ABCDEF"

section .bss

input: resb 16

section .text

global _start

_start:
    ; Print the prompt
    mov edi, prompt
    xor eax, eax
    call printf

    ; Read the input from the user
    mov esi, input
    mov edx, 16
    xor eax, eax
    call scanf

    ; Convert the binary number to hexadecimal
    xor eax, eax
    mov esi, input
    mov ecx, 0
    mov bl, 2

convert_loop:
    ; Shift the current value of eax left by 4 bits
    shl eax, 4

    ; Get the next digit from the input string
    mov dl, [esi + ecx]
    inc ecx

    ; Convert the digit to its hexadecimal value
    sub dl, '0'
    cmp dl, 10
    ja convert_digit
    jmp convert_next

convert_digit:
    ; The digit is a letter, so subtract 'A'-'9'-1 to get the correct value
    sub dl, 'A'-'9'-1

convert_next:
    ; Add the hexadecimal value of the digit to eax
    add eax, edx

    ; Check if we have reached the end of the input string
    cmp [esi + ecx], '.'
    jne convert_loop

    ; Print the result
    xor edx, edx
    mov edi, hex_digits
    call puthex

    ; Exit the program
    xor eax, eax
    call exit