Not getting GRUB magic number

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
lwinkenb
Posts: 3
Joined: Tue Jul 14, 2009 12:02 am

Not getting GRUB magic number

Post by lwinkenb »

I'm following the bare bones tutorial, and I've modified it a little bit to do a higher half kernel. The problem is that instead of getting 0x2BADB002 as the magic number, I get 0x2BAD0010. I'm using Bochs to run my kernel.

My loader file looks like:

Code: Select all

global loader           ; making entry point visible to linker
extern kmain            ; kmain is defined elsewhere

; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ  1<<0                   ; align loaded modules on page boundaries
MEMINFO     equ  1<<1                   ; provide memory map
FLAGS       equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ    0x1BADB002           ; 'magic number' lets bootloader find the header
CHECKSUM    equ -(MAGIC + FLAGS)        ; checksum required

section .text
align 4
MultiBootHeader:
    dd MAGIC
    dd FLAGS
    dd CHECKSUM

; reserve initial kernel stack space
STACKSIZE equ 0x4000                  ; that's 16k.

loader:
    ; load a temporary GDT
    lgdt [tempgdt]
    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax

    ; jump to the higher half kernel
    jmp 0x08:higherhalf

higherhalf:
    ; The CPU will now translate every address by adding
    ; the base 0x40000000

    mov esp, stack+STACKSIZE           ; set up the stack
    push eax                           ; pass Multiboot magic number
    push ebx                           ; pass Multiboot info structure

    call  kmain                       ; call kernel proper

    cli

hang:
    hlt                                ; halt machine should kernel return
    jmp   hang

[global gdt_flush] ; make gdt_flush accessible from C code
[extern gp]        ;

gdt_flush:
    lgdt [gp]
    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    jmp 0x08:flush2

flush2:
    ret

;
; Code to load the interrupt descriptor table
;
[global idt_load]
extern idtp
idt_load:
    lidt [idtp]
    ret

[section .setup]

tempgdt:
    dw gdt_end - gdt - 1 ; size of the GDT
    dd gdt               ; address of the GDT

gdt:
    dd 0, 0
    db 0xFF, 0xFF, 0, 0, 0, 10011010b, 11001111b, 0x40  ; code selector 0x08: base 0x40000000, limit 0xFFFFFFFF, type 0x9A, granularity 0xCF
    db 0xFF, 0xFF, 0, 0, 0, 10010010b, 11001111b, 0x40  ; data selector 0x10: base 0x40000000, limit 0xFFFFFFFF, type 0x92, granularity 0xCF

gdt_end:


section .bss
align 32
stack:
   resb STACKSIZE                     ; reserve 16k stack on a quadword boundary
Thanks guys.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Not getting GRUB magic number

Post by xenos »

The magic is passed in EAX. Now look at your code:
lwinkenb wrote:

Code: Select all

    mov ax, 0x10
You don't save the contents of EAX before writing to it. Any questions left? ;)
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
lwinkenb
Posts: 3
Joined: Tue Jul 14, 2009 12:02 am

Re: Not getting GRUB magic number

Post by lwinkenb »

Thanks XenOS. I'm very new to assembly; I actually thought that ax and eax were different registers :oops:

It's working now though. Thank you :)
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Not getting GRUB magic number

Post by Love4Boobies »

You don't know the difference between EAX and AX and you're developing an OS...? :shock:
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
lwinkenb
Posts: 3
Joined: Tue Jul 14, 2009 12:02 am

Re: Not getting GRUB magic number

Post by lwinkenb »

Hey, everybody's got to start somewhere.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Not getting GRUB magic number

Post by Love4Boobies »

Agreed but working on an OS is not just 'somewhere', it's the most difficult thing you will probably do as a programmer. Perhaps you should learn assembly and learn more about the architecture you are developing for. Stuff like design and theory can come later, when you actually start learning OSDev'ing. That's just a friendly advice.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Coty
Member
Member
Posts: 286
Joined: Thu Feb 12, 2009 5:12 pm

Re: Not getting GRUB magic number

Post by Coty »

Love4Boobies wrote:You don't know the difference between EAX and AX and you're developing an OS...? :shock:
actually I started trying to make an OS before I new anything but the slim basics of C++ :oops:
EAX 32bit version of AX
My hero, is Mel.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Not getting GRUB magic number

Post by Solar »

Coddy wrote:EAX 32bit version of AX
True, but not the whole truth.

Please do take a step back, and don't write OS related code before you are confident in your main language of choice, and have read at least one Assembler tutorial (not a bootloader howto, but a "real" Assembler tutorial) and at least book 3 of the Intel Manuals in full.

Anything less will only lead to pain and desaster.
Every good solution is obvious once you've found it.
User avatar
Coty
Member
Member
Posts: 286
Joined: Thu Feb 12, 2009 5:12 pm

Re: Not getting GRUB magic number

Post by Coty »

Well every thing I have read only describes EAX as a 32 bit register nothing more, same with all the others like ECX, EDX, ect.
My hero, is Mel.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Not getting GRUB magic number

Post by Solar »

Then you have had lousy sources for your Assembly knowledge, indeed. :shock:

Try The Art of Assembly, PC ASM or Assembly Language step by step.
Every good solution is obvious once you've found it.
User avatar
Coty
Member
Member
Posts: 286
Joined: Thu Feb 12, 2009 5:12 pm

Re: Not getting GRUB magic number

Post by Coty »

Well, I am still reading my IBM PC assembly language and programing by Peter Able and I am about half way through, But I do allot more exercises, so I can grasp it more, I am not very far into it :(

Besides it I use the OS-dev wiki and mini DOS
My hero, is Mel.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Not getting GRUB magic number

Post by Solar »

Just for the records, and those poor sods who will google this thread in two years or so...

eax is 32 bits. The lower 16 bits of eax can be accessed as ax. The lower 8 bits of ax can be accessed as al, the higher 8 bits as ah.

All of this takes place in the same register, i.e. a write to al changes eax and vice versa.
Every good solution is obvious once you've found it.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Not getting GRUB magic number

Post by Brendan »

Hi,
Solar wrote:eax is 32 bits. The lower 16 bits of eax can be accessed as ax. The lower 8 bits of ax can be accessed as al, the higher 8 bits as ah.

All of this takes place in the same register, i.e. a write to al changes eax and vice versa.
For the sake of completeness, RAX is the 64-bit version of the same register. Unlike writing to AX, AH or AL (which doesn't effect the highest 16-bits of EAX), writing to EAX does cause the highest 32-bits of RAX to be zeroed.

The same relationships apply to all general registers: RAX/EAX/AX/AH/AL, RBX/EBX/BX/BH/BL, RCX/ECX/CX/CH/CL, RDX/EDX/DX/DH/DL, RSI/ESI/SI/SIL, RDI/EDI/DI/DIL, RBP/EBP/BP/BPL, RSP/ESP/SP/SPL, then R8/R8D/R8W/R8B to R15/R15D/R15W/R15B. Note that for some registers you can't access bits 8 to 15 on it's own (there is no SIH, DIH, BPH, SPH or R8H to R15H), and only 64-bit code can use some of these (SIL, DIL, BPL, SPL, and all variations of R8 to R15).

In addition, RFLAGS is the 64-bit version of EFLAGS (which is the 32-bit version of FLAGS); and RIP is the 64-bit version of EIP (which is the 32-bit version of IP). Writing to EIP (e.g. with a 32-bit JMP) clears the highest 32 bits of RIP, and writing to IP (e.g. a 16-bit JMP) also clears the highest 16 bits of EIP (and the highest 48 bits of RIP). Writing to FLAGS doesn't effect the highest 16-bits of EFLAGS. The highest 32 bits of RFLAGS can't be modified and are always zero (as they're not used for anything).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply