0

I want to print argv[1] (in C terminallogy) from another routine and not from start routine(that's entry point). But it given a seg fault:

format ELF executable 3
entry start
segment readable executable

start:
    pop ebx ;argc
    pop ebp ;argv[0]
    call printarg

    ;; exit
    xor ebx,ebx
    mov eax,1
    int 80h

printarg:
    pop ebp ;argv[1]
    call puts
    ret
puts:
    pusha
    mov eax,ebp
    xor edx,edx
    ;; get string length
.loop1:
    cmp byte [eax],0
    je .loop2
    inc eax
    inc edx
    jmp .loop1
    ;; print it
.loop2:
    mov eax,4
    mov ebx,1
    mov ecx,ebp
    int 80h
    ;print a new line
    mov eax,4
    mov ebx,1
    mov ecx,NL
    mov edx,1
    int 80h 
    popa
    ret

segment readable writeable
NL db 0xA

can someone explain it?

Jack
  • 16,276
  • 55
  • 159
  • 284

1 Answers1

1

You can't do

call something

and then

something:
  pop ebp

and expect it to work - in the routine, what you are popping, then, is the return address that the call just pushed.

  • Thanks. So,is possible to access the arguments of program in `something` routine? – Jack Feb 14 '13 at 01:31
  • Sure - since they're going to be just over the return address, which is at `[esp]`, once in the routine `mov ebp, [esp+4]` in place of your `pop ebp` should give you `argv[1]` in `ebp`. – 500 - Internal Server Error Feb 14 '13 at 01:34
  • Really. I just forget it. I'm a bit down today. LOL no idea why. Anyway,thank you again. – Jack Feb 14 '13 at 01:44