compiler-construction - 如何链接两个nasm源文件

标签 compiler-construction assembly linker nasm extern

我有一个定义非常基本的 IO 函数的文件,我想创建另一个使用该文件的文件。

有没有办法将这两个文件链接起来?

打印.asm:

os_return:
    ;some code to return to os
print_AnInt:
    ;some code to output an int, including negatives - gets param from stack
print_AChar:
    ;some code to output a char - gets param from stack

使用PrintTest.asm:
main:
   push qword 'a'
   call print_AChar ;gets this from prints.asm somehow (that's my question)
   call os_return   ;and this too..

请注意,这些不是实际文件......它们只是用来解释我的问题:)

谢谢!

最佳答案

当然 - 你只需要使用链接器。组装每个文件:

nasm -o prints.o prints.asm
nasm -o usingPrintTest.o usingPrintTest.asm

然后您可以将输出对象传递给您的链接器。就像是:
gcc -o myProgramName prints.o usingPrintTest.o

使用 gcc因为链接器驱动程序可以通过链接程序运行所需的操作系统库来解决一些有趣的问题。您可能需要在 usingprintTest.asm 中进行一些声明让它知道 print_Acharos_return将在别处定义 - 在 nasm 中,您将使用 extern汇编指令:
extern print_Achar
extern os_return

关于compiler-construction - 如何链接两个nasm源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8187028/

相关文章:

assembly - 使用手写汇编调用 native 代码

c - 与-O3相比,gcc -Ofast的汇编代码中计算不精确的来源在哪里?

linux - 内核模块中的符号

c++ - OpenCV + 实感 : Visual Studio Link 2019 error

gcc - -lgcc_s 和 gcc 的区别

Java:方法名称/签名解析是否静态完成(编译时)?

visual-studio-2005 - Visual Studio,如何输出构建所花费的时间?

c++ - VS2010和C/C++后台编译

c - 共享库中定义的变量的内存位置

c# - JIT 编译器如何编译泛型?