Skip to content
Advertisement

Converting User Input Hexadecimal to Decimal in Assembly

I am trying to create an assembly program that takes a user input hexadecimal number no greater than 4 digits and outputs the same number in base 10. This is being done using NASM on a Linux install. Using some tutorials I’ve found and my very limited understanding of this language, I have come up with this.

section .data
    inp_buf: times 6 db 0
    numPrompt: db "Enter a hexadecimal value no greater than 4 digits: "
    len1 equ $ - numPrompt
    answerText: db "The decimal value is: "
    len2 equ $ - answerText

section .bss
    dec:   resb    5

section .text
    global _start
_start:
    mov edx, len1       
    mov ecx, numPrompt  
    mov ebx, 1      
    mov eax, 4      
    int 0x80

    mov eax, 3      
    mov ebx, 0      
    mov ecx, inp_buf    
    mov edx, 6      
    int 0x80

    mov esi, dec+11     
    mov byte[esi], 0xa              
    mov eax, ecx         ;I feel like the problem must be here
    mov ebx, 0xa            
    mov ecx, 1              

next:
    inc ecx                 
    xor edx, edx            
    div ebx                 
    add edx, 0x30           
    dec esi                 
    mov [esi], dl           
    cmp eax, 0              
    jnz next

    mov edx, len2           
    mov ecx, answerText     
    mov ebx, 1              
    mov eax, 4              
    int 0x80 

    mov edx, ecx            
    mov ecx, esi            
    mov ebx, 1              
    mov eax, 4              
    int 0x80            

    mov ebx, 0              
    mov eax, 1              
    int 0x80

It should be noted that if the user input is ignored and you just put in a variable with the hex in the data section, the program can convert that with no problem. For example, if you have the line hexNum: equ 0xFFFF and you replace the commented line above with mov eax, hexNum then the code is capable of converting that to base 10 correctly. The error must be with the format of the user input, but I don’t know how to verify that.

I appreciate any insight on what my issue is here. This is my first assembly program other than “hello world” and a lot of these new concepts are still confusing and hard to understand for me. If there is a better or simpler way for me to go about this entire program then I would love to hear about that as well. Thanks!

Advertisement

Answer

The values in `inp_buffer are going to be in hex format, for eg.

1234

will be stored as

0x31 0x32 0x33 0x34

in 4 consecutive bytes.

The number must be converted from ascii format into hex format using the exact opposite procedure for reverse conversion.

After getting the number into hex form, the procedure for conversion to decimal is correct, what may be called as decimal dabble.

I recommend complete conversion first followed by byte by byte conversion into ascii. It is always better to have the final result and then go for conversions, especially in assembly language programming where debugging is difficult.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement