Got almost everything working except this thing.

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.
Jens

Got almost everything working except this thing.

Post 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.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:Got almost everything working except this thing.

Post 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...)
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
Jens

Re:Got almost everything working except this thing.

Post 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
Jens

Re:Got almost everything working except this thing.

Post 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);
}
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:Got almost everything working except this thing.

Post 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.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
Jens

Re:Got almost everything working except this thing.

Post 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...
Jens

Re:Got almost everything working except this thing.

Post by Jens »

Another oops! The line:

char *myString = {...};

was ment to be:

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

Re:Got almost everything working except this thing.

Post by AR »

The .rodata section. Add "*(.rodata*)" to .text in the link script.
Jens

Re:Got almost everything working except this thing.

Post by Jens »

That didnt help. This is my linker script:

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

Re:Got almost everything working except this thing.

Post by Jens »

I have also tried *(.rodata) and also making it its own section... nothing works...
Jens

Re:Got almost everything working except this thing.

Post by Jens »

Solved it... the compiler named it .rdata and not .rodata... now it works! yes!
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Got almost everything working except this thing.

Post by Pype.Clicker »

just out of curiosity, what compiler is that ? (version, distro, etc. "gcc --version" should tell this all)
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Got almost everything working except this thing.

Post 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.
AR

Re:Got almost everything working except this thing.

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Got almost everything working except this thing.

Post 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).
Post Reply