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
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
.globalor.globldirective. 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.oor.objobject files will have import lists of symbols they use, and require to be defined somewhere else.)So if your assembly file contains this:
All you need to do in order to use num, is to create a extern variable like this
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
numhas an asm name of_num. Linux/ELF doesn’t do this, so the asm name would also benum.