Assembler kernel with multiboot

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
daemoned
Posts: 5
Joined: Wed Oct 14, 2009 8:35 am

Assembler kernel with multiboot

Post by daemoned »

Hello, I recently started out testing some osdev, first with some C tutorials, but have now decided to create my kernel in assembly and load it with grub.
It assemblies fine with NASM (nasm -f bin -o kernel.bin kernel.asm) but it won't boot with grub, I get a "Error 13: Invalid or unsupported executable format"
Can anyone take a quick look I can't figure out what I am doing wrong, I have checked some other assembler kernels thats multiboot but can't figure out what I'm doing wrong, code follows.


File: constants.asm

Code: Select all

; asmsyntax=nasm

;;;;;;;;;;;;;;;;;;;;;;;; MULTIBOOT
MB_MODULEALIGN          equ 1<<0                         ; align loaded modules on page boundaries
MB_MEMINFO              equ 1<<1                         ; provide memory map
MB_AOUT                 equ 1<<16                       
MB_FLAGS                equ MB_MODULEALIGN | MB_MEMINFO | MB_AOUT        ; this is the Multiboot 'flag' field
MB_MAGIC                equ 0x1badb002                   ; 'magic number' lets bootloader find the header
MB_CHECKSUM             equ -(MB_MAGIC + MB_FLAGS)              ; checksum required


;;;;;;;;;;;;;;;;;;;;;;;; KERNEL STUFF
STACKSIZE               equ 0x4000                        ; set size for stack, 16k stack


;;;;;;;;;;;;;;;;;;;;;;;; OTHER STUFF
VIDEO_MEMORY            equ 0xb8000
File: kernel.asm

Code: Select all

; asmsyntax=nasm
ORG 0x100000

BITS 32
CPU 386

%include "constants.asm" ; Alot of constants for making things easier.

MultiBootHeader:
  dd MB_MAGIC
  dd MB_FLAGS
  dd MB_CHECKSUM
  dd MultiBootHeader
  dd kernel_start
  dd kernel_end
  dd kernel_end
  dd kernel_start

kernel_start:
  mov esp, 0x1ffffc ; set up the stack
  push eax ; pass Multiboot magic number
  push ebx ; pass Multiboot info structure
  
  mov [0xb8000], dword 0x07690748
 
  cli

hang:
   hlt                                ; halt machine should kernel return
   jmp   hang

kernel_end:
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Assembler kernel with multiboot

Post by Creature »

GRUB doesn't load binary executables (AFAIK) you have to use the PE format (and the AOUT_KLUDGE) or the ELF format.

Not sure if that's your problem though (but I'm guessing it is, since it's complaining about the executable format).

Besides, these instructions are fairly useless:

Code: Select all

  push eax ; pass Multiboot magic number
  push ebx ; pass Multiboot info structure
But I'm guessing you're going to add a C entry point soon.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Assembler kernel with multiboot

Post by Combuster »

Your multiboot header is (as the error says) invalid. Try fixing the start address for starters.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: Assembler kernel with multiboot

Post by Kevin »

Creature wrote:GRUB doesn't load binary executables (AFAIK) you have to use the PE format (and the AOUT_KLUDGE) or the ELF format.
The binary format shouldn't matter with the a.out kludge.
Developer of tyndur - community OS of Lowlevel (German)
daemoned
Posts: 5
Joined: Wed Oct 14, 2009 8:35 am

Re: Assembler kernel with multiboot

Post by daemoned »

Got it to load now, as a flat binary. Im not 100% sure, but I think it was cause the start address was not pointing to the very start of the binary file?

Thanks for help and pointing it out.

Changed MultiBootHeader

Code: Select all

MultiBootHeader:
  dd MB_MAGIC
  dd MB_FLAGS
  dd MB_CHECKSUM
  dd MultiBootHeader
  dd $$
  dd kernel_end
  dd kernel_end
  dd kernel_start
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Assembler kernel with multiboot

Post by Creature »

Kevin wrote:
Creature wrote:GRUB doesn't load binary executables (AFAIK) you have to use the PE format (and the AOUT_KLUDGE) or the ELF format.
The binary format shouldn't matter with the a.out kludge.
Thanks for pointing that out, I guess I just assumed that it was this way because I've never seen it been done before (I have a lot to learn ;)).
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
Post Reply