added a build.sh and code to kernel.c

This commit is contained in:
Gregory Kenneth Bowne 2024-08-31 17:16:34 -07:00
parent 8dbc66b68b
commit d3a41ed76d
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,44 @@
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check for required commands
for cmd in nasm gcc qemu-system-i386; do
if ! command_exists "$cmd"; then
echo "Error: $cmd is not installed or not in PATH" >&2
exit 1
fi
done
# Compile the assembly file
echo "Compiling boot4.asm..."
nasm -f elf32 boot4.asm -o boot4.o
# Compile and link the kernel
echo "Compiling and linking kernel..."
gcc -m32 -ffreestanding -nostdlib kernel.c boot4.o -o kernel.bin -T linker.ld
# Check if compilation was successful
if [ -f kernel.bin ]; then
echo "Build successful. kernel.bin created."
# Ask user if they want to run QEMU
read -p "Do you want to run QEMU now? (y/n) " -n 1 -r
echo # Move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Running QEMU..."
qemu-system-i386 -enable-kvm -net none -fda kernel.bin
else
echo "QEMU not started. You can run it later with:"
echo "qemu-system-i386 -enable-kvm -net none -fda kernel.bin"
fi
else
echo "Build failed. Check for errors above." >&2
exit 1
fi

View File

@ -0,0 +1,13 @@
#include <stdint.h>
void kmain(void)
{
const uint16_t color = 0x0F00;
const char *hello = "Hello C world!";
volatile uint16_t *vga = (volatile uint16_t *)0xb8000;
for (int i = 0; i < 16; ++i)
{
vga[i + 80] = color | (uint16_t)hello[i];
}
}