Do Local Variables and Function Prototypes in a C Program Produce any Assembly Code

Question6: Do Local Variables and Function Prototypes in a C Program Produce any Assembly Code

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.

advertisement
advertisement

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.