initializing

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.
mobruan
Member
Member
Posts: 71
Joined: Thu Oct 09, 2008 8:25 am
Location: Rio de Janeiro - Brazil

initializing

Post by mobruan »

Hi, im using the following bootstrap for initialization, but the computer resets...any ideias?

Code: Select all

org 0x07c00

jmp start

;**********
;gdt
;**********

bits 16

gdt_addr:
lim dw 23
base dd gdt

gdt:

null dd 0
null2 dd 0

c_limit dw 0xffff
c_base dw 0x0000
c_base2 db 0x00
c_type db 0x98
c_limit2 db 0xcf
c_base3 db 0x00

d_limit dw 0xffff
d_base dw 0x0000
d_base2 db 0x00
d_type db 0x92
d_limit2 db 0xcf
d_base3 db 0x00

end_gdt:

start:
;*********
;PE Mode
;*********

cli
lgdt [gdt_addr]
mov eax, cr0
or eax, 0x01
mov cr0, eax
jmp 0x08:code


;**********
;32-bit code
;**********
bits 32

code:

mov ax, word 0x10    
mov ds, word ax

spin:
jmp spin


TIMES   510-($-$$) DB 0
DW      0xAA55
thanks,
Ângelo
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: initializing

Post by 01000101 »

There are a few things you've left out.

You don't set up a stack, init your segment registers (before or after the jump to PMode), align the GDT, or ensure that the BIOS didn't start at 0x07C0:0x000 or 0x0:0x7C00 or any other variation.

I'd also imagine you may have some GDT descriptor issues as well.

Also, next time (or even this time), include a bit more information as it makes it a lot easier for us to help. Do you use BOCHS or any other emulator/virtualization with debugging features? If so, where does it fault, what's in the registers, examine these things and you may find your answer.
mobruan
Member
Member
Posts: 71
Joined: Thu Oct 09, 2008 8:25 am
Location: Rio de Janeiro - Brazil

Re: initializing

Post by mobruan »

Hi, im trying on my computer, i used it on bochs some time ago and i remember it worked.
I didnt get the bios address thing...
thanks
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: initializing

Post by geppyfx »

Code seems to work fine when I converted it to Fasm (bochs & code2duo).
But I dont fully understand why. First 4 bits of 0x98 constant means its code section for execution. Try chaging it to 9A: code + execute + read

And dont forget to display something on the screen so like "mov word [0b8000h], 0404h" so youknow it does(not) work.

Edit: a bird just told me that Read is required when you keep static data such as constants in the code section and reference them. I guess it would count when you have 4GB data & code starting at the same address.
Last edited by geppyfx on Sat May 02, 2009 12:55 pm, edited 1 time in total.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: initializing

Post by Troy Martin »

Code: Select all

mov ax, word 0x10   
mov ds, word ax
Is wrong. It should be:

Code: Select all

mov ax, 0x10   
mov ds, ax
mov es, ax
And you should set up a stack using the same selector and a nice offset like 0x9000 or something.

EDIT: What assembler are you using? NASM?
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: initializing

Post by geppyfx »

Troy Martin wrote:

Code: Select all

mov ax, word 0x10   
mov ds, word ax
Is wrong. It should be:

Code: Select all

mov ax, 0x10   
mov ds, ax
mov es, ax
None of his instructions uses ES.
Odd but even fasm allows "mov ds, word ax ". Well same sizes.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: initializing

Post by Troy Martin »

ES should be initialized anyways.

It's probably the GDT. Try putting "align 4" before it.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
mobruan
Member
Member
Posts: 71
Joined: Thu Oct 09, 2008 8:25 am
Location: Rio de Janeiro - Brazil

Re: initializing

Post by mobruan »

Hi, im using nasm. The code so far is like this.

Code: Select all

org 0x7c00

jmp start

;**********
;gdt
;**********

bits 16
align 4

gdt_addr:
lim dw 23
base dd gdt

gdt:

null dd 0
null2 dd 0

c_limit dw 0xffff
c_base dw 0x0000
c_base2 db 0x00
c_type db 0x9a
c_limit2 db 0xcf
c_base3 db 0x00

d_limit dw 0xffff
d_base dw 0x0000
d_base2 db 0x00
d_type db 0x92
d_limit2 db 0xcf
d_base3 db 0x00

end_gdt:

start:
;*********
;PE Mode
;*********

cli
lgdt [gdt_addr]
mov eax, cr0
or eax, 0x01
mov cr0, eax
jmp 0x08:code


;**********
;32-bit code
;**********
bits 32

code:

mov ax, word 0x10    
mov ds, word ax
mov ss, word ax
mov es, word ax

spin:
jmp spin


TIMES   510-($-$$) DB 0
DW      0xAA55
mobruan
Member
Member
Posts: 71
Joined: Thu Oct 09, 2008 8:25 am
Location: Rio de Janeiro - Brazil

Re: initializing

Post by mobruan »

"Im going throw a walk, nothing one the dinner kind..." - Bad Religion
mobruan
Member
Member
Posts: 71
Joined: Thu Oct 09, 2008 8:25 am
Location: Rio de Janeiro - Brazil

Re: initializing

Post by mobruan »

Hi, i dont know what to do...any ideias? the code dont work...
Thanks,
Ângelo
kay10
Member
Member
Posts: 30
Joined: Mon Apr 13, 2009 6:10 am
Location: Oldenburg, Germany

Re: initializing

Post by kay10 »

Umm... How do you know that it doesn't work, if you don't get any output?
I compiled your code with NASM and tested it with bochs. For me, it works well.
At first I was confused because I don't saw any difference in bochs after running your code,
but then I recognized the infinite loop that you built in at the end of the code without printing anything on the screen.
Print something on the screen so you'll see that it should work.
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: initializing

Post by geppyfx »

I have compiled you last source with nasm and written it at the begining of the floppy (you can use usb stick). Works (along with the code in you first post). Image attached.

Show some proof that it doesn't work such as bochs debugging info.
Attachments
boot.zip
(201 Bytes) Downloaded 84 times
mobruan
Member
Member
Posts: 71
Joined: Thu Oct 09, 2008 8:25 am
Location: Rio de Janeiro - Brazil

Re: initializing

Post by mobruan »

This is the problem...in bochs it work but on my machine it dont...im thinking if it not the a20 line...its possible?
i put this output(mov [0xb8000], word 0x0404) before the loop and it didnt appear on the screen before the computer reset...
Any ideias?
Thanks,
Ângelo
kay10
Member
Member
Posts: 30
Joined: Mon Apr 13, 2009 6:10 am
Location: Oldenburg, Germany

Re: initializing

Post by kay10 »

Ah, I hate it if emulators don't act like my real pc, feels like doing something for nothing.
I think you test your code with a floppy, don't you?
Maybe your bios requires a table (forgot the special name for it :mrgreen: ) like the following one:

Code: Select all

jmp start
OSName			db 'OS      '
BytesPerSec		dw 512
SecPerClus		db 1
RsvdSecCnt		dw 2
NumFATs			db 2
RootEntCnt		dw 224
TotSec			dw 2880
MediaType		db 0xF0
FATSize			dw 9
SecPerTrack		dw 18
NumHeads		dw 2
HiddenSec		dd 0
TotSec32		dd 0
DrvNum			db 0x00
Reserved		db 0
BootSig			db 0x29
VolumeID		dd 00000000h
VolumeLabel		db 'NO NAME    '
FileSysType		db 'FAT12   '
Put it at the top of your bootstrap and look if anything has changed on your real pc.
I'm not sure whether (older?) bioses need this piece of code in a bootloader.
I don't think the A20 line is the cause of it because you never try to write something above the 1MB limit.
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: initializing

Post by geppyfx »

You may wanna disable NMI(Non Masked Interrupts) for now
add this anywhere before entering Protected Mode

Code: Select all

  in   al, 70h
  or   al, 80h
  out  70h, al   
I personaly like having another "cli" right after you jumped to protected mode, very first instruction.
Post Reply