Is this True that all Variables Declared to be Registers in a C Program Allocated Necessarily in Register Memory

Question: Is this true that all the Variables Declared to be Registers in a C Program Given Allocation Necessarily in Register Memory

Answer: Thus far, we have become familiar with ‘general purpose register set of x86-64 bit’ architecture. There are 6 registers reserved for obtaining arguments being passed to a procedure. These are:

%rdi
%rsi
%rdx
%rcx
%r8
%r9

When one function calls other function with up to max 6 arguments (integers and pointers) being passed to it, these arguments are stored into the above mentioned registers in sequence from upside down. Notice that floating-point variables are passed through floating-point registers apart from registers mentioned above.

So, what will happen if one function calls other function with more than 6 arguments and are all declared registers in the called function? Don’t you like to experiment this case and observe the result? Aren’t you? Sure! Let’s write a simple C program for the issue in hand, then generate assembly for that to clear the confusion.

  1 #include <stdio.h>
  2
  3 void register_args(int, long, long, int, int *, long *,
  4                register int, register char *);
  5
  6
  7 int main(void)
  8 {
  9     int a1 = 10, d4 = 20, e7 = 30;
 10     int *pi5 = &a1;
 11     long b2 = 40, c3 = 50;
 12     long *pl6 = &b2;
 13     char f9 = 'A';
 14     char *pc8 = &f9;
 15
 16     register_args(a1, b2, c3, d4, pi5, pl6, e7, pc8);
 17     return 0;
 18 }
 19
 20 void register_args(int a1, long b2, long c3, int d4, int *pi5,
 21     long *pl6, register int e7, register char *pc8)
 22 {
 23     /* do here manipulations */
 24     e7 += 1;
 25     *pc8 = 'B';
 26
 27 }

Notice that in above C program, main() called ‘register_args()’ with 8 arguments, Arguments 7 and 8 are declared registers in called function. Let’s switch to assembly output to look into what interesting happened!

advertisement
advertisement
register_args:
        pushq   %rbp
        movq    %rsp, %rbp
        movl    %edi, -4(%rbp)
        movq    %rsi, -16(%rbp)
        movq    %rdx, -24(%rbp)
        movl    %ecx, -8(%rbp)
        movq    %r8, -32(%rbp)
        movq    %r9, -40(%rbp)
        movq    24(%rbp), %rax
        movb    $66, (%rax)
        popq    %rbp
        ret

So, what do you observe? It’s obvious by looking into assembly. As it was sure that the first six parameters would be allocated into register to the available 6 and it’s so. The rest two, ‘e7’ and ‘pc8’, even though , were declared registers not given register allocation. They were pushed onto stack. Recall that variables, more than six and excepting the first six, if all declared registers are not given register allocation because of fixed no. of registers for arguments being passed to a procedure.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

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

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.