Page 1 of 1

Assembler kernel with multiboot

Posted: Wed Oct 14, 2009 8:46 am
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:

Re: Assembler kernel with multiboot

Posted: Wed Oct 14, 2009 10:05 am
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.

Re: Assembler kernel with multiboot

Posted: Wed Oct 14, 2009 10:48 am
by Combuster
Your multiboot header is (as the error says) invalid. Try fixing the start address for starters.

Re: Assembler kernel with multiboot

Posted: Wed Oct 14, 2009 12:35 pm
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.

Re: Assembler kernel with multiboot

Posted: Wed Oct 14, 2009 12:43 pm
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

Re: Assembler kernel with multiboot

Posted: Thu Oct 15, 2009 8:26 am
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 ;)).