Search code examples
assemblyx86asciinasmdisassembly

what does so many hexadecimal values mean to print a string in the assembly?


I'm new to the assembly world and I study converting code in C to better understand the logic. I use objconv to do the conversion and I use Windows. I converted the following C code:

#include <stdio.h>

int main()
{
    int vector[5] = {2, 6, 8, 9, 4};
    int aux = 0;

    for(int i = 0; i < 5; i++)
    {
        aux = aux + vector[i];
    }

    printf("The summation of array is: %d", aux);
}

The generated assembly code was:

global _main: 

extern _printf                                          
extern ___main                                          


SECTION .text                            

.text:  ; Local function

_main:
        push    ebp                                     
        mov     ebp, esp                                
        and     esp, 0FFFFFFF0H                        
        sub     esp, 48                                 
        call    ___main                                
        mov     dword [esp+14H], 2                      
        mov     dword [esp+18H], 6                      
        mov     dword [esp+1CH], 8                      
        mov     dword [esp+20H], 9                      
        mov     dword [esp+24H], 4                      
        mov     dword [esp+2CH], 0                     
        mov     dword [esp+28H], 0                     
        jmp     ?_002                                  

?_001:  mov     eax, dword [esp+28H]                   
        mov     eax, dword [esp+eax*4+14H]             
        add     dword [esp+2CH], eax                   
        add     dword [esp+28H], 1                     
?_002:  cmp     dword [esp+28H], 4                     
        jle     ?_001                                  
        mov     eax, dword [esp+2CH]                   
        mov     dword [esp+4H], eax                    
        mov     dword [esp], ?_003                     
        call    _printf                                
        mov     eax, 0                                  
        leave                                          
        ret                                           

        nop                                            


SECTION .data                        


SECTION .bss                        


SECTION .rdata                    

?_003:                                                  ; byte
        db 54H, 68H, 65H, 20H, 73H, 75H, 6DH, 6DH       ; 0000 _ The summ
        db 61H, 74H, 69H, 6FH, 6EH, 20H, 6FH, 66H       ; 0008 _ ation of
        db 20H, 61H, 72H, 72H, 61H, 79H, 20H, 69H       ; 0010 _  array i
        db 73H, 3AH, 20H, 25H, 64H, 00H, 00H, 00H       ; 0018 _ s: %d...

As you can see, C's printf is representing the assembly at label ?_003. What exactly would all this data be? Is it possible to simplify this output?


Solution

  • The hexadecimal block at the label ?_003 is just the representation of the C string

    "The summation of array is: %d"

    You can see an ASCII translation on the right side after the comment char ;: the number is the relative address, the underscore a delimiter, and the chars are part of the string.

    db 54H, 68H, 65H, 20H, 73H, 75H, 6DH, 6DH       ; 0000 _ The summ
    db 61H, 74H, 69H, 6FH, 6EH, 20H, 6FH, 66H       ; 0008 _ ation of
    db 20H, 61H, 72H, 72H, 61H, 79H, 20H, 69H       ; 0010 _  array i
    db 73H, 3AH, 20H, 25H, 64H, 00H, 00H, 00H       ; 0018 _ s: %d...
    

    An equivalent in (NASM) assembly that isn't created automatically (like by objconv) would be

    outStr: db "The summation of array is: %d",0