I disassembled a simple C program and I saw that in the beginning of the main function there is an instruction
sub $0x10,%rsp
However, this instruction does not exist in the function "add", which is called by main. Why is that? Here is the assembly. The 4th instruction in main is the sub instruction that I am talking about.
<pre>1149: f3 0f 1e fa endbr64
114d: 55 push %rbp
114e: 48 89 e5 mov %rsp,%rbp
1151: 48 83 ec 10 sub $0x10,%rsp
1155: c7 45 f0 04 00 00 00 movl $0x4,-0x10(%rbp)
115c: c7 45 f4 03 00 00 00 movl $0x3,-0xc(%rbp)
1163: 8b 55 f0 mov -0x10(%rbp),%edx
1166: 8b 45 f4 mov -0xc(%rbp),%eax
1169: 01 d0 add %edx,%eax
116b: 89 45 f8 mov %eax,-0x8(%rbp)
116e: 8b 55 f4 mov -0xc(%rbp),%edx
1171: 8b 45 f0 mov -0x10(%rbp),%eax
1174: 89 d6 mov %edx,%esi
1176: 89 c7 mov %eax,%edi
1178: e8 20 00 00 00 callq 119d <add>
117d: 89 45 fc mov %eax,-0x4(%rbp)
1180: 8b 45 fc mov -0x4(%rbp),%eax
1183: 89 c6 mov %eax,%esi
1185: 48 8d 3d 78 0e 00 00 lea 0xe78(%rip),%rdi # 2004 <_IO_stdin_used+0x04>
118c: b8 00 00 00 00 mov $0x0,%eax
1191: e8 ba fe ff ff callq 1050 <printf@plt>
1196: b8 00 00 00 00 mov $0x0,%eax
119b: c9 leaveq
119c: c3 retq
000000000000119d <add>
119d: f3 0f 1e fa endbr64
11a1: 55 push %rbp
11a2: 48 89 e5 mov %rsp,%rbp
11a5: 89 7d ec mov %edi,-0x14(%rbp)
11a8: 89 75 e8 mov %esi,-0x18(%rbp)
11ab: 8b 55 ec mov -0x14(%rbp),%edx
11ae: 8b 45 e8 mov -0x18(%rbp),%eax
11b1: 01 d0 add %edx,%eax
11b3: 89 45 fc mov %eax,-0x4(%rbp)
11b6: 8b 45 fc mov -0x4(%rbp),%eax
11b9: 5d pop %rbp
11ba: c3 retq
</pre>
Here is the C code for both main and add. Keep in mind that this is just a simple program which I wrote because I wanted to understand assembly better.
#include<stdio.h>
int add(int a, int b);
int main(){
int x = 4;
int y = 3;
int res = x + y;
int resFunc = add(x, y);
printf("%d", resFunc);
return 0;
}
In another file I have the add function which looks like this:
int add(int a, int b){
int result = a + b;
return result;
}