0

Here is my code

    segment .data
    ; initialized data is put in the data segment here
    ;

    segment .text
    global  main
main:
    enter   0,0     ; setup stack frame
    pusha

    call    func
    ;

    ; 

    ; 
    ;

    popa
    mov eax, 0  ; 
    leave           ; 
    ret

func:
    push ebp
    mov ebp,esp
    sub esp,4  ;this is for local variable

    mov eax,dword [ebp+12]
    call    print_int
    call    print_nl

    mov eax,dword [ebp+8]
    call    print_int
    call    print_nl

    mov esp,ebp
    pop ebp
    ret

If I type command $./test 1 2 3 then, in stack

---------------
pointer to argv-------->ebp+12
---------------
argc           -------->ebp+8
---------------
return address -------->ebp+4
---------------
saved ebp      -------->ebp
---------------

it should print out 3(the number of argument) but there's only 0 in console.

what is wrong?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    Your stack layout applies to `main` not for your `func` for obvious reasons. The `enter`, the `pusha` and the `call` all change the stack and you are not passing any arguments to `func`. – Jester Dec 01 '18 at 16:42
  • 1
    please format question – Eugene Mihaylin Dec 01 '18 at 17:34
  • `pusha` puts all the integer regs on the stack, where `func` will see them as its function args. Your stack-frame diagram looks right for `main`, but not for `func`. – Peter Cordes Dec 01 '18 at 20:45

0 Answers0