I am new to operating system programming and I am reading a book which gives a simple example of kernel as follows:
main() {
char *video_memory = 0xb8000;
*video_memory = 'X';
}
To compile this file which is called kernel.c, I use MinGW under Windows 7 as follows:
gcc -ffreestanding -c kernel.c -o kernel.o
This creates the object file kernel.o. However, following command does not work.
ld -o kernel.bin -Ttext 0x1000 kernel.o --oformat binary
I receive the following error:
ld: cannot perform PE operations on non PE output file 'kernel.bin'
I cannot solve the problem. Please help me.
Thank you
After helps of Ross, I succeded to jump to kernel offset. However, I cannot call C function from Kernel_entry.asm. Moreover, when I remove the C function from my kernel.bin and change the code as seen below, Three weird chars are shown 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
All messages are shown correctly but the last one in the Kernel_entry.asm is shown as three weird chars. I don't understand what happens.
The first thing you'll need to is change the name of you function. If you call it main
the MinGW version of GCC will insert a call to __main
to do initialization. For example:
start() {
char *video_memory = 0xb8000;
*video_memory = 'X';
}
This means you'll also have to edit kernel_entry.asm
accordingly:
[bits 32]
[extern _start]
call _start
jmp $
Next compile and assemble the two files as you did before, and link it with these commands:
ld -T NUL -o kernel.tmp -Ttext 0x1000 kernel_entry.o kernel.o
objcopy -O binary -j .text kernel.tmp kernel.bin
The first command links the objects as a PECOFF executable, and then the second command converts it to a binary. Now combine the binary with the boot loader:
copy /b boot_sect.bin+kernel.bin os-image
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With