Skip to content

Instantly share code, notes, and snippets.

@alirezaarzehgar
Last active May 25, 2024 07:11
Show Gist options
  • Select an option

  • Save alirezaarzehgar/f111f57dc1246eb241514c5948a85a01 to your computer and use it in GitHub Desktop.

Select an option

Save alirezaarzehgar/f111f57dc1246eb241514c5948a85a01 to your computer and use it in GitHub Desktop.
Hello World OS
#!/usr/bin/env bash
nasm -f elf32 kernel.asm -o kasm.o
gcc -m32 -c kernel.c -o kc.o
ld -m elf_i386 -T link.ld -o kernel kasm.o kc.o
mkdir -p root/boot/grub
cp kernel root/boot
echo "multiboot /boot/kernel;boot" > root/boot/grub/grub.cfg
grub-mkrescue -o os.iso root
rm -rf root *.o
section .text
align 4
dd 0x1BADB002
dd 0x00
dd - (0x1BADB002 + 0x00)
global start
extern kernel_main
start:
cli
mov esp, stack_space
call kernel_main
hlt
section .bss
resb 8192
stack_space:
#define VIDEO_MEMORY (char *)0xb8000
char *vid = VIDEO_MEMORY;
void print_str(const char *const str, unsigned int len)
{
for (unsigned int i = 0; i < len; i++)
{
*(vid++) = str[i];
*(vid++) = 0x07;
}
}
void clear_screen()
{
const char *str = " ";
for (int i = 0; i < 80 * 25; i++)
print_str(str, 10);
vid = VIDEO_MEMORY;
}
void kernel_main(void)
{
clear_screen();
print_str("Ha bemola ke!", 13);
}
@alirezaarzehgar
Copy link
Copy Markdown
Author

Install dependencies on debian:

apt install mtools grub-pc-bin nasm

Run project:

chmod +x instsall.sh
./install.sh

Note:
You may have following error:

./install.sh
./install.sh: line 2: nasm: command not found
ld: cannot find kasm.o: No such file or directory
cp: cannot stat 'kernel': No such file or directory
grub-mkrescue: error: `mformat` invocation failed

You should install nasm, mtools.

You may build your os without error, but after booting iso using qemu, qemu say no bootable device. You should install grub-pc-bin.

Boot iso using qemu:

qemu-system-x86_64 -cdrom ./os.iso
qemu-system-x86_64 -kernel kernel

Resource: https://wiki.osdev.org/Bare_Bones

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment