Why can I restore old ESP value

Asked

Viewed 53 times

0

Well I have here a question that involves Assembly and C.

The variable format has more than 4 bytes, yet I can do pushfrom it without using cast and return the old ESP value after push. How is it possible?

ASM

segment .data
 format db "oleeee %d",10,0;tem muito mais que 4 bytes

segment .text
    global  _my_func
    extern _printf
    extern _soma


 _my_func:
    push ebp
    mov ebp, esp ;inicio 

    push dword [numero]
    push dword format
    call _printf
    add esp, 8 ;remover

    push dword 5
    push dword 200
    call _soma
    add esp,8 ;remover

    push eax
    push dword format
    call _printf


    mov esp,ebp
    pop ebp ;fim
    ret

C

int soma(int a, int b){

return a+b;
 }

 int main()
 {
   int ret_status;
   ret_status = my_func();
   return ret_status;
 }
  • When you push in format you push the memory address not the value, and ESP is saved in EBP, one thing that is missing from your code, is the return of my_func(), in case it is returned any trash, to return a value, you have q use the EAX.

  • Ah, that bad ;p I know I have to value the EAX, in this case it was not necessary because I do not do anything with your return.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.