Is it possible to access the variables defined in the assembly from C?

Can I read or write a variable defined in my assembly file in my C file? I could not figure it out on my own. For example, a C file looks like this:

int num = 33;

and produces this assembly code:

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

When I started learning assembler, I often heard that speed is the reason I need to choose assembler, reduce file size and all that ...

I am using MNW (32-bit) GNU builds on Windows 7

+5
source share
2 answers

Yes, the linker merges all .o files (created from .s files) and creates one object file. Thus, all your c files will first become build files.

. , .global .globl. , extern c. (, NASM, GAS . , , . .o .obj , , - .)

, :

    .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

, , num, ,

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

.


(Windows, OS X) _num , C num asm _num. Linux/ELF , asm num.

+5

, . .globl, , C , C, asm.

+4

All Articles