Reputation: 7400
I have a C program, and I want to access global variables of this program inside an external assembly file. How do I do this? With NASM or FASM assembler.
Sample code here:
[niko@dev1 test]$ cat cprogram.c
#include <stdio.h>
int array[1024];
void func_increment(int position_index);
int main() {
array[2]=4;
func_increment(2);
printf("val=%d\n",array[2]);
}
[niko@dev1 test]$ cat asmcode.S
use64
global func_increment
section .text
func_increment:
mov eax, array[position] <- how should I insert here the symbol located inside my C program
inc eax
ret
[niko@dev1 test]$
I have many number of types in C programs, for example, a struct type which is declared as array and it is about 32MB long:
typedef struct buf {
char data[REQ_BUF_SIZE];
} buf_t;
I have pointers, integers, and a lot of variable types:
char data[64] __attribute__ ((aligned (16)));
char nl[16] __attribute__ ((aligned (16)));
uint positions[32];
Upvotes: 0
Views: 2116
Reputation: 58822
As far as the symbols go, if they are global you can reference them by name. Depending on assembler and environment you might have to declare the symbol external and/or mangle it by prepending an underscore.
Using 64 bit linux convention and nasm syntax, your code might look like:
extern array
global func_increment
func_increment:
; as per calling convention, position_index is in rdi
; since each item is 4 bytes, you need to scale by 4
inc dword [array + 4 * rdi]
ret
Upvotes: 4