Kernel using C

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
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

I only need two words to describe what this is about:
Voodoo Programming

Next, define the gdt offset as a doubleword.

After that, get away from it as its a ticking timebomb.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

hmm this is pretty interesting... or I might have missed something

Code: Select all

[BITS 16]
[global start]
[extern _k_main]
start:

   xor ax, ax   ; make it zero
   mov ds, ax   ; DS=0
   mov es, ax
   cli      ; no interrupt

   lgdt [gdtinfo]   ; load gdt register

   mov  eax, cr0   ; switch to pmode by
   or al,1         ; set pmode bit
   mov  cr0, eax

   mov  bx,10000b
   mov  ds, bx ; set data buffer to ds
   mov ss, bx
   mov esp,0x9200

   jmp 08h:temp
[bits 32]
temp:
   call _k_main
   jmp $

gdt        dd 0,0  ; entry 0 is always unused
code    db 0xff, 0xff, 0, 0, 0, 10011010b, 11000000b, 0 ; code buffer
datastack    db 0xff, 0xff, 0, 0, 0, 10010010b, 11001111b, 0                     ; data buffer
gdt_end:

gdtinfo:
   dw gdt_end - gdt - 1
   dw gdt

man:
   db 'w'
this this doesnt work

but when i type

Code: Select all

man:
   db 0
it works

also

Code: Select all

gdtinfo:
   dw gdt_end - gdt - 1
   dw gdt,0

man:
   db 'w'
this works
Regards


Nitin
Aali
Member
Member
Posts: 58
Joined: Sat Apr 14, 2007 12:13 pm

Post by Aali »

its quite simple, the GDT address is 32 bits, not 16

try:

Code: Select all

gdtinfo:
   dw gdt_end - gdt - 1
   dd gdt 
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post by mathematician »

The boot sector should really be used to load another file from disk; either the kernel or the second stage of a chain loader. By calling _k_main from the boot sector you might simply be transgressing the 512 byte limit, especially as there is no way of easily calculating how many bytes a C procedure will add to the end of the asm code.
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

mathematician wrote:The boot sector should really be used to load another file from disk; either the kernel or the second stage of a chain loader. By calling _k_main from the boot sector you might simply be transgressing the 512 byte limit, especially as there is no way of easily calculating how many bytes a C procedure will add to the end of the asm code.
I totally aggree with you. I will be doing the same thing. Basically what I wanted was to compile a binary(flat binary) file using C programming.
Regards


Nitin
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

Aali wrote:its quite simple, the GDT address is 32 bits, not 16

try:

Code: Select all

gdtinfo:
   dw gdt_end - gdt - 1
   dd gdt 
Sorry!!! Thats what I must tell. I was refering a bad tutorial. Thanks for all the help :)
Regards


Nitin
com1
Member
Member
Posts: 105
Joined: Sat Apr 28, 2007 11:57 am
Location: TN

just write a character

Post by com1 »

If you just want to write a character string, you have to get in touch with video memory. My shell is designed to run as an app, so i enable printf functions.
oh microsoft, microsoft, what souls you have dismayed
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post by t0xic »

iirc, you can't call other functions from a binary unless they are inside of the binary, so your boot loader would not have even worked if _k_main was small enough.

Try looking at my bootloader I used before switching to grub (attached)

An example second stage you could use would be this: (save as loader.asm and compile in nasm)

Code: Select all

bits 32
global _start
extern _k_main

_start:
  call _k_main
  cli
  hlt
--Michael
Attachments
fat12boot.asm
Fat12 Compliant Bootloader
(4.14 KiB) Downloaded 23 times
exkor
Member
Member
Posts: 111
Joined: Wed May 23, 2007 9:38 pm

Post by exkor »

nitinjavakid wrote:

Code: Select all

   mov  eax, cr0
   or al,1
   mov  cr0, eax
   mov  bx,10000b
   mov  ds, bx ; [b]if i dont put jmp $ before this then bochs restarts :([/b]
   mov ss, bx
   mov esp,0x9200
   jmp 08h:temp
[BITS 32]
[extern _k_main]
temp:
   call _k_main
   jmp $
standart technique is to set up segment register after you jump into PM, I dont know who it works with C/C++ combination though

mov eax, cr0
or al,1
mov cr0, eax
jmp 08h:temp
[BITS 32]
[extern _k_main]
temp:
mov bx,10000b
mov ds, bx
mov ss, bx
mov esp,0x9200
call _k_main ;maybe try a simple jump here
jmp
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

This time I am trying to load the file into 0x0000:0x9000 and then the code in 0x9000 will move it to PMode, however there seems to be a memory problem which I checked by printing check: It prints '\0' :roll:

temp.asm

Code: Select all

[BITS 16]
[global start]
;[extern _k_main]
start:

   xor ax, ax   ; make it zero
   mov ds, ax   ; DS=0
   mov es, ax   ; ES=0
  
;; here is the loader code
 
	mov	ax,0x0900
	mov	es,ax			
	mov	bx,0			

	mov	dl,0
	mov	dh,0
	mov	cl,2
	mov	ch,0

	mov	ah,2			
	mov	al,1			
				
	int	013h			
 
       
;;loader code ends here now moving to protected mode :)
jmp 0x0000:0x9000
   

times 510-($-$$) db 0
db 0x55
db 0xAA
kernel.asm

Code: Select all

[bits 16]
   ;cli      ; no interrupt
   
   mov ax,0xb800
   mov es,ax
   mov al, [check]
   mov [es:0000],al
   jmp $
   lgdt [gdtinfo]   ; load gdt register

   mov  eax, cr0   ; switch to pmode by
   or al,1         ; set pmode bit
   mov  cr0, eax
   
   jmp 08h:temp

[bits 32]
temp:
   mov  bx,10000b 
   mov  ds, bx ; set data buffer to ds

   mov ss, bx
   mov esp,0x9200
   mov al,'w'
   mov [ds:0xb8000],al
   jmp $

gdt        dd 0,0  ; entry 0 is always unused
code    db 0xff, 0xff, 0, 0, 0, 10011010b, 11000000b, 0 ; code buffer
datastack    db 0xff, 0xff, 0, 0, 0, 10010010b, 11001111b, 0                     ; data buffer
gdt_end:

gdtinfo:
   dw gdt_end - gdt - 1
   dd gdt

check:
   db 'q'

times 512-($-$$) db 0
link.ld

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
	. = 0x7c00;
	.text : {ks.o(.text)}	
	. = 0x9000;
	.text : {kernel.o(.text)}
}
Please help!
Regards


Nitin
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

it can be handy to put data and rodata in the linker script
Author of COBOS
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

... and .bss ...
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
exkor
Member
Member
Posts: 111
Joined: Wed May 23, 2007 9:38 pm

Post by exkor »

does this work?
mov ax, 0404h
mov [ds:0b8000h], ax ;red diamond on screen

do you have/need this?
mov ax, 3 ;textmode
int 10h

whats is '\0'? one symbol? 0h? 30h? a space?

symbols in text mode take 2 bytes by the way
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

exkor wrote:does this work?
mov ax, 0404h
mov [ds:0b8000h], ax ;red diamond on screen

do you have/need this?
mov ax, 3 ;textmode
int 10h

whats is '\0'? one symbol? 0h? 30h? a space?

symbols in text mode take 2 bytes by the way
mov ax, 0404h
mov [ds:0b8000h], ax ;red diamond on screen
This thing works. Only when I refer mov ax,[check] . It doesnt work properly, ie. wrong data is printed. Obviously, . = 0x9000 isnt working or I am doing something wrong.

Also, plz tell me how .bss .data and .rodata would help?
Regards


Nitin
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

nitinjavakid wrote:Also, plz tell me how .bss .data and .rodata would help?
Uh, the answer is a few posts back? Back where I told you what happens when no .data and .bss were present?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply