Home > OS >  why is c program reserving space for local variables unused?
why is c program reserving space for local variables unused?

Time:06-14

I'm reading Programming from the Ground Up. pdf address: http://mirror.ossplanet.net/nongnu/pgubook/ProgrammingGroundUp-0-8.pdf

I'm curious about Page37's reserve space for local variables. He said, we need to 2 words of memory, so move stack pointer down 2 words. execute this instruction: subl $8, %esp so, here, I think I'm understand.

But, I write c code to verify this reserve space.

#include <stdio.h>

int test(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int a11, int a12) {
    printf("a1=%#x, a2=%#x, a3=%#x, a4=%#x, a5=%#x, a6=%#x, a7=%#x, a8=%#x, a9=%#x, a10=%#x, a11=%#x, a12=%#x", a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12);

    return 0;
}

int main(void){
    test(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12);
    printf("Wick is me!");

    return 0;
}

then, I use gcc convert to Executable file, gcc -Og -g, and use gdb debugger.

I use disass to main function, and copied some of the asm code in below.

   0x000055555555519d < 0>: endbr64 
   0x00005555555551a1 < 4>: sub    $0x8,%rsp  # reserve space?
   0x00005555555551a5 < 8>: pushq  $0x12
   0x00005555555551a7 < 10>:    pushq  $0x11
   0x00005555555551a9 < 12>:    pushq  $0x10
   0x00005555555551ab < 14>:    pushq  $0x9
   0x00005555555551ad < 16>:    pushq  $0x8
   0x00005555555551af < 18>:    pushq  $0x7
   0x00000000000011b1 < 20>:    mov    $0x6,%r9d
   0x00000000000011b7 < 26>:    mov    $0x5,%r8d
   0x00000000000011bd < 32>:    mov    $0x4,           
  • Related