在实模式下组合 NASM、BCC 和 Ld86:有错误的幻数

Combining NASM, BCC and Ld86 in real mode: has bad magic number

我编写了一个引导加载程序并使用 NASM 汇编程序(不是 AS86)编译了它,一切都运行良好。

现在,我想学习如何将 16 位 C 代码插入到我的应用程序中。我从几个 SO 那里了解到,由于它支持 8086 处理器,因此建议在这种情况下使用 bcc

在将我的代码与 C 测试代码相结合时,我遇到了以下错误:ld86: testasm.o has bad magic number

我将代码缩减为以下内容:

testasm.asm:

[bits 16]
global foo

foo:
    mov ax, 0x0e41
    int 0x10
    jmp $

testc.c:

extern void foo();

main() {
  foo();
}

Makefile:

CFLAGS=-0 -c
LDFLAGS=-T 0x7C00 -0
ASFLAGS=-f aout

all: testc.bin

testc.bin: testasm.o testc.o
    ld86 -o $@ $^ $(LDFLAGS)

testc.o: testc.c
    bcc -o $@ $^ $(CFLAGS)

testasm.o: testasm.asm
    nasm -o $@ $^ $(ASFLAGS)

clean:
    rm -f *.o testc.bin

我仍然有问题。任何人都知道如何将 NASMbccld86 组合在一起。

对于新手,我发现了这个问题。 NASM的输出格式应该是AS86,以便与LD86兼容。 所以,

ASFLAGS=-f aout

应替换为

ASFLAGS=-f as86

另外,代码还有一个问题: testasm.asm 中的 foo 应该替换为 _foo 但不要问我为什么!