【问题标题】:nasm calling subroutine from another filenasm 从另一个文件调用子例程
【发布时间】:2013-03-01 01:11:03
【问题描述】:

我正在做一个项目,该项目将我编写的子程序附加到老师包含的主文件中。他给了我们使我们的子程序全局化的说明,但显然我是个白痴。这两个 asm 文件在同一个文件夹中,我使用 nasm -f elf -g prt_dec.asmld prt_dec,然后对 main.asm 执行相同操作。这是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 时,call prt_dec 行会抛出“未定义的 prt_dec 引用”

这是我的 prt_dec.asm 中的 a 代码段:

    Section .text
    global prt_dec
    global _start

start:
prt_dec:
      (pushing some stuff)
L1_top:
(code continues)

【问题讨论】:

    标签: nasm subroutine


    【解决方案1】:

    您想调用另一个 asm 文件或目标文件中的例程吗? 如果您正在组装 prt_dec.asm 并且正在链接多个 asm 文件以在主程序中使用,这里是一个示例,2 个 asm 文件已组装并链接在一起... * 注意 * hello.asm *DOES NOT * 有开始标签!

    主 asm 文件: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

    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
    

    制作文件:

    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",然后组装并链接您的主文件。

    【讨论】:

    • 谢谢,看来问题出在我组装文件的方法上。
    • 谢谢%include "asmfile.asm" 是我需要的。组装最难的部分是缺乏文档。
    猜你喜欢
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多