Page 2 of 3

Posted: Tue Jun 26, 2007 7:24 am
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.

Posted: Tue Jun 26, 2007 9:03 am
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

Posted: Tue Jun 26, 2007 10:30 am
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 

Posted: Tue Jun 26, 2007 10:56 am
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.

Posted: Tue Jun 26, 2007 3:17 pm
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.

Posted: Tue Jun 26, 2007 3:19 pm
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 :)

just write a character

Posted: Tue Jun 26, 2007 5:26 pm
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.

Posted: Tue Jun 26, 2007 5:34 pm
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

Posted: Tue Jun 26, 2007 5:49 pm
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

Posted: Tue Jun 26, 2007 7:28 pm
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!

Posted: Wed Jun 27, 2007 1:01 am
by os64dev
it can be handy to put data and rodata in the linker script

Posted: Wed Jun 27, 2007 2:18 am
by Combuster
... and .bss ...

Posted: Wed Jun 27, 2007 3:00 am
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

Posted: Wed Jun 27, 2007 5:14 am
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?

Posted: Wed Jun 27, 2007 5:26 am
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?