Number Parsing in Assembly Language

; Number Parsing in Assembly

section .data
    float_str db "1.234", 0
    int_str db "123", 0
    hex_str db "0x1c8", 0
    uint_str db "789", 0
    atoi_str db "135", 0
    invalid_str db "wat", 0
    fmt_float db "%.3f", 10, 0
    fmt_int db "%d", 10, 0
    fmt_uint db "%u", 10, 0
    fmt_error db "Error parsing: %s", 10, 0

section .text
    global main
    extern printf
    extern atof
    extern atoi
    extern strtol
    extern strtoul

main:
    ; Parse float
    push float_str
    call atof
    add esp, 4
    sub esp, 8
    fstp qword [esp]
    push fmt_float
    call printf
    add esp, 12

    ; Parse int
    push int_str
    call atoi
    add esp, 4
    push eax
    push fmt_int
    call printf
    add esp, 8

    ; Parse hex
    push 0      ; base (0 for auto-detect)
    push 0      ; endptr (not used)
    push hex_str
    call strtol
    add esp, 12
    push eax
    push fmt_int
    call printf
    add esp, 8

    ; Parse uint
    push 0      ; base (0 for auto-detect)
    push 0      ; endptr (not used)
    push uint_str
    call strtoul
    add esp, 12
    push eax
    push fmt_uint
    call printf
    add esp, 8

    ; Parse int with atoi
    push atoi_str
    call atoi
    add esp, 4
    push eax
    push fmt_int
    call printf
    add esp, 8

    ; Attempt to parse invalid input
    push invalid_str
    call atoi
    add esp, 4
    cmp eax, 0
    jne .end
    push invalid_str
    push fmt_error
    call printf
    add esp, 8

.end:
    ret

This Assembly code demonstrates number parsing, similar to the original example. Here’s an explanation of the code:

  1. We define our string constants and format strings in the .data section.

  2. In the .text section, we implement the main function and declare external functions we’ll use for parsing.

  3. To parse a float, we use the atof function, which converts a string to a double-precision floating-point number.

  4. For parsing integers, we use atoi, which converts a string to an integer.

  5. To parse hexadecimal numbers, we use strtol, which can handle different bases. We pass 0 as the base to auto-detect the format.

  6. For parsing unsigned integers, we use strtoul.

  7. We demonstrate error handling by attempting to parse an invalid string “wat”. If atoi returns 0, we assume an error occurred and print an error message.

  8. After each parsing operation, we use printf to display the result.

To run this program, you would need to assemble it with NASM and link it with a C library that provides the parsing functions. The exact commands may vary depending on your system, but it might look something like this:

$ nasm -f elf64 number_parsing.asm
$ gcc -no-pie number_parsing.o -o number_parsing
$ ./number_parsing
1.234
123
456
789
135
Error parsing: wat

Note that Assembly Language doesn’t have built-in parsing functions like higher-level languages do. We’re relying on C library functions for the actual parsing. In a real Assembly program, you might implement your own parsing functions or use more low-level system calls.