How to link two nasm source files

旧时模样 提交于 2019-12-01 02:18:00

问题


I've got a file that defines very basic IO functions and I want to create another file that uses this file.

Is there a way to link these two files up?

prints.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

usingPrintTest.asm:

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

Note these aren't the actual files... They're just used to explain my problem :)

Thanks!


回答1:


Sure - you just need to use the linker. Assemble each of your files:

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

You can then pass the output objects to your linker. Something like:

gcc -o myProgramName prints.o usingPrintTest.o

Using gcc as the linker driver can solve some funny business with linking the OS libraries you need for your program to run. You might need to make some declarations in usingprintTest.asm to let it know that print_Achar and os_return are going to be defined elsewhere - in nasm, you'll use the extern assembler directive:

extern print_Achar
extern os_return


来源:https://stackoverflow.com/questions/8187028/how-to-link-two-nasm-source-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!