Page 1 of 2

Got almost everything working except this thing.

Posted: Thu Sep 29, 2005 10:40 pm
by Jens
Hi!

I've just started doing my own bootloader and kernel but I am stuck with a probably simple thing but I can not get it to work.

I am able to load the kernel but I cant seem to get arrays to work. What is the problem?

Im using WinXP with Mingw gcc, ld and Nasm.

Re:Got almost everything working except this thing.

Posted: Thu Sep 29, 2005 10:48 pm
by Colonel Kernel
Jens wrote:What is the problem?
Are you asking us to guess?

(Hint: Try at least providing some code and error messages...)

Re:Got almost everything working except this thing.

Posted: Thu Sep 29, 2005 11:52 pm
by Jens
I think the problem might have something to do with my compiling commands or perhaps that I have forgot to setup some register or something... i have shorten some code so that it could be posted here.

==== BOOT.ASM ====

[bits 16]
[org 0x7c00]

jmp boot ; jump over the data to our code

;-----------------------Data-----------------------;
;------------GDT Table---------------;
;----------End GDT Table-------------;

;------------Variables---------------;
;----------End Variables-------------;

;------------Functions---------------;
;; 'wait keyboard to clear' function ;;
wkc:
;; 'wait keyboard to be full' function ;;
wkf:
;; 'halt on error' function ;;
halt:
;----------End Functions-------------;
;---------------------End Data---------------------;
boot:
mov [drive],dl ; save boot drive number(0x00=floppy 0x80=hard drive)

mov ax,cs ; setup ds segment
mov ds,ax
mov es,ax
mov fs,ax
mov ax,0x1D0 ; stack is at 0x1D00
mov ss,ax ; align stack also
mov sp,0x200 ; 512 byte stack

mov ax,0xb800 ; setup video segment
mov gs,ax

jmp init ; Some BIOSes jump to 0x7c0:0x0 rather than 0x0:0x7c0

init:

==== A20 ====

sti

==== LOAD KERNEL ====

; move GDT to 0x500
xor ax,ax
mov ds,ax
mov es,ax
mov si,GDT ; Move From [DS:SI]
mov di,[GDTbase] ; Move to [ES:DI]
mov cx,[GDTsize] ; size of GDT
cld ; Clear the Direction Flag
rep movsb ; Move it

cli
;enter pmode
mov eax,cr0
or al,1
mov cr0,eax

;load gdt
lgdt[GDTR]

;clear cs/ip/eip
jmp CODESEL:FLUSH ; set cs to CODESEL

[bits 32]
FLUSH:

;refresh all segment registers
mov eax,DATASEL
mov ds,eax
mov es,eax
mov fs,eax
mov gs,eax
mov ss,eax
mov esp,0xffff

;jump to kernel
jmp CODESEL:0x100000

hlt

TIMES 510-($-$$) DB 0
SIGNATURE DW 0xAA55

==== LOAD.ASM ====

[bits 32] ; 32bit code here
[global start] ; start is a global function
[extern _k_main] ; this is the kernel function

start:

call _k_main

jmp $

==== MAIN.C ====

void k_main() {

while(1);

}

==== COMPILE COMMANDS ====

nasm -f bin boot.asm -o boot.bin
nasm -f win32 load.asm -o load.o
C:\Program\CodeBlocks\bin\gcc -ffreestanding -c main.c -o main.o
C:\Program\CodeBlocks\bin\ld -T link.ld -o kernel.o load.o main.o
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
partcopy boot.bin 0 200 hd10meg.img
partcopy kernel.bin 0 400 hd10meg.img 200

Re:Got almost everything working except this thing.

Posted: Fri Sep 30, 2005 12:10 am
by Jens
SORRY!!! Arrays work, but pointers dont... this main.c doesnt work for instance:

Code: Select all

void print(char message[])
{
  unsigned char *vidmem = (unsigned char *)0xB8000;
  *vidmem = message[0];
}

void k_main()
{
  char *myString;
  myString = "hejsan";

  print(myString);

  while(1);
}

Re:Got almost everything working except this thing.

Posted: Fri Sep 30, 2005 1:35 am
by Colonel Kernel
Jens wrote:

Code: Select all

void print(char message[])
{
  unsigned char *vidmem = (unsigned char *)0xB8000;
  *vidmem = message[0];
}
Your code would be easier to read if you put code tags around it.

Video memory in text mode is actually an interleaving of character bytes and attribute bytes. Here is an example from the FAQ of what you're trying to do:

Code: Select all

 /* note this example will always write to the top
           line of the screen */
        void write_string(int colour, char *string)
        {
                char *video=(char*)0xB8000;
                while(*string!=0)
                {
                        *video=*string;
                        string++;
                        video++;
                        *video=colour;
                        video++;
                }
        }
You're probably drawing a black character on a black background, which is why you can't see it.

Re:Got almost everything working except this thing.

Posted: Fri Sep 30, 2005 3:35 am
by Jens
Thanks alot for the reply! The print function you gave me was alot better than the one i had, thanx you!

However the problem still exist... if I define the string as

char *myString = {'h','i',0};

i can print(myString); but not if i define it as

char *myString = "hi";

Well... ill probably figure it out eventually...

Re:Got almost everything working except this thing.

Posted: Fri Sep 30, 2005 3:37 am
by Jens
Another oops! The line:

char *myString = {...};

was ment to be:

char myString[3] = {...};

Re:Got almost everything working except this thing.

Posted: Fri Sep 30, 2005 3:38 am
by AR
The .rodata section. Add "*(.rodata*)" to .text in the link script.

Re:Got almost everything working except this thing.

Posted: Sun Oct 02, 2005 1:49 pm
by Jens
That didnt help. This is my linker script:

ENTRY(start)
SECTIONS
{
.text 0x00100000 : {
*(.text)
*(.rodata*)
}
.data : {
*(.data)
}
.bss : {                
*(.bss)
}
}

Re:Got almost everything working except this thing.

Posted: Sun Oct 02, 2005 1:57 pm
by Jens
I have also tried *(.rodata) and also making it its own section... nothing works...

Re:Got almost everything working except this thing.

Posted: Sun Oct 02, 2005 2:15 pm
by Jens
Solved it... the compiler named it .rdata and not .rodata... now it works! yes!

Re:Got almost everything working except this thing.

Posted: Mon Oct 03, 2005 1:58 am
by Pype.Clicker
just out of curiosity, what compiler is that ? (version, distro, etc. "gcc --version" should tell this all)

Re:Got almost everything working except this thing.

Posted: Mon Oct 03, 2005 3:01 am
by Candy
Pype.Clicker wrote: just out of curiosity, what compiler is that ? (version, distro, etc. "gcc --version" should tell this all)
I somehow doubt that "gcc --version" will help, since I also doubt that he's using gcc.

Re:Got almost everything working except this thing.

Posted: Mon Oct 03, 2005 5:24 am
by AR
Candy wrote:
Pype.Clicker wrote: just out of curiosity, what compiler is that ? (version, distro, etc. "gcc --version" should tell this all)
I somehow doubt that "gcc --version" will help, since I also doubt that he's using gcc.
Jens wrote:Im using WinXP with Mingw gcc, ld and Nasm.
Either the MinGW people changed it because they felt like it (unlikely), or COFF/PE calls it "rdata" and it was changed to match.

Re:Got almost everything working except this thing.

Posted: Mon Oct 03, 2005 5:59 am
by Candy
AR wrote:
Candy wrote:
Pype.Clicker wrote: just out of curiosity, what compiler is that ? (version, distro, etc. "gcc --version" should tell this all)
I somehow doubt that "gcc --version" will help, since I also doubt that he's using gcc.
Jens wrote:Im using WinXP with Mingw gcc, ld and Nasm.
Either the MinGW people changed it because they felt like it (unlikely), or COFF/PE calls it "rdata" and it was changed to match.
Remind me not to post before drinking at least one +2 coffee (yes, our coffee machine actually calls it a +2 coffee if you press extra strong twice).