简体   繁体   English

从另一个程序集文件调用程序集程序?

[英]Call assembly procedure from another assembly file?

Just a simple question: 只是一个简单的问题:

Let's say I had the following two assembly programs: 假设我有以下两个汇编程序:

1: 1:

add10:
   add eax, 10
   ret
;call add5 from other file

2: 2:

add5:
   add eax, 5
   ret
;call add10 from other file

Could I call add10 (declared in the first file) from the second file, or vice-versa? 我可以从第二个文件中调用add10 (在第一个文件中声明),反之亦然吗? If so, how can it be done? 如果是这样,怎么办? (even if it isn't feasible) (即使不可行)

NOTE: This will be running on bare metal, not on any fancy NT calls! 注意:这将在裸机上运行,​​而不是在任何花哨的NT电话上运行!

Thanks. 谢谢。

Edit: I'm using NASM on Windows. 编辑:我在Windows上使用NASM。

Two files: 两个文件:

1: 1:

BITS 32

GLOBAL add5

section .code
add5:
    add eax, 5
    ret

2: 2:

BITS 32

EXTERN add5
EXTERN printf
EXTERN ExitProcess

section .data
    fmt db `eax=%u\n`

section .code
add10:
    add eax, 5
    call add5
    ret

_main:
    mov eax, 87
    call add10

    push eax
    push fmt
    call printf
    add esp, 8

    push 0
    call ExitProcess

Assemble & link them together. 组装并将它们链接在一起。 I used as linker GoLink, other linkers are similar: 我用作链接器GoLink,其他链接器类似:

nasm.exe -fwin32 -o add5.obj add5.asm
nasm.exe -fwin32 -o add10.obj add10.asm
GoLink.exe /ENTRY:_main /console /fo add10.exe add5.obj add10.obj kernel32.dll msvcrt.dll

I named the sources "add5.asm" and "add10.asm". 我将源命名为“ add5.asm”和“ add10.asm”。 The assembler produces "add5.obj" and "add10.obj". 汇编器产生“ add5.obj”和“ add10.obj”。 The linker uses "add5.obj" and "add10.obj" and some system libraries (for 'printf' and 'ExitProcess'). 链接器使用“ add5.obj”和“ add10.obj”以及某些系统库(用于“ printf”和“ ExitProcess”)。 The result is the executable "add10.exe". 结果是可执行文件“ add10.exe”。 Look at the command lines to get the order of those names. 查看命令行以获取这些名称的顺序。 The names are arbitrary. 名称是任意的。

HTH HTH

If both files are linked into the same executable, yes. 如果两个文件都链接到相同的可执行文件中,则为是。 Lookup EXTERN or EXTRN. 查找EXTERN或EXTRN。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM