从另一个文件中的nasm调用子例程 [英] nasm calling subroutine from another file
问题描述
我正在做一个项目,该项目将我编写的子例程附加到老师包含的主文件中.他给了我们有关使子程序全局化的说明,但显然我是个白痴.这两个asm文件位于同一文件夹中,我使用的是nasm -f elf -g prt_dec.asm
和ld prt_dec
,然后对main.asm执行相同的操作.这是main.asm中的相关代码:
I'm doing a project that attaches a subroutine that I wrote to a main file included by the teacher. He gave us the instructions for making our subroutine global but apparently I'm an idiot. The two asm files are in the same folder, I'm using nasm -f elf -g prt_dec.asm
and ld prt_dec
and then doing the same for main.asm. Here's the relevant code in the main.asm:
SECTION .text ; Code section.
global _start ; let loader see entry point
extern prt_dec
_start:
mov ebx, 17
mov edx, 214123
mov edi, 2223187809
mov ebp, 1555544444
mov eax, dword 0x0
call prt_dec
call prt_lf
当我使用ld main.o
这是我prt_dec.asm中的代码段:
Here's the a code segment from my prt_dec.asm:
Section .text
global prt_dec
global _start
start:
prt_dec:
(pushing some stuff)
L1_top:
(code continues)
推荐答案
您要在另一个asm文件或目标文件中调用例程吗?
如果您正在组装prt_dec.asm并链接了要在主程序中使用的多个asm文件,则这里是一个示例,其中2个asm文件已组装并链接在一起... *注意* hello.asm
*没有* 带有开始标签!
You want to call a routine in another asm file or object file?
if you are Assembling prt_dec.asm and are linking multiple asm files to use in your main program, here is a sample, 2 asm files Assembled and linked together... * NOTE * hello.asm
*DOES NOT * have a start label!
主要asm文件:hellothere.asm
Main asm file: hellothere.asm
sys_exit equ 1
extern Hello
global _start
section .text
_start:
call Hello
mov eax, sys_exit
xor ebx, ebx
int 80H
第二个asm文件:hello.asm
Second asm file: hello.asm
sys_write equ 4
stdout equ 1
global Hello
section .data
szHello db "Hello", 10
Hello_Len equ ($ - szHello)
section .text
Hello:
mov edx, Hello_Len
mov ecx, szHello
mov eax, sys_write
mov ebx, stdout
int 80H
ret
makefile:
APP = hellothere
$(APP): $(APP).o hello.o
ld -o $(APP) $(APP).o hello.o
$(APP).o: $(APP).asm
nasm -f elf $(APP).asm
hello.o: hello.asm
nasm -f elf hello.asm
现在,如果您只想将代码分成多个asm文件,则可以将它们包含在您的主要源代码中:在主要源文件的开头加上%include "asmfile.asm"
,只需汇编和链接您的主要文件即可.
Now, if you just want to separate your code into multiple asm files, you can include them into your main source: with %include "asmfile.asm"
at the beginning of your main source file and just assemble and link your main file.
这篇关于从另一个文件中的nasm调用子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!