Printing `argv[]` with nasm -


i'm trying print command line arguments given program, using nasm:

global main extern printf  section .rodata fmt db "argument: %s", 10, 0  section .text main:     push    ebp                 ; push ebp0     mov     ebp, esp        ; [ebp1] == ebp0      push    dword[ebp+8]        ; push argc     call    print_args      mov     eax, 0      ; return(0)     mov     esp, ebp    ; pop     pop     ebp             ; stack frame     ret  print_args:     push    ebp             ; pusheo ebp1     mov     ebp, esp    ; [ebp2] == ebp1     mov     edi, dword[ebp+8]   ; [ebp+8] == argc     jmp     lop postlop:     mov     esp, ebp     pop     ebp     ret   lop:     sub     edi, 1     cmp     edi, 0     jz      postlop     mov     esi, [ebp] ; [esi] == ebp1     mov     ebx, [esi + 12] ; [esi+12] = [ebp1+12] = argv[0]?     push    ebx     push    fmt     call    printf     jmp     lop 

however, prints garbage (i believe should print argv[0], argc-1 times.).

i'm compiling code with:

nasm -f elf32 main.asm gcc -m32 main.o -o main.out 

what wrong?

by way, using dword[ebp+8] works correctly pick up argc.

i'm running on ubuntu. program output argument: ... argc-1 times, ... garbage.

just [epb+8]is argc, [esi + 12] argv, i.e. address of the array of argument adresses. thus, in order find argv[0], have dereference once more.

mov     esi, [ebp]      ; [esi] == ebp1 mov     ebx, [esi + 12] ; [esi+12] = [ebp1+12] = argv push    dword [ebx]     ; [ebx] = argv[0]        ;^^^^^^^^^^^ push    fmt call    printf 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -