Nico
Nico

Reputation: 195

NASM X86_64 global variable from C

I am completely new to NASM assembly on X86_64.I would like to access my variable toto in an asm programm and increment its value. So far I'm doing the following:

C program:

#include <stdio.h>
#include <inttypes.h>

int64_t toto;
extern void modifytoto(void);

int main() {
    toto=0;
    modifytoto();
    printf("toto = %d \n",toto);
    return 0;
}

and the assembly program is the following (the incrementation is pseudo-code).

global  modifytoto
global  toto

section .text
        modifytoto:
            mov rax, 1
            mov toto, rax
            ret  

I can't use toto as an argument to modifytoto() because this is supposed to be used in a more complex program in which I don't wan't to modify the arguments.

I'm assemblying with the following cmd

nasm -f elf64 -o mix_asm.o kernel3.asm

and I'm getting this message:

kernel3.asm:7: error: symbol `toto' undefined

What is wrong with my code ?

Upvotes: 4

Views: 2245

Answers (1)

fuz
fuz

Reputation: 93014

You need to put the line

extern toto

somewhere before you use toto to tell NASM that toto is an external symbol. That's like in C: The compiler has no idea what toto is supposed to be if you don't tell it by declaring toto.

Upvotes: 3

Related Questions