Page 2 of 2

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 2:06 pm
by CopperMan
Ok, I'll try to explain ...

We load 0 to SS segment register when :

Code: Select all

xor ax, ax
mov ss, ax
And then zeroing SP with :

Code: Select all

xor sp, sp
After this we have 0000:0000 in SS:SP. When we try to push somethig to stack, SP gets decreased : SP = SP - 2 (for example). Value of SP after that FFFD, and the SS:SP will look like 0000:FFFD. Pushed value will be stored at this address. So we have a stack at top of first 64k of ram, and room for this stack 10000 - 07E00(where bootsector code ends) is about 32k.

Very sorry for my bad English.

Cheers,
CopperMan ;)

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 3:36 pm
by gravaera
Right. I forgot clean about the wraparound thing.

And no, your English really isn't that bad, as far I can see.

Re: problem in booting at int 13h

Posted: Sun Sep 20, 2009 4:19 pm
by geppyfx
If you code doesn't work in Bochs then following hardcoded values can be incorrect
phoenix07p wrote: MOV AH,02
MOV AL,18d
MOV CH,0
MOV CL,5d
MOV DH,0
MOV DL,81h
MOV BX,0
MOV ES,BX
MOV BX,0X800
INT 13H
If you code works fine in bochs and doesn't work on selected PCs then these are possible:
1. you are reading multiple sectors at once while bios works correctly with one sector at a time.
2. on some bioses with some disks(mostly USB emulation as HDD or FDD in my experience) ah=2 doesn't work while returning successfully, use int13 extension (ah=42h ...). In modern systems if disk ID in DL is 0x80 or higher - try using int13 extensions first .
3. interrupts must be enabled, (disabled when you setup SS & SP)
4. especially for floppies you need to issue a reset if (and only if) read fails and only then try again

Re: problem in booting at int 13h

Posted: Mon Sep 21, 2009 1:18 am
by qw
phoenix07p wrote:meanwhile just a question why would I want to initialize segment register if i am not even using them
Yes you are. You're just not aware of it.

Re: problem in booting at int 13h

Posted: Mon Sep 21, 2009 3:29 am
by phoenix07p
I think int 13h is working cause the sector count actually read when i printed turned out to be '↕' i checked that this was ascii 18 which is exactly the number of sectors i gave to read. I think there must some problem with the location of my memory then.

and this is the kernel code

Code: Select all

**********
kernel.c
**********

#define WHITE_TXT 0x07
extern void k_main();
void k_clear_screen();
unsigned int k_printf(char *message, unsigned int line);
void update_cursor(int row, int col);

void k_main() // like main in a normal C program
{
        k_clear_screen();
        k_printf("Hi!\nHow's this for a starter OS?", 0);
};

void k_clear_screen() // clear the entire text screen
{
        char *vidmem = (char *) 0xb8000;
        unsigned int i=0;
        while(i < (80*25*2))
        {
                vidmem[i]=' ';
                i++;
                vidmem[i]=WHITE_TXT;
                i++;
        };
};

unsigned int k_printf(char *message, unsigned int line) // the message and then the line #
{
        char *vidmem = (char *) 0xb8000;
        unsigned int i=0;
        i=(line*80*2);
        while(*message!=0)
        {
                if(*message=='\n') // check for a new line
                {
                         line++;
                         i=(line*80*2);
                         message++;
                } else {
                         vidmem[i]=*message;
                         message++;
                         i++;
                         vidmem[i]=WHITE_TXT;
                         i++;
                };
        };
        return(1);
};
**********
kernel_start.asm
**********
global loader           ; making entry point visible to linker
extern k_main            ; kmain is defined elsewhere
 
; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ  1<<0                   ; align loaded modules on page boundaries
MEMINFO     equ  1<<1                   ; provide memory map
FLAGS       equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ    0x1BADB002           ; 'magic number' lets bootloader find the header
CHECKSUM    equ -(MAGIC + FLAGS)        ; checksum required
 
section .text
align 4
MultiBootHeader:
   dd MAGIC
   dd FLAGS
   dd CHECKSUM
 
; reserve initial kernel stack space
STACKSIZE equ 0x4000                  ; that's 16k.
 
loader:
   mov esp, stack+STACKSIZE           ; set up the stack
   push eax                           ; pass Multiboot magic number
   push ebx                           ; pass Multiboot info structure
 
   call  kmain                       ; call kernel proper
 
   cli
hang:
   hlt                                ; halt machine should kernel return
   jmp   hang
 
section .bss
align 32
stack:
   resb STACKSIZE                     ; reserve 16k stack on a quadword boundary
***********
linker.ld
**********
ENTRY (loader)

SECTIONS{
    . = 0x00100000;

    .text :{
        *(.text)
    }

    .rodata ALIGN (0x1000) : {
        *(.rodata)
    }

    .data ALIGN (0x1000) : {
        *(.data)
    }

    .bss : {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
Thanks

[EDIT]: Brendan added code tags...[/EDIT]

Re: problem in booting at int 13h

Posted: Mon Sep 21, 2009 7:43 am
by neon
neon wrote: Please use code tags when posting code - it makes it easier to read. Also, it might help if you post your bochs crash log (Only the last 10-20 lines please - not the whole log.)

Re: problem in booting at int 13h

Posted: Tue Sep 22, 2009 12:45 pm
by phoenix07p
Thanks brenden didn't know about that. I don't use bochs.

Re: problem in booting at int 13h

Posted: Thu Sep 24, 2009 3:49 am
by phoenix07p
ok i set up bochs . the problem is the kernel.c halts without printing anything.

I think for the kernel_start.asm to work i will have to swtich to protected mode. but when i make that switch machine restarts immediately after jump.
I added this immediately after int 13h if successful

Code: Select all

cli 
lgdt  [gdtr]
mov   eax, cr0
or    al,0x1 
mov   cr0,eax
jmp   0x0000:0x8000                  ; this is where es:bx was pointing while reading sectors from floppy
and this near the end before times

Code: Select all

: 
gdt:        dw    0x0000, 0x0000, 0x0000, 0x0000
sys_data:   dw    0xFFFF, 0x0000, 0x9200, 0x00CF
sys_code:   dw    0xFFFF, 0x0000, 0x9800, 0x00CF
gdt_end:

gdtr:	    dw gdt_end - gdt - 1	                                  
 	    dd gdt
whats the problem now??