ASM From AI

Paste ID: 4f4afd83

Created at: 2024-10-10 03:42:07

code assembler assembly converter
; Program: Hexadecimal to Binary Conversion with Error Handling
; Language: x86 Assembly
; Assembler: NASM
; Platform: x86 32-bit

section .data
    prompt db "Enter a hexadecimal number (e.g., 1A3): ", 0
    hex_input db 8, 0  ; Buffer for user input (up to 8 characters)
    result db "Binary representation: ", 0
    binary_output db 32, 0 ; Buffer for binary output (up to 32 characters)
    error_msg db "Error: Invalid hexadecimal input!", 0
    newline db 0xA ; ASCII newline character for formatting

section .bss

section .text
    global _start

_start:
    ; Print the prompt message
    mov eax, 4             ; syscall: sys_write
    mov ebx, 1             ; file descriptor: stdout
    mov ecx, prompt        ; message to write
    mov edx, 34            ; message length
    int 0x80               ; call kernel

    ; Read the hexadecimal input from the user
    mov eax, 3             ; syscall: sys_read
    mov ebx, 0             ; file descriptor: stdin
    mov ecx, hex_input     ; buffer to store input
    mov edx, 8             ; maximum input length
    int 0x80               ; call kernel

    ; Convert the hexadecimal string to an integer
    mov esi, hex_input     ; source pointer to hex_input
    xor ebx, ebx           ; clear ebx (will store the integer result)
convert_loop:
    mov al, [esi]          ; load next character
    cmp al, 0xA            ; check if it's a newline character
    je done_conversion     ; if newline, we're done
    cmp al, 0              ; check if null terminator (end of string)
    je done_conversion     ; if null, we're done

    ; Convert ASCII character to integer value
    cmp al, '0'
    jl invalid_input       ; if less than '0', invalid input
    cmp al, '9'
    jg check_alpha         ; if greater than '9', check if it’s a letter
    sub al, '0'            ; convert '0' to '9' to 0-9
    jmp store_digit

check_alpha:
    cmp al, 'A'
    jl invalid_input       ; if less than 'A', invalid input
    cmp al, 'F'
    jg invalid_input       ; if greater than 'F', invalid input
    sub al, 'A'            ; convert 'A' to 'F' to 0-5
    add al, 10             ; adjust to 10-15

store_digit:
    shl ebx, 4             ; shift previous result 4 bits to the left
    or ebx, eax            ; add the new digit to the result
    inc esi                ; move to the next character
    jmp convert_loop       ; repeat for all characters

done_conversion:
    ; Store the result in binary_output as a binary string
    mov ecx, binary_output ; pointer to binary_output
    mov eax, ebx           ; move the integer value to eax
    mov edi, 31            ; index to binary_output (start from end)
print_binary:
    test eax, eax          ; check if eax is zero
    jz print_done          ; if zero, we're done printing
    shl eax, 1             ; shift left (get the MSB)
    jc set_one             ; if carry is set, bit was 1
    mov byte [ecx+edi], '0'
    jmp next_digit
set_one:
    mov byte [ecx+edi], '1'
next_digit:
    dec edi                ; move to the next binary position
    jmp print_binary

print_done:
    ; Print the "Binary representation: " message
    mov eax, 4             ; syscall: sys_write
    mov ebx, 1             ; file descriptor: stdout
    mov ecx, result        ; message to write
    mov edx, 23            ; message length
    int 0x80               ; call kernel

    ; Print the binary output
    mov eax, 4             ; syscall: sys_write
    mov ebx, 1             ; file descriptor: stdout
    mov ecx, binary_output ; binary output to print
    mov edx, 32            ; binary output length
    int 0x80               ; call kernel

    ; Exit the program
    mov eax, 1             ; syscall: sys_exit
    xor ebx, ebx           ; exit code 0
    int 0x80               ; call kernel

invalid_input:
    ; Print error message for invalid input
    mov eax, 4             ; syscall: sys_write
    mov ebx, 1             ; file descriptor: stdout
    mov ecx, error_msg     ; error message to write
    mov edx, 32            ; error message length
    int 0x80               ; call kernel

    ; Print a newline for formatting
    mov eax, 4             ; syscall: sys_write
    mov ebx, 1             ; file descriptor: stdout
    mov ecx, newline       ; newline character
    mov edx, 1             ; length of newline character
    int 0x80               ; call kernel

    ; Exit the program with error code
    mov eax, 1             ; syscall: sys_exit
    mov ebx, 1             ; exit code 1 (indicating an error)
    int 0x80               ; call kernel

Share this Paste