Kernel using C
- nitinjavakid
- Member
- Posts: 65
- Joined: Sat Oct 21, 2006 11:28 am
- Location: Exams over!
- Contact:
hmm this is pretty interesting... or I might have missed something
this this doesnt work
but when i type
it works
also
this works
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'
but when i type
Code: Select all
man:
db 0
also
Code: Select all
gdtinfo:
dw gdt_end - gdt - 1
dw gdt,0
man:
db 'w'
Regards
Nitin
Nitin
its quite simple, the GDT address is 32 bits, not 16
try:
try:
Code: Select all
gdtinfo:
dw gdt_end - gdt - 1
dd gdt
- mathematician
- Member
- Posts: 437
- Joined: Fri Dec 15, 2006 5:26 pm
- Location: Church Stretton Uk
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.
- nitinjavakid
- Member
- Posts: 65
- Joined: Sat Oct 21, 2006 11:28 am
- Location: Exams over!
- Contact:
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.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.
Regards
Nitin
Nitin
- nitinjavakid
- Member
- Posts: 65
- Joined: Sat Oct 21, 2006 11:28 am
- Location: Exams over!
- Contact:
Sorry!!! Thats what I must tell. I was refering a bad tutorial. Thanks for all the helpAali wrote:its quite simple, the GDT address is 32 bits, not 16
try:
Code: Select all
gdtinfo: dw gdt_end - gdt - 1 dd gdt
![Smile :)](./images/smilies/icon_smile.gif)
Regards
Nitin
Nitin
just write a character
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
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)
--Michael
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
- Attachments
-
- fat12boot.asm
- Fat12 Compliant Bootloader
- (4.14 KiB) Downloaded 22 times
standart technique is to set up segment register after you jump into PM, I dont know who it works with C/C++ combination thoughnitinjavakid 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 $
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
- nitinjavakid
- Member
- Posts: 65
- Joined: Sat Oct 21, 2006 11:28 am
- Location: Exams over!
- Contact:
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' ![Rolling Eyes :roll:](./images/smilies/icon_rolleyes.gif)
temp.asm
kernel.asm
link.ld
Please help!
![Rolling Eyes :roll:](./images/smilies/icon_rolleyes.gif)
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
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
Code: Select all
OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
. = 0x7c00;
.text : {ks.o(.text)}
. = 0x9000;
.text : {kernel.o(.text)}
}
Regards
Nitin
Nitin
- nitinjavakid
- Member
- Posts: 65
- Joined: Sat Oct 21, 2006 11:28 am
- Location: Exams over!
- Contact:
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
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.mov ax, 0404h
mov [ds:0b8000h], ax ;red diamond on screen
Also, plz tell me how .bss .data and .rodata would help?
Regards
Nitin
Nitin