Hello,
I am using the following code for reading the kernel of 128 sectors.
when i allocate stack to the kernel and pass control to it,its code is not executed.Could you plz help me what is wrong in the following code??
Thanks,


########################################################
.equ BOOT_SEGMENT,0x07c0
.equ DISPLAY_SEGMENT,0xb800
equ SECTOR_SIZE, 0x0200

.text # Code segment
.globl _start # The entry point must be global
.code16 # Real mode

_start:
jmp over

os_size:
.long 0x00000000
drive_number:
.byte 0x00
max_sector:
.byte 0x00
max_head:
.byte 0x00
over:
movw $NEW_BOOT_SEGMENT, %ax #The address where the bios loaded the bootloader
movw %ax, %ds
movb %dl, drive_number #Address of the boot device
movb $0x08, %ah #getting parameters of boot device
int $0x13
andb $0x3f, %cl
movb %cl, max_sector
movb %dh, max_head
movw os_size, %bp #loop counter
movw $0x0002, %cx
movb $0x00, %dh
movw $0x0000, %ax
movw %ax, %es
movw $0x0100, %bx
read_sectors:
cmpw $0x0000, %bp #termination check
jng after_load
decw %bp
movw $0x0201, %ax #reading 1 sector at a time
int $0x13
jc error #if cf=1
cmpb max_sector, %cl
jnl next_head
incb %cl
jmp after_next

next_head:
movb $0x01, %cl
cmpb max_head, %dh
jnl next_cylinder
incb %dh
jmp after_next
next_cylinder:
movb $0x00, %dh
incb %ch
after_next:
addw $SECTOR_SIZE, %bx
jnc read_sectors
after_load:
movw $0x0003, %ax
int $0x10
mov $0x0e.%ah #after loading kernel at es:bx display a flag character
mov $'O',%al
int $0x10

mov $0x9000,%bx #allocate stack to the kernel
mov %bx,%ss
mov $0xfffe,%sp
ljmp 0x01000 #long jump to the kernel location
error:
mov $0x0e.%ah
mov $'E',%al
int $0x10
forever:
# Loop forever
hlt
jmp forever


Posted on 2010-04-12 07:25:42 by Ehsanulhaq
Ehsanulhaq,

Are you sure this code compiles? GAS doesn't think so:


...
equ SECTOR_SIZE, 0x0200
bootloader.gas:4: Error: no such instruction: `equ SECTOR_SIZE,0x0200'
...
mov $0x0e.%ah #after loading kernel at es:bx display a flag character
        ^    #before loading kernel GAS chokes on dot
...
ljmp 0x01000 #long jump to the kernel location
bootloader.gas:74: Warning: indirect ljmp without `*'


Additionally, boot sector is expected to contain 0x55,0xAA signature at offset 510 (otherwise BIOS considers media as non-bootable).

As a sidenote, [?code] [?/code] BBCode tags show that you respect the readers.
Posted on 2010-04-12 13:58:27 by baldr
Thank you for your reply.Plz try this code.it works.


# .equ symbol, expression
# These directive set the value of the symbol to the expression
   .equ    BOOT_SEGMENT,0x07c0
   .equ    DISPLAY_SEGMENT,0xb800
   .equ SECTOR_SIZE, 0x0200


.text               # Code segment
.globl    _start    # The entry point must be global
.code16             # Real mode

#
# The first instruction to execute in a program is called the entry
# point. The linker expects to find the entry point in the "symbol" _start
# (with underscore).
#

_start:
   jmp     over

drive_number:
.byte 0x00
max_sector:
.byte 0x00
max_head:
.byte 0x00
os_size:
   # Area reserved for createimage to write the OS size
   .word   0
   .word   0

   # This is where the bootloader goes
   # Over prints a single character to the screen
over:
   movw    $DISPLAY_SEGMENT,%bx
   movw    %bx,%es
   movw    $0x074b,%es:(0x0)


movw $BOOT_SEGMENT, %ax
movw %ax, %ds

movb %dl,drive_number

movb $0x08, %ah
int $0x13

andb $0x3f, %cl
movb %cl, max_sector

movb %dh, max_head
movw os_size, %bp

movw $0x0002, %cx #sector number
movb $0x00, %dh

movw $0x0000, %ax
movw %ax, %es
movw $0x0100,%bx

movw $0,  %dx
load_loop:
cmpw $5, %dx
jge loop1done

mov $0x0e,%ah
mov $':',%al
int $0x10

#movw %cx, %dx
incw %dx

movb drive_number, %dl
movw $0x0201, %ax

xchgw %bx, %bx

int $0x13
jc error

cmpb max_sector, %cl
jnl next_head
incb %cl
jmp after_next

jmp load_loop


next_head:


movb $0x01, %cl
cmpb max_head, %dh
jnl next_cylinder
incb %dh
jmp after_next
next_cylinder:
movb $0x00, %dh
incb %ch
after_next:
# Increment the destination
addw $SECTOR_SIZE, %bx
# Check the carry bit
jnc load_loop
# Advance the segment if it's on

movw %es, %ax
addw $0x1000, %ax
movw %ax, %es
jmp load_loop

error:

mov $0x0e,%ah
mov $'E',%al
int $0x10


forever:
   # Loop forever
   hlt
   jmp     forever

loop1done:
mov $0x0e,%ah
mov $'C',%al
int $0x10


#mov $0x9000,%bx
#mov %bx,%ss
#mov $0xfffe,%sp

       jmp 0x01000


Posted on 2010-04-12 21:35:11 by Ehsanulhaq

As a sidenote, [?code] [?/code] BBCode tags show that you respect the readers.


Yes... so we don't feel compelled to yell Posted on 2010-04-12 22:21:46 by SpooK
I forgot to mention one thing that i have found this project on website of princeton university. I am completing this project for learning purpose.Could you plz help??
Posted on 2010-04-13 00:19:10 by Ehsanulhaq
Hello,
I am using the following code for reading the kernel of 128 sectors,store it at address 0000:1000 and then pass control to it.
The code is not working properply.Could anyone plz help??

.equ NEW_BOOT_SEGMENT, 0x00e0
# Number of bytes in a sector
.equ SECTOR_SIZE, 0x0200
# The boot loader puts the OS here
.equ OS_SEGMENT, 0x0100
# offset in the GDT of the OS code segment
.equ OS_CODE_DESCRIPTOR, 0x00
# offset in the GDT of the OS data segment
.equ OS_DATA_DESCRIPTOR, 0x10
# Initial stack setup
.equ STACK_SEGMENT, 0x9000
.equ STACK_POINTER, 0xfffe
    .equ    BOOT_SEGMENT,0x07c0
    .equ    DISPLAY_SEGMENT,0xb800

.text              # Code segment
.globl    _start    # The entry point must be global
.code16            # Real mode

#
# The first instruction to execute in a program is called the entry
# point. The linker expects to find the entry point in the "symbol" _start
# (with underscore).
#

_start:
    jmp    over
boot_flag:     
.word 0xAA55

os_size:
.long 0x10000000
# Reserve space for the drive parameters
drive_number:
.byte 0x00
max_sector:
.byte 0x00
max_head:
.byte 0x00

    # Area reserved for createimage to write the OS size
    .word  0
    .word  0

    # This is where the bootloader goes
    # Over prints a single character to the screen
over:
    movw    $DISPLAY_SEGMENT,%bx
    movw    %bx,%es
    movw    $0x074b,%es:(0x0)



        movw $0x0030, %ax
movw %ax, %ss
movw $0x0100, %sp


movw $BOOT_SEGMENT, %ax
movw %ax, %ds
movw $0x0000, %si

        movw $NEW_BOOT_SEGMENT, %ax
movw %ax, %es
movw $0x0000, %di

movw $SECTOR_SIZE, %cx

cld

rep movsb

after_move:

movw $NEW_BOOT_SEGMENT, %ax
movw %ax, %ds

movb %dl,drive_number

movb $0x08, %ah
int $0x13

andb $0x3f, %cl
movb %cl, max_sector

movb %dh, max_head
movw os_size, %bp

movw $0x0002, %cx #sector number
movb $0x00, %dh

movw $0x0000, %ax
movw %ax, %es
movw $0x1000,%bx


load_loop:

cmpw $0x0000, %bp
jng after_load
decw %bp

movb drive_number, %dl
movw $0x0201, %ax

xchgw %bx, %bx

int $0x13
jc error
cmpb max_sector, %cl
jnl next_head
incb %cl
jmp after_next
next_head:
movb $0x01, %cl
cmpb max_head, %dh
jnl next_cylinder
incb %dh
jmp after_next
next_cylinder:
movb $0x00, %dh
incb %ch

after_next:

# Increment the destination
addw $SECTOR_SIZE, %bx
# Check the carry bit
jnc load_loop

# Advance the segment if it's on

movw %es, %ax
addw $0x1000, %ax
movw %ax, %es
jmp load_loop

error:

mov $0x0e,%ah
mov $'E',%al
int $0x10

jmp forever

after_load:

        movw $0x0003, %ax
int $0x10
 
mov $0x0e,%ah
mov $'C',%al
int $0x10
        movw $0x0, %ax
movw %ax, %ds

movw $0x00,%ax
movw $0x00,%bx
movw $0x00,%cx
movw $0x00,%dx
movw %bx,%ss
movw %bx,%es

# Set up the stack
mov $0x9000,%bx
mov %bx,%ss
mov $0xfffe,%sp
        ljmp 0x1000
forever:
mov $0x0e,%ah
mov $'F',%al
int $0x10
    # Loop forever
    hlt
    jmp    forever
Posted on 2010-04-13 04:22:14 by Ehsanulhaq
Ehsanulhaq,

OK, it compiles. Probably you can even --force GRUB to chain-load and execute it (or disable bootsig check in Bochs; do you understand why it won't be executed by regular BIOS?). What have you learnt from it already?
Posted on 2010-04-13 07:16:04 by baldr
Thanks for your reply.I have learnt one thing that it is very to write code for a boot loader :) .I am using QEMU for testing the image.could you plz point out what is wrong in the code.
Posted on 2010-04-13 07:24:01 by Ehsanulhaq

I am using QEMU for testing the image.could you plz point out what is wrong in the code.


baldr explained that in his first response to this thread, the code you posted is missing the standard MBR boot signature.
Posted on 2010-04-13 11:57:19 by SpooK
Ehsanulhaq, I've merged your "boot loader" threads into this single discussion, as they are fundamentally no different from each other and exhibit the same problem(s).
Posted on 2010-04-13 12:09:54 by SpooK

I am using the following code for reading the kernel of 128 sectors,store it at address 0000:1000 and then pass control to it.
The code is not working properply.Could anyone plz help??


Yes, I can help by pointing out that you have a severe misunderstanding of what a MBR is and its corresponding structure/format.

I recommend that you read the OSDev Wiki article on the MBR.
Posted on 2010-04-13 12:14:48 by SpooK
Alot of pointless code included like yours equivalent bytes-you only have 510 bytes, don't waste it.  :lol:
Lol
Posted on 2010-08-18 12:09:34 by marcalexanderreed