6

Can I read from or write to a variable defined in my assembly file in my C file? I couldn't figure it out on my own. For example the C file looks as follows:

int num = 33;

and produces this assembly code:

    .file   "test.c"
    .globl  _num
    .data
    .align 4
_num:
    .long   33

As i started to learn assembly i heard often the speed is the reason why i have to pick assembly and lower file size and all that stuff...

I am using mingw(32 bit) gnu assembly on windows 7

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
orustammanapov
  • 1,792
  • 5
  • 25
  • 44
  • 7
    don't be so defensive :) – UmNyobe Mar 11 '12 at 22:57
  • 9
    *As i started to learn assembly i heard often the speed is the reason why i have to pick assembly adn lower file size and all that stuff...* Someone who is capable of hand optimizing assembler better than the compiler knows exactly when it is appropriate and when it is not. You don't seem to be there yet. Your compiler likely outputs better assembly than you do (but that doesn't mean stop learning!). – Ed S. Mar 11 '12 at 22:58
  • 1
    exactly that is why i'am asking questions even if they might appear silly at first look – orustammanapov Aug 25 '12 at 13:41

2 Answers2

9

Yes, Linker combines all the .o files (built from .s files) and makes a single object file. So all your c files will first become assembly files.

Each assembly file will have an import list, and an export list. Export list contains all the variables that have a .global or .globl directive. Import list contains all the variables that start with a extern in the c file. (GAS, unlike NASM, doesn't require declaring imports, though. All symbols that aren't defined in the file are assumed to be external. But the resulting .o or .obj object files will have import lists of symbols they use, and require to be defined somewhere else.)

So if your assembly file contains this:

    .globl  _num        # _num is a global symbol, when it is defined
    .data               # switch to read-write data section
    .align 4
_num:                   # declare the label 
    .long  33           # 4 bytes of initialized storage after the label

All you need to do in order to use num, is to create a extern variable like this

extern int num;  // declare the num variable as extern in your C code   

and then you'll be able to read it or modify it.


Many platforms (Windows, OS X) add a leading underscore to symbol names, so the C variable num has an asm name of _num. Linux/ELF doesn't do this, so the asm name would also be num.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
theRealWorld
  • 1,188
  • 2
  • 8
  • 23
  • 1
    @Cherry: your edit introduced multiple errors that I fixed with another edit. Some of them were due to misleading parts of the original answer though. e.g. `.globl _num` doesn't declare `_num`, it only means that *if* it's declared, it will be a global symbol. This answer missed the `_num:` `.long` from teh question. You never want to use C vars with leading underscores in their names; those are always reserved for the standard library and other parts of the implementation. On an ELF platform where there isn't that form of name mangling, you remove `_` from the asm, not add it to the C. – Peter Cordes Dec 21 '18 at 18:23
4

Yes, you can share variables both ways. use the .globl as you have and then in C declare an external variable as if it were in another C module but instead it is in an asm module.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • I've tried it but had some linker problems, here is the assembly piece: `.global num num: .int 13` and my C file: `#include extern int num; int main() { printf("%d\n", num); }` I have a linker problem: `math.c:(.text+0xf): undefined reference to 'num'` what am i doing wrong? – orustammanapov Mar 11 '12 at 23:19
  • are you compiling both files together or just your c file? – theRealWorld Mar 11 '12 at 23:30
  • I am compiling them as follows: gcc -m32 -o test test.c myasm.s – orustammanapov Mar 11 '12 at 23:35
  • 1
    @orustam the variable name should be _num in asm file. Here is another thread talk about this underscore issue on mingw. http://stackoverflow.com/questions/1034852/adding-leading-underscores-to-assembly-symbols-with-gcc-on-win32 – Sam Liao Mar 12 '12 at 02:21
  • @orustam, are you sure `_num` is in the right section? Shouldn't the `.data` directive (and maybe `.align` for that matter) appear beforehand? – Brett Hale Mar 12 '12 at 06:07
  • arsane - THANKS!!!! it worked, i knew that functions should have underscores but never heard about variables. as i disassembled my c file gcc have also put underscores to my variables and i should figure it out on my own, but thanks for your help. – orustammanapov Mar 12 '12 at 08:22