I wrote a very simple C++ function in VS2019 Community Edition and I have a question in corresponding disassembly.
Function:
void manip(char* a, char* b, long* mc) {
long lena = 0;
long lenb = 0;
long lmc;
long i, j;
for (; a[lena] != NULL; lena++);
for (; b[lenb] != NULL; lenb++);
lmc = lena + lenb + *mc;
for (i=0; i < lena; i++) a[lena] = a[lena] + lmc;
for (j=0; j < lenb; j++) b[lenb] = b[lenb] + lmc;
}
Disassembly (Excerpt):
void manip(char* a, char* b, long* mc) {
00007FF720DE1910 mov qword ptr [rsp+18h],r8
00007FF720DE1915 mov qword ptr [rsp+10h],rdx
00007FF720DE191A mov qword ptr [rsp+8],rcx
00007FF720DE191F push rbp
00007FF720DE1920 push rdi
00007FF720DE1921 sub rsp,188h
00007FF720DE1928 lea rbp,[rsp+20h]
00007FF720DE192D mov rdi,rsp
00007FF720DE1930 mov ecx,62h
00007FF720DE1935 mov eax,0CCCCCCCCh
00007FF720DE193A rep stos dword ptr [rdi]
In the first three lines we are placing the arguments in stack before the frame pointer. The frame rbp pointer is pushed after that. What troubles me are following three lines :
00007FF720DE1921 sub rsp,188h
00007FF720DE1928 lea rbp,[rsp+20h]
00007FF720DE192D mov rdi,rsp
Of the three lines above, the first one as I understand reserves the space on the stack.
Questions:
- I do not understand why this huge space (188h) is reserved while we need just enough to save 5 longs, which are no more than 5*4=20 (16h) bytes.
- Second line is calculation of new frame pointer, but I don't understand how did we get 20h(32).
- I also don't get the significance of 3rd line.