函式呼叫棧

轩邈、發表於2024-07-14

1、程式原始碼:

[xuanmiao@localhost Practice]$ cat main.c
#include <stdio.h>

int plus(int a, int b)
{
    int c = a+b;
    return c;
}

int main()
{
    int a = 1, b = 2;
    int c = 0;
    c = plus(a,b);
    return 0;
} 

2、編譯

[xuanmiao@localhost Practice]$ gcc main.c -o main -g
[xuanmiao@localhost Practice]$ ls
main  main.c

3、生成彙編程式碼(此處只取main和plus函式部分)

彙編程式碼(只取main和plus函式)
00000000004004ed <plus>:
#include <stdio.h>

int plus(int a, int b)
{
  4004ed:	55                   	push   %rbp
  4004ee:	48 89 e5             	mov    %rsp,%rbp
  4004f1:	89 7d ec             	mov    %edi,-0x14(%rbp)
  4004f4:	89 75 e8             	mov    %esi,-0x18(%rbp)
    int c = a+b;
  4004f7:	8b 45 e8             	mov    -0x18(%rbp),%eax
  4004fa:	8b 55 ec             	mov    -0x14(%rbp),%edx
  4004fd:	01 d0                	add    %edx,%eax
  4004ff:	89 45 fc             	mov    %eax,-0x4(%rbp)
    return c;
  400502:	8b 45 fc             	mov    -0x4(%rbp),%eax
}
  400505:	5d                   	pop    %rbp
  400506:	c3                   	retq   

0000000000400507 <main>:

int main()
{
  400507:	55                   	push   %rbp
  400508:	48 89 e5             	mov    %rsp,%rbp
  40050b:	48 83 ec 10          	sub    $0x10,%rsp
    int a = 1, b = 2;
  40050f:	c7 45 fc 01 00 00 00 	movl   $0x1,-0x4(%rbp)
  400516:	c7 45 f8 02 00 00 00 	movl   $0x2,-0x8(%rbp)
    int c = 0;
  40051d:	c7 45 f4 00 00 00 00 	movl   $0x0,-0xc(%rbp)
    c = plus(a,b);
  400524:	8b 55 f8             	mov    -0x8(%rbp),%edx
  400527:	8b 45 fc             	mov    -0x4(%rbp),%eax
  40052a:	89 d6                	mov    %edx,%esi
  40052c:	89 c7                	mov    %eax,%edi
  40052e:	e8 ba ff ff ff       	callq  4004ed <plus>
  400533:	89 45 f4             	mov    %eax,-0xc(%rbp)
    return 0;
  400536:	b8 00 00 00 00       	mov    $0x0,%eax
} 
  40053b:	c9                   	leaveq 
  40053c:	c3                   	retq   
  40053d:	0f 1f 00             	nopl   (%rax)

相關文章