Thread: Speed of pointers vs. speed of arrays/structs

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Kempelen View Post
    I don't know why, but mingw get an error "syntax error before asm", any idea?
    With the code I just posed, or with your post?

    My code compiles with gcc-mignw 3.4.2.

    What options are you using for gcc? If you give -ansi, then all asm-statements are syntax errors, since asm is not ansi compliant. You can still use inline assembler if you use __asm__ instead of asm, in which case my code also compiles with -ansi.

    Code:
    register int x __asm__("ebx");
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    With the code I just posed, or with your post?

    My code compiles with gcc-mignw 3.4.2.

    What options are you using for gcc? If you give -ansi, then all asm-statements are syntax errors, since asm is not ansi compliant. You can still use inline assembler if you use __asm__ instead of asm, in which case my code also compiles with -ansi.

    Code:
    register int x __asm__("ebx");
    --
    Mats
    Hi Mats, thx for you support. At the end, I think i can't test it, because I have multiple files, and if I declare a variable as register, then it is not extern and vice-versa. Only one storage class type is allowed.
    I have test it declaring the variable as register in .h, and normal one in .c. It compile good but dont get expected result when run.
    Anyway I didn't confident I would obtein speed improvements.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can't you use the global register declaration in a header-file (that is then included everywhere)[1]? I'm pretty sure that will work. Although I still think the compiler on it's own will do a better job, so you may well be right that you won't benefit much (if anything). I'd be interested to see what happens, however.

    [1] This really shouldn't cause any problems, as there won't be a global symbol for something that the compiler makes into a register.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    Can't you use the global register declaration in a header-file (that is then included everywhere)[1]? I'm pretty sure that will work. Although I still think the compiler on it's own will do a better job, so you may well be right that you won't benefit much (if anything). I'd be interested to see what happens, however.

    [1] This really shouldn't cause any problems, as there won't be a global symbol for something that the compiler makes into a register.

    --
    Mats
    I can use it. I was refering that the version of the program that is declared as extern is running OK and I get the expected results from the program. If I declare as register in the way writed above, I can compile, but when run the program don't show the same result as the non-register version, even a crash. dont know why.
    Also, when compile I get an error : "warning: unknow register name: reg" that I dont understand (dont know where 'reg' came from)

    (EDIT: forget to say that the crash is in a line where the global reg var is not used, so it is more extrange.....isn't it?)
    Last edited by Kempelen; 06-26-2008 at 06:53 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Kempelen View Post
    I can use it. I was refering that the version of the program that is declared as extern is running OK and I get the expected results from the program. If I declare as register in the way writed above, I can compile, but when run the program don't show the same result as the non-register version, even a crash. dont know why.
    Also, when compile I get an error : "warning: unknow register name: reg" that I dont understand (dont know where 'reg' came from)
    I'll have a little play...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This works:
    Code:
    // asmvars.h
    register int x __asm__("ebx");
    
    //asmvars.c
    #include <stdio.h>
    
    #include "asmvars.h"
    
    extern int getx(void);
    
    int main()
    {
      x = 7;
      printf("x = %d\n", getx());
      return 0;
    }
    
    // asm2.c
    #include "asmvars.h"
    
    int getx()
    {
      return x;
    }
    compiled with
    Code:
    gcc -Wall -ansi asmvars.c asm2.c
    And I looked at the code generated, it is using ebx when I expect it to.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I just had a couple of thoughts:
    First of all, the register you choose must be one that is "preserved" by the compiler - otherwise, it won't work. EBX, EDI, ESI are your only choice (but 64-bit machine also has R8-R15 for you to use), as EAX, ECX, EDX, EBP and ESP are used by the compiler either as scratch-pad registers (presumed lost when calling functions) or as very specific use (ESP -> stack pointer, EBP -> frame pointer).

    Second, I doubt this is a commonly used feature in gcc - there may well have bugs in the compiler that make things "go wrong" if you use it in a sufficiently complex piece of code - it may for example "forget" that the register is a global variable, and use it for temporary use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 08:30 AM
  3. pointers to pointers within structs
    By Lord_azrael99 in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 04:29 AM
  4. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 02:02 AM
  5. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 04:55 PM