代码之家  ›  专栏  ›  技术社区  ›  i cant think of a name

C文件中定义的变量在程序集文件中未定义[重复]

  •  0
  • i cant think of a name  · 技术社区  · 1 年前

    我对X86_64上的NASM程序集完全陌生。我想在asm程序中访问我的变量toto并增加它的值。到目前为止,我正在做以下工作:

    C程序:

    #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;
    }
    

    汇编程序如下(增量是伪代码)。

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

    我不能用toto作为修改to()的参数,因为它应该用于更复杂的程序中,我不想在其中修改参数。

    我正在使用以下cmd进行汇编

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

    我收到这样的信息:

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

    我的代码出了什么问题?

    0 回复  |  直到 9 年前
        1
  •  3
  •   fuz    9 年前

    你需要把线

    extern toto
    

    在你使用之前的某个地方 toto 告诉NASM toto 是一个外部符号。这就像在C中一样:编译器不知道什么 toto 如果你不声明 toto