Home > Back-end >  extern command not importing a declared variable in other source file in assembly
extern command not importing a declared variable in other source file in assembly

Time:11-08

So , I am trying to implement a program where it will use a variable declared in other source file. The book I am reading stated that I can import a variable and function name declared in other file by using extern < symbolName > where the symbolName is either the function name or a globally declared variable (i.e. in .data and .bss section). However when I am trying to export the variable var , the declaration of the variable being an external variable is not wrong ,but when trying to use it in code , yasm is notifying me that it is an undeclared name. Why is it happening ?

edit: File1.asm

section .data
var db 123

File2.asm

section .data
SYS_exit equ 60
extern var
section .text
global _start
_start:
mov al,byte[var]
last:
mov rax,SYS_exit
mov rdi,0
syscall

after this, if I assemble, this error happens: ld: Undefined identifier "var"

CodePudding user response:

You need to add the directive global var in File1.asm, before the line var db 123. See https://www.tortall.net/projects/yasm/manual/html/manual.html#nasm-directive-global

By default, when you define a label in an assembly source file, the label is local to that module. It will be used to resolve references from within that same file, but the symbol won't be included in the symbol table of the object file created by the assembler. So it can't be referenced from other files, as the linker would never have heard of that symbol. This is much like a static file-scope variable in C or C .

The global directive causes the assembler to include the symbol in the object file's symbol table, so that other files will be able to reference it, like a global variable in C or C . It's sometimes called "exporting the symbol". That's what you want here.

Note you already used this directive for global _start, and for much the same purpose: the _start symbol needs to appear in the symbol table, so that the linker can know its value to use it as the entry point for the executable.

  • Related