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?