Search code examples
cassemblynasmreal-modebcc

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


I wrote a bootloader and compiled it using NASM assembler (not AS86), everything was working perfectly.

Now, I want to learn how to insert 16-bit C code into my application. I read from several SOs that bcc is recommended for such situations due to the fact that it supports 8086 processors.

During combining my code with a C test code I faced the following error: ld86: testasm.o has bad magic number

I reduced my code to the following:

testasm.asm:

[bits 16]
global foo

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

testc.c:

extern void foo();

main() {
  foo();
}

and the 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

and I still have the problem. Any one knows how to combine NASM, bcc and ld86 all together.


Solution

  • For new comers, I detected the problem. The output format of NASM should be AS86 in order to be compatible with LD86. So,

    ASFLAGS=-f aout
    

    should be replaced with

    ASFLAGS=-f as86
    

    In addition, the code have another problem: foo in testasm.asm should be replaced with _foo but don't ask me why!