C os programming

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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C os programming

Post by Pype.Clicker »

SpiglerG wrote: The code is attached.
That can't work. period.

You need to switch to protected mode before you run code generated by GCC (be it from dev-cpp, linux, cygwin, djgpp or whatever).
SpiglerG

Re:C os programming

Post by SpiglerG »

In fact i asked you all if you can help me modify the start.asm file to jump protected mode.
beyondsociety

Re:C os programming

Post by beyondsociety »

so, could you help me? i need someone to correct and update the code in the .zip i posted. i know a simple method to switch to protected mode and load a gdt, but if someone could post some code, i'll be very happy.
I suggest taking a look at this link. It doesnt contain code, but it will tell you actually what you need to setup protected mode.Click here.
SpiglerG

Re:C os programming

Post by SpiglerG »

I followed some tutorials and i think i can jump to protected mode. i wonder only if you could help me with some code, because it will be faster.
Visitor..

Re:C os programming

Post by Visitor.. »

SpiglerG wrote: i wonder only if you could help me with some code, because it will be faster.
You'll need a lot of patience when developing an OS/kernel. You could just as well start getting used to it now.
beyondsociety

Re:C os programming

Post by beyondsociety »

i wonder only if you could help me with some code, because it will be faster.
It may be faster for you if we give code, but you will not learn anything from it. If you have problems with code you have tried to write to enable protected mode, we will be glad to help. We are not here to write your os for you, there are tons of tutorials on the net showing how to enable protected mode. Everything you need to enable protected mode is found in the link I gave you. This is what I used and I only had minor problems in getting my code to work. If you dont want to write your own code, use a premade bootloader such as Grub or download a premade protected mode bootloader found by searching the net. ::)

Have you taken a look at http://www.osdever.net/ or http://my.execpc.com/CE/AC/geezer/os/
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C os programming

Post by Solar »

Although I suspect the post above is __pissed trying another nick, he's right: This is a do-it-yourself forum, with a stress being put on yourself. What you're doing here is asking other people to write an OS loader for you. Not nice.

Besides, such a thing already exists, and it's called GRUB. See the FAQ for details.
Every good solution is obvious once you've found it.
SpiglerG

Re:C os programming

Post by SpiglerG »

Yes, i know. here is the start.asm code i tryied:
[BITS 16]

[SEGMENT .text]

start:
mov ax, 0x0100 ;location where kernel is loaded
mov ds, ax
mov es, ax

cli
mov ss, ax            ;stack segment
mov sp, 0xFFFF         ;stack pointer at 64k limit
sti



cli ; Disable interrupts, we want to be alone

xor ax, ax
mov ds, ax ; Set DS-register to 0 - used by lgdt

lgdt [gdt_desc] ; Load the GDT descriptor

mov eax, cr0 ; Copy the contents of CR0 into EAX
or eax, 1 ; Set bit 0
mov cr0, eax ; Copy the contents of EAX into CR0

jmp 08h:clear_pipe ; Jump to code segment, offset clear_pipe


[BITS 32]
[global start]
[extern _k_main]

clear_pipe:
mov ax, 10h ; Save data segment identifyer
mov ds, ax ; Move a valid data segment into the data segment register
mov ss, ax ; Move a valid data segment into the stack segment register
mov esp, 090000h ; Move the stack pointer to 090000h

jmp 08h:01000h ; Jump to section 08h (code), offset 01000h



gdt_desc: ; The GDT descriptor
dw gdt_end - gdt - 1 ; Limit (size)
dd gdt ; Address of the GDT



call _k_main

cli ; stop interrupts
hlt ; halt the CPU





[SEGMENT .data]


gdt: ; Address for the GDT

gdt_null: ; Null Segment
dd 0
dd 0

gdt_code: ; Code segment, read/execute, nonconforming
dw 0FFFFh
dw 0
db 0
db 10011010b
db 11001111b
db 0

gdt_data: ; Data segment, read/write, expand down
dw 0FFFFh
dw 0
db 0
db 10010010b
db 11001111b
db 0

gdt_end: ; Used to calculate the size of the GDT


[SEGMENT .bss]
beyondsociety

Re:C os programming

Post by beyondsociety »

here is the start.asm code i tryied:
And what happens when you try it?
SpiglerG

Re:C os programming

Post by SpiglerG »

It displays the boot texts and then it doesn't do anything.
SpiglerG

Re:C os programming

Post by SpiglerG »

i recompiled it(with -ffreestanding...) and it doesn't display the boot texts.
SpiglerG

Re:C os programming

Post by SpiglerG »

when i link it it says:
ld.exe: warning: cannot find entry simbol start; defaulting to 00000100.
I think is not a problem because the bootloader load the kernel at that address.
beyondsociety

Re:C os programming

Post by beyondsociety »

ld.exe: warning: cannot find entry simbol start; defaulting to 00000100. I think is not a problem because the bootloader load the kernel at that address.
In order to use ld, you have to use protected mode code. So 0x00000100 will not work as a load address because it is a read mode address. Try loading it to a higher address.

Also: havent tested your code yet.

EDIT: The warning is because you didnt specify the load address to ld. You can do this by either adding a linker script or hard coding the load address to the linker.
SpiglerG

Re:C os programming

Post by SpiglerG »

So, i need to load it, say, at 0x1000 instead of 0x0100?
If so, i need to modify the bootloader too.
beyondsociety

Re:C os programming

Post by beyondsociety »

So, i need to load it, say, at 0x1000 instead of 0x0100?If so, i need to modify the bootloader too.
It has to be a protected mode address. Most people load their protected mode os at the 1MB mark because the memory area is free. You can load it lower, but there are certain regions below 1MB that are free and others that cannot be touched.

0 - 3FF RAM Real mode, IVT (Interrupt Vector Table)
400 - 4FF RAM BDA (BIOS data area)
500 - 9FFFF RAM Free memory, 7C00 used for boot sector
A0000 - BFFFF Video RAM Video memory
C0000 - C7FFF Video ROM Video BIOS
C8000 - EFFFF ? BIOS shadow area
F0000 - FFFFF ROM System BIOS
FFFFF(1MB) - 4GB = FREE space

Hope this helps.
Post Reply