0

I looked for a solution to this a lot, but I didn't find anything helpful. I'm making an OS with assembly and added C++ code. I made a file that links everything named "compileasm.bat":

nasm bootloader.asm -f bin -o bootloader.bin

nasm ExtendedProgram.asm -f elf64 -o ExtendedProgram.o

x86_64-w64-mingw32-gcc -ffreestanding -mno-red-zone -m64 -c "Kernel.cpp" -o "Kernel.o"

ld -T NUL -o Kernel.tmp -Ttext 0x8000 ExtendedProgram.o Kernel.o

ld -T "link.ld"

objcopy -O binary Kernel.tmp Kernel.bin

copy /b bootloader.bin+kernel.bin bootloader.flp

pause

link.ld has the following code:

OUTPUT_FORMAT(binary)

ENTRY (_start)

INPUT
(
    ExtendedProgram.o
    Kernel.o
)

OUTPUT
(
  Kernel.bin
)

SECTIONS
{
    . = 0X8000;

    .text : ALIGN(0x1000)
    {
      *(.text)
    }

    .data : ALIGN(0x1000)
    {
      *(.data)
    }

    .rodata : ALIGN(0x1000)
    {
      *(.rodata)
    }

    .bss : ALIGN(0x1000)
    {
      *(COMMON)
      *(.bss)
    }
}

The C++ file is Kernel.cpp:

#include "TextPrint.cpp"
extern "C" void _start() {
    SetCursorPosition(1000);
    return;
}

And the main file is ExtendedProgram.asm:

jmp EnterProtectedMode

%include "gdt.asm"
%include "print.asm"

EnterProtectedMode:
    call EnableA20
    cli
    lgdt [gdt_descriptor]
    mov eax, cr0
    or eax, 1
    mov cr0, eax
    jmp codeseg:StartProtectedMode

EnableA20:
    in al, 0x92
    or al, 2
    out 0x92, al
    ret

[bits 32]

%include "CPUID.asm"
%include "SimplePaging.asm"

StartProtectedMode:

    mov ax, dataseg
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    mov ebp, 0x90000
    mov esp, ebp

    mov [0xb8000], byte 'H'
    mov [0xb8002], byte 'e'
    mov [0xb8004], byte 'l'
    mov [0xb8006], byte 'l'
    mov [0xb8008], byte 'o'
    mov [0xb800a], byte ' '
    mov [0xb800c], byte 'W'
    mov [0xb800e], byte 'o'
    mov [0xb8010], byte 'r'
    mov [0xb8012], byte 'l'
    mov [0xb8014], byte 'd'
    mov [0xb8016], byte '!'

    call DetectCPUID
    call DetectLongMode
    call SetUpIdentityPaging
    call EditGDT
    jmp codeseg:Start64Bit

[bits 64]
[extern _start]

Start64Bit:
    mov edi, 0xb8000
    mov rax, 0x1f201f201f201f20
    mov ecx, 500
    rep stosq
    call _start
    jmp $

times 2048-($-$$) db 0

I tried the solutions I found but they didn't work at all. What's the problem with the code?

alex12
  • 36
  • 4
  • `ld -T "link.ld"` already makes a flat binary `kernel.bin` (not `kernel.tmp`), that's the point of your linker script. So you don't need to `objcopy` any sections out of it into a flat binary; in fact you can't because there's no metadata for objcopy to find sections. – Peter Cordes Apr 26 '21 at 12:46
  • I added the line temporarily because the ld command was deleting kernel.bin – alex12 Apr 26 '21 at 12:49
  • 1
    Does the first line of `ld` work? The output file of `nasm` should be in "elf64" format but MinGW's `ld` should only accept "coff" file format. – Martin Rosenau Apr 26 '21 at 19:32
  • Oh, Martin makes a good point. It's usually recommended to use ELF cross-compilers for osdev, not native MinGW under Windows. – Peter Cordes Apr 27 '21 at 03:10
  • I installed mingw x86_64 but I don't know if it works as a cross-compiler. I started using mingw because I had problems with the WSL. – alex12 Apr 27 '21 at 06:09

0 Answers0