Page 1 of 1

Make 32 Bit Kernel

Posted: Thu May 28, 2009 2:58 pm
by Cjreek
Hi,

I've to admit, that I'm a real beginner in OS development. I startet yesterday. My bootloader works like it is supposed to, but I'd like to have a 32-Bit kernel.

Do I have to switch to protected mode before I can do anything else?

Well, Here's my Code. If I use [Bits 32] my cls and print procedure don't work anymore.

Code: Select all

; boot.asm
; this code only works with [BITS 16] !
[BITS 32]    
   	mov eax, 0x07C0
   	mov ds, eax
   	
   	call cls
   	mov esi, msg
   	call print
 
kernel: 
    jmp kernel
	
cls: 
    mov cx, 2000
    mov ax, 0x0420
    mov dx, 0xB800
    mov es, dx
    rep stosw            ; clear video buffer
    
    mov ah, 0x02
    xor dx, dx
    xor bh, bh
    int 10h              ; reset cursor pos
   
    ret
    
print:
    cld
    mov edx, 0xB800
    mov es, dx
     
  .ploop:    
    lodsb
    or al, al
    jz .done
    mov ah, 0x02   
    mov di, [xpos]   
    stosw                    ; write char at xpos using flags specified in ah
    
    add byte [xpos], 2  ; inc xpos
    jmp .ploop       
  .done:
    ret

msg	 db "Welcome to my OS!", 0
xpos   db 0
         times 510-($-$$) db 0
   	 db 0x55
   	 db 0xAA
Cjreek

Re: Make 32 Bit Kernel

Posted: Thu May 28, 2009 3:10 pm
by imate900
You need to switch into protected mode.

Otherwise, fixed cls and print:

Code: Select all

cls:
    mov cx, 2000
    mov ax, 0x0420
    mov dx, 0xB800
    mov es, dx
    rep stosw            ; clear video buffer
   
    ret
   
print:
    cld
    mov edi, 0xB800
    mov es, dx
     
  .ploop:   
    lodsb
    or al, al
    jz .done
    mov ah, 0x02   
    mov edi, [xpos]   
    stosw                    ; write char at xpos using flags specified in ah
   
    add byte [xpos], 2  ; inc xpos
    jmp .ploop       
  .done:
    ret

Re: Make 32 Bit Kernel

Posted: Thu May 28, 2009 3:35 pm
by Cjreek
Hi,

Ok thanks. I changed my code, but it does not work yet.

And this part:

Code: Select all

print:
    cld
    mov edi, 0xB800
    mov es, dx           <--- this should be edi instead of dx?
But neither cls nor print works :/

Re: Make 32 Bit Kernel

Posted: Thu May 28, 2009 4:24 pm
by neon
Rather then taking code without understanding it, you should read this.

You do need to go into protected mode, but you have not performed the steps to create the environment (gdt/ints) nor switch the processor into protected mode. Just adding a [Bits 32] doesnt do anything related to protected mode.

Re: Make 32 Bit Kernel

Posted: Thu May 28, 2009 4:30 pm
by Combuster
@imate: I find it very disturbing that over half the time, you post incorrect things and worse, assert those as being truth.

@Cjreek: Please read the Babystep tutorial.

Re: Make 32 Bit Kernel

Posted: Thu May 28, 2009 4:39 pm
by Cjreek
neon wrote:Rather then taking code without understanding it, you should read this.

You do need to go into protected mode, but you have not performed the steps to create the environment (gdt/ints) nor switch the processor into protected mode. Just adding a [Bits 32] doesnt do anything related to protected mode.
Hi,

Sorry, I'm not that kind of guy who takes code without understanding it ;) I admit, that I used some code from the wiki as a base. But I spend half an hour understanding every single line of code. I could tell you something about every line :wink:

And it seems that there's a misunderstanding. I know that I've done nothing to enter protected mode. That's not my goal (not yet).

My question just was how to make my 16 Bit print and cls code running with [BITS 32] or whether it's necessary to switch to protected mode in order to run 32 Bit code.

And as far as I understood imate900 I can choose between running THIS code in protected mode OR change my code.
And changing my code the way imate900 suggested did not lead to success.

@Combuster: I've read the babystep tutorial til Babystep 4

Re: Make 32 Bit Kernel

Posted: Thu May 28, 2009 5:33 pm
by Troy Martin
And changing my code the way imate900 suggested did not lead to success.
And what have we learned from this? :P Just kidding.

@imate: Understand what you're talking about before you say it. If I were you, I'd be doing so or expecting a ban soon enough.

Re: Make 32 Bit Kernel

Posted: Thu May 28, 2009 5:40 pm
by JamesM
Hi,
Cjreek wrote:
neon wrote:Rather then taking code without understanding it, you should read this.

You do need to go into protected mode, but you have not performed the steps to create the environment (gdt/ints) nor switch the processor into protected mode. Just adding a [Bits 32] doesnt do anything related to protected mode.
Hi,

Sorry, I'm not that kind of guy who takes code without understanding it ;) I admit, that I used some code from the wiki as a base. But I spend half an hour understanding every single line of code. I could tell you something about every line :wink:

And it seems that there's a misunderstanding. I know that I've done nothing to enter protected mode. That's not my goal (not yet).

My question just was how to make my 16 Bit print and cls code running with [BITS 32] or whether it's necessary to switch to protected mode in order to run 32 Bit code.

And as far as I understood imate900 I can choose between running THIS code in protected mode OR change my code.
And changing my code the way imate900 suggested did not lead to success.

@Combuster: I've read the babystep tutorial til Babystep 4
Apologies if Combuster sounded a little aggressive - we get people who copy-paste without reading in around the ratio 50-1 with people who actually attempt to understand.

There is a mode where you can run 32-bit code without protected mode. It's called "unreal mode" - there should be a wiki article on it. Other than that, if you want to run 32-bit code you'll have to switch to protected mode, which means setting up a GDT and setting the CR0.PE bit. Again, there's plenty about that on the wiki.

Cheers,

James

Re: Make 32 Bit Kernel

Posted: Thu May 28, 2009 5:49 pm
by Troy Martin
JamesM wrote:There is a mode where you can run 32-bit code without protected mode. It's called "unreal mode" - there should be a wiki article on it.
Umm, last time I checked, unreal mode was just a register trick to use the GDT's selectors in real mode.

Re: Make 32 Bit Kernel

Posted: Thu May 28, 2009 5:59 pm
by JamesM
Troy Martin wrote:
JamesM wrote:There is a mode where you can run 32-bit code without protected mode. It's called "unreal mode" - there should be a wiki article on it.
Umm, last time I checked, unreal mode was just a register trick to use the GDT's selectors in real mode.
Quite right. Lesson learned - don't post at 00:59 am.

Re: Make 32 Bit Kernel

Posted: Thu May 28, 2009 6:53 pm
by kop99
Cjreek,
Unless you are going to make the real mode kernel, First thing you have to do is Protected Mode entrance...

Re: Make 32 Bit Kernel

Posted: Thu May 28, 2009 11:45 pm
by Cjreek
Hi,
JamesM wrote: Apologies if Combuster sounded a little aggressive - we get people who copy-paste without reading in around the ratio 50-1 with people who actually attempt to understand.
I think this sounds familiar with me :mrgreen: I'm registered in another forum and there it's the same. Many people (mostly new members) "code" their programs using copy&paste :roll:
kop99 wrote:Cjreek,
Unless you are going to make the real mode kernel, First thing you have to do is Protected Mode entrance...
This is what I wanted to hear :)

But prepare... For Sure there will be another noob question soon :twisted: :P
(Of course after reading every possible tutorial :mrgreen: )