Page 1 of 1

Problem kernel code

Posted: Fri Oct 03, 2003 11:00 pm
by modshah
Hi I am back....

I have a kernel (right now it does nothing though). All that it does is print a 'S' character and calls a C function. And this calling is within a while(1) loop(infinite). But bochs reports SP==1 error(stack pointer error) after a going thru the loop for some time. This is the code :

start.asm
---------

extern _Dmain
global _Start

[SEGMENT .text]

_Start:
        mov ax, cs
        mov ds, ax
        mov ss, ax
        mov es, ax
        mov sp, 0xFFFF
        call _Dmain
        
kernel.c
--------

void summa();

void Dmain()
{
int a = 20;   //No use vars
char b = 'S'; //No use vars

while(1)
{
  print();
  summa();
}
}

void summa()
{
  //Function does nothing
}


print.asm
----------global _print

[SEGMENT .text]
_print:
        push ebp
        mov ebp, esp
        mov ah,0x0E
        mov al,0x53
        int 0x10
        pop ebp
        ret

link.ld - My linker script
---------------------------
OUTPUT_FORMAT("binary")
ENTRY(_Start)
SECTIONS
{
.text 0x00000000 :
{
  *(.text)
}
}

PS: I am in real mode (obvious isn't it?)

RE:Problem kernel code

Posted: Sun Oct 05, 2003 11:00 pm
by knicos
Is your code been compiled as 16bit or 32bit? Cant see any [BITS 16], and what compiler are you using.

RE:Problem kernel code

Posted: Sun Oct 05, 2003 11:00 pm
by mindvnas
always align your (e)sp on 4 byte boundaries - see intel for explanation.

RE:Problem kernel code

Posted: Mon Oct 06, 2003 11:00 pm
by modshah
I use gcc and nasm. As for the bits I believe gcc outputs only 32bit code. Correct me if I am wrong.

regards
modshah

RE:Problem kernel code

Posted: Mon Oct 06, 2003 11:00 pm
by jcmatias
1 - if u use gcc and ld , your kernel certaly is  32 bits .
2- U must prepare a 32 bits cpu mode creating and seting gdt, idt and loading
   valid selectors in your segment registers etc..

3- There are a very particular thing about linking using this script:
   declare a data section end bss, otherwise your vars may be in stack