Search code examples
assemblysegmentation-faultfasm

Why does this given a seg fault?


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?


Solution

  • 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.