Answer: This is an easy task! Let’s experiment with our machine to confirm this! I’ve written below a C function within which declared several variables, pointers and also a few function prototypes.
void lvar_funpt() { long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10; int *pi1, *pi2, *pi3, *pi4, *pi5, *pi6, *pi7, *pi8, *pi9, *pi10; fun_ret_ptr(); fun_ret_int(); }
x86-64 bit assembly implementation for above code fragment follows
lvar_funpt: pushq %rbp # pushed return address movq %rsp, %rbp # copied stack-pointer to base-pointer movl $0, %eax # move value '0' into '%eax' call fun_ret_ptr # call to function movl $0, %eax call fun_ret_int # call to function popq %rbp # popping off of return add ret
Notice in the assembly code produced for above C function that there aren’t any assembly instructions generated for variables’ declarations or for function prototypes. Notice that I annotated each assembly instruction to its extremely right.
Now, let’s try and see what happens if variables are initialized? We modify the above C code fragment and set to begin with another interesting experiment below,
void lvar_funpt() { long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10; int *pi1, *pi2, *pi3, *pi4, *pi5, *pi6, *pi7, *pi8, *pi9, *pi10; /* assignment */ a1 = 10; a2 = 20; a3 = 30; a4 = 40; a5 = 50; pi1 = (int *)100; pi2 = (int *)200; fun_ret_ptr(); fun_ret_int(); }
x86-64 bit implementation of the above modified C function
lvar_funpt: pushq %rbp # return address saved movq %rsp, %rbp # stack-pointer copied to base-pointer subq $64, %rsp # 64 bytes allocated for stack movq $10, -8(%rbp) # copied a1 movq $20, -16(%rbp) # copied a2 movq $30, -24(%rbp) # copied a3 movq $40, -32(%rbp) # copied a4 movq $50, -40(%rbp) # copied a5 movq $100, -48(%rbp) # copied pi1 movq $200, -56(%rbp) # copied pi2 movl $0, %eax call fun_ret_ptr movl $0, %eax call fun_ret_int leave ret
So, what do you observe differently now? Don’t you see assembly instructions for variables and pointers you had all initialized there. Remember that had any local variables been initialized in their declarations, assembly instructions would appear here to perform assignment.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for Computer Science Internship
- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Apply for C Internship
- Check Computer Science Books