Write code for a C kernel
- User
Write code for a C kernel
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Write code for a C kernel
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is an example of a minimal kernel written in C together with a tiny assembly “bootloader” (really just the Multiboot header and a small start routine) that you can build into a bootable binary. Please note: This is a very basic kernel intended for educational purposes (for example, following OSDev tutorials). In a real-world kernel you would need far more setup (e.g. setting up descriptor tables, paging, interrupt handlers, etc.).
Below you’ll find three files:
You’ll then compile and link them to produce a binary that GRUB can load.
/* boot.s */
/*
* This file contains a minimal Multiboot header and a start routine.
* The Multiboot header tells a Multiboot-compliant bootloader (like GRUB)
* that this OS is compatible.
*/
.section .multiboot
.align 4
/* Multiboot header magic constant */
.long 0x1BADB002
/* Flags: 0 means default settings */
.long 0x0
/* Checksum: correct value so that (magic + flags + checksum) == 0 */
.long -(0x1BADB002 + 0x0)
.section .text
.global start
start:
/* Call the C kernel entry point. */
call kernel_main
hang:
jmp hang
/* kernel.c */
void kernel_main() {
/*
* Write a simple message directly to VGA text mode memory.
* VGA text mode memory starts at 0xb8000.
* Each character on screen is represented by two bytes:
* - the character's ASCII code
* - the attribute (color information)
*/
char *video_memory = (char *)0xb8000;
const char *message = "Hello, kernel!";
for (int i = 0; message[i] != '\0'; i++) {
video_memory[i * 2] = message[i]; // Character
video_memory[i * 2 + 1] = 0x07; // Attribute: Light grey on black
}
/* Loop forever so the kernel does not return. */
while (1) { }
}
/* link.ld */
ENTRY(start)
OUTPUT_FORMAT("elf32-i386")
OUTPUT_ARCH(i386)
ENTRY(start)
SECTIONS
{
/* Place the code starting at an address commonly used for kernels.
For example, 0x00100000 means our kernel will load at 1 MB in memory. */
. = 0x00100000;
.text : {
*(.multiboot)
*(.text*)
}
.rodata : {
*(.rodata*)
}
.data : {
*(.data*)
}
.bss : {
*(.bss*)
*(COMMON)
}
}
Install Dependencies:
Make sure you have the GNU assembler (as), GCC, and GNU linker (ld) installed.
Assemble and Compile:
You can use the following commands from the directory where your files are located:
# Assemble the bootloader
as --32 boot.s -o boot.o
# Compile the kernel C code
gcc -m32 -ffreestanding -c kernel.c -o kernel.o
The flag -m32 tells GCC to produce 32-bit code. The -ffreestanding flag tells the compiler that there is no standard library (which is typical for kernel development).
Link:
Link the object files using the custom linker script:
ld -m elf_i386 -T link.ld -o kernel.bin boot.o kernel.o
Create a Bootable ISO (Optional):
To test your kernel, you can create a bootable ISO image using GRUB. First, create the following directory structure:
iso/
└── boot/
└── grub/
└── grub.cfg
Create a grub.cfg file inside iso/boot/grub/ with the following content:
menuentry "My minimal kernel" {
multiboot /boot/kernel.bin
boot
}
Then copy your binary:
mkdir -p iso/boot
cp kernel.bin iso/boot/
Finally, build the ISO with:
grub-mkrescue -o mykernel.iso iso
Test in an Emulator:
You can test the generated ISO using QEMU:
qemu-system-i386 -cdrom mykernel.iso
You should see a black screen with "Hello, kernel!" printed near the top left.
This example should serve as a starting point for experimenting with kernel development in C. Enjoy exploring and learning!