Thread: Accessing C global variable from assembly file

  1. #16
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Simplest working example I could think of:
    test.c
    Code:
    #include <stdio.h>
    
    void asmfunction(void);
    
    int test_variable = 0;
    
    int main()
    {
    	printf("before asm: %d\n", test_variable);
    	asmfunction();
    	printf("after asm: %d\n", test_variable);
    	return 0;
    }
    test.S
    Code:
    .globl _asmfunction
    
    _asmfunction:
    	movl $12345, (_test_variable)
    	ret
    $ gcc test.c test.S
    $ ./a.exe
    before asm: 0
    after asm: 12345

    Notice how all the names are prefix with an underscore in the assembler file but not in the C file. Although I'm not sure if this is standard for gcc/gas or if it is cygwin specific.
    Edit:
    I just noticed that the underscore and lack of '$' had already been mentioned before. I blame needing to look up this stupid AT&T syntax which took to much time

    Quote Originally Posted by tabstop View Post
    I have to admit I hadn't heard of RIP until two minutes ago.
    RIP is the x64 version of the instruction pointer.
    It can be used for addressing variables in a way where the address of the variable is stored as an offset relative to the current IP to create position independent code. This is called RIP-relative addressing.
    For some more detailed information see for example Intel® 64 and IA-32 Architectures Software Developer's Manuals volume 2a section 2.2.1.6
    Last edited by _Mike; 08-03-2011 at 02:58 PM.

  2. #17
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by _Mike View Post
    Notice how all the names are prefix with an underscore in the assembler file but not in the C file. Although I'm not sure if this is standard for gcc/gas or if it is cygwin specific.
    It isn't cygwin specific, it is the standard for gcc/gas. I don't blame you with the AT&T syntax, I cry a little bit everytime I see it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #18
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,792
    >>Notice how all the names are prefix with an underscore in the assembler file but not in the C file. Although I'm not sure if this is standard for gcc/gas or if it is cygwin specific.

    AFAIK its convention, not the rule. As in my example earlier, I didn't bother with the prefix and as I was using the same build tool (not using NASM or something ant then linking in the object files) it was able to recognise the symbol as external and link it up. It worked on a recent version of GCC on Ubuntu

  4. #19
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Fordy View Post
    AFAIK its convention, not the rule. As in my example earlier, I didn't bother with the prefix and as I was using the same build tool (not using NASM or something ant then linking in the object files) it was able to recognise the symbol as external and link it up. It worked on a recent version of GCC on Ubuntu
    Well, I stand corrected. Thank you for clearing that up Fordy. *runs back to NASM land*
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #20
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,792
    Quote Originally Posted by AndrewHunter View Post
    Well, I stand corrected. Thank you for clearing that up Fordy. *runs back to NASM land*
    Actually, I just realized something. I assumed that the OP was using Linux where this thing seems to operate differently. Most compilers on 32 bit windows seem to prefix their C function symbols with and underscore if they are using the STDCALL or CDECL calling convention (and it seems variables follow this). Testing my code on Mingw shows that they have adopted the same methods so my original code would not build on windows without the underscore.

    Ah well...I guess that's partly why they invented C - to cover up all this nastiness!

  6. #21
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Isn't that the truth. I do enjoy the occasional assembly but I think I would jab my eyes out if that was the only programming language.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #22
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Fordy View Post
    Actually, I just realized something. I assumed that the OP was using Linux where this thing seems to operate differently. Most compilers on 32 bit windows seem to prefix their C function symbols with and underscore if they are using the STDCALL or CDECL calling convention (and it seems variables follow this). Testing my code on Mingw shows that they have adopted the same methods so my original code would not build on windows without the underscore.

    Ah well...I guess that's partly why they invented C - to cover up all this nastiness!
    Wow, this is strange. I just rebooted to linux to test and I can confirm this.
    I can understand that different compilers might have different naming schemes, but the same compiler just on different platforms?
    And even stranger that they only apply this (re)naming scheme to the compiler and not the assembler to maintain consistency and portability.

  8. #23
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,792
    Quote Originally Posted by _Mike View Post
    Wow, this is strange. I just rebooted to linux to test and I can confirm this.
    I can understand that different compilers might have different naming schemes, but the same compiler just on different platforms?
    And even stranger that they only apply this (re)naming scheme to the compiler and not the assembler to maintain consistency and portability.
    I assume it's due to the C/C++ standards not specifying how the symbols appear to the linking process - C++ has name mangling but AFAIK doesn't specify how that mangling is represented, that's why its difficult to link a MSVC exported symbol to one in Borland without using 'extern "C"'. If the standards dont specify how symbols are represented I guess its down to someone else - I imagine GCC would pretty much decide for the linux platform and I'm pretty sure Microsoft had the final call for Windows (well the C part anyway). If this convention was rooted in the compilers MS produced it makes sense that other compiler producers would follow it when they built compilers for that platform.

  9. #24
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Yeah you make a valid point. I suppose the _ prefix on windows is so you can link object files produced with different compilers together. I just tested linking 2 object files, one compiled in cygwin-gcc and one in msvc and it works.
    I had always assumed that the binary formats of the object files would be different and make this impossible.
    But they could have made the gas assembler follow the same convention and add an implicit underscore to global names to make things easier for the programmer.

  10. #25
    Registered User
    Join Date
    Jun 2009
    Posts
    26
    Hi guys,

    I tried pretty much what _Mike suggested but without calling the assembly function form the C code.

    That is, I tried that:

    main.c
    Code:
    void my_test_assembly_funct(void);
    int test_variable=0;
    
    int main(void)
    {
       ...
    }
    test.s
    Code:
    .globl _my_test_assembly_funct
    
    _my_test_assembly_funct:
    	movl $12345, (_test_variable)
    	ret
    Unfortunately, I still got the "undefined reference to `_test_variable'" error so I don't really know what else to try...

    Maybe it has to do with the linker or something?

    Thanks.

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are linking the two .o files together (main.o and test.o)?

  12. #27
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,792
    How are you building it, what Operating system and what version of GCC?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Refering to a global variable in a different object file
    By Canadian0469 in forum C Programming
    Replies: 1
    Last Post: 11-11-2007, 08:53 PM
  2. Global Assembly Cache
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-22-2007, 12:08 AM
  3. Initializomg a global variable from a data file
    By cybernike in forum C++ Programming
    Replies: 4
    Last Post: 06-27-2007, 01:07 AM
  4. accessing a variable from another C file
    By cblix in forum C Programming
    Replies: 3
    Last Post: 12-05-2005, 06:16 AM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM