I am new to operating system programming, and I am reading a book that gives a simple kernel example as follows:
main() {
char *video_memory = 0xb8000;
*video_memory = 'X';
}
To compile this file called kernel.c, I use MinGW under Windows 7 as follows:
gcc -ffreestanding -c kernel.c -o kernel.o
This creates the kernel.o object file. However, the following command does not work.
ld -o kernel.bin -Ttext 0x1000 kernel.o --oformat binary
I get the following error:
ld: cannot perform PE operations on non PE output file 'kernel.bin'
I can not solve the problem. Please help me.
thank
After Ross's help, I managed to move on to a core shift. However, I cannot call the C function from Kernel_entry.asm. Moreover, when I remove the C function from my kernel.bin and change the code as shown below, three strange characters are displayed on the screen.
Kernel_entry.asm is as follows:
[bits 32]
;[extern _start]
mov ebx, MSG_KERNEL_ENTRY
call print_string_pm
;call _start
jmp $
%include "print_string_pm.asm"
MSG_KERNEL_ENTRY db "Kernel entry is invoked", 0
bootsec.asm is as follows:
[org 0x7c00]
KERNEL_OFFSET equ 0x1000
mov [BOOT_DRIVE], dl
mov bp, 0x9000
mov sp, bp
mov bx, MSG_REAL_MODE
call print_string
call load_kernel
call switch_to_pm
jmp $
%include "print_string.asm"
%include "disk_load.asm"
%include "gdt.asm"
%include "print_string_pm.asm"
%include "switch_to_pm.asm"
%include "clear_screen.asm"
[bits 16]
load_kernel:
mov bx, MSG_LOAD_KERNEL
call print_string
mov bx, KERNEL_OFFSET
mov dh, 15
mov dl, [BOOT_DRIVE]
call disk_load
ret
[bits 32]
BEGIN_PM:
;call clear_screen
mov ebx, MSG_PROT_MODE
call print_string_pm
call KERNEL_OFFSET
jmp $
BOOT_DRIVE db 0
MSG_REAL_MODE db "Started in 16-Bit Real Mode", 0
MSG_PROT_MODE db "Successfully switched to 32-Bit Protected Mode", 0
MSG_LOAD_KERNEL db "Loading Kernel into memory", 0
times 510 - ($ - $$) db 0
dw 0xaa55
, Kernel_entry.asm . , .