Thread: Register variable declaration

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    54

    Register variable declaration

    What exactly the following declaration will do?
    Code:
    register int *ptr asm ("r9");
    1)Memory is allocated for ptr pointer variable and the address of that memory will be saved in r9 register?
    (OR)
    2)what value we assign to ptr varible will be stored in r9 register?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    ptr is allocated on r9 register and "register" keyword tells the compiler that the address-of ptr cannot be taken.

    Code:
    int *p asm ( "r8" );
    register int *q asm ( "r9" );
    int **pp = &p;  // this will generate an warning
    int **qq = &q; // this will generate an error!
    Last edited by flp1969; 02-23-2019 at 06:10 AM.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    So r9 register will act as a pointer, what ever value we assign to ptr will be stored in r9 register.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by krkr View Post
    So r9 register will act as a pointer, what ever value we assign to ptr will be stored in r9 register.
    The point here is that "register" keyword isn't a modifier to garantee register allocation... The semantics is "you cannot get the address-of this identifier"...
    And, using the GCC extension to allocate a local variable in a specific register is only a hint that can be ignored:

    Code:
    unsigned long f( unsigned long *p, unsigned int count )
    {
      unsigned long sum = 0;
      unsigned long *q asm ("r9");
    
      q = p;
      while ( count-- )
        sum += *q++;
    
      return sum;
    }
    Compiing (SysV x86-64 ABI):

    Code:
    $  cc -O2 -c test.c
    test.c: In function ‘f’:
    test.c:4:18: warning: ignoring asm-specifier for non-static local variable ‘q’
       unsigned long *q asm ( "r9" );
                      ^
    The created code:
    Code:
    f:
      test esi,esi
      lea  eax,[rsi-1]
      je   .L4
      lea  rdx,[rdi+rax*8+8]
      xor  eax,eax
    .L3:
      add  rax,[rdi]
      add  rdi,8
      cmp  rdi,rdx
      jne  .L3
      ret
    .L4:
      xor  eax,eax
      ret
    Where is r9? If you don't use -O2 option, the compiler will, still, ignore the hint. Try it.
    Last edited by flp1969; 02-23-2019 at 06:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Register variable storage
    By onlynishant in forum C Programming
    Replies: 3
    Last Post: 05-30-2012, 02:35 AM
  2. address of register variable
    By siperi in forum C Programming
    Replies: 15
    Last Post: 11-08-2010, 02:26 PM
  3. Register variable
    By karthigayan in forum C Programming
    Replies: 2
    Last Post: 05-30-2009, 03:41 AM
  4. Register variable
    By Lettin03 in forum C Programming
    Replies: 2
    Last Post: 11-20-2007, 06:14 AM
  5. Register variable... where are you?
    By pollopinolo in forum C Programming
    Replies: 7
    Last Post: 12-28-2005, 09:09 AM

Tags for this Thread