Writing Bootloader

Can anybody help me? I try to write a by-pass boot code, but it fails.
I don't know what is going wrong. It is expected that the below code will load the HD's OS into 1000h:0000h and run it after printing a string.

Thanks.

compiler: NASM

;===START=================================================
[bits 16]

start: mov ax, 07c0h ;update DS, ES
mov ds, ax
mov es, ax

;print "LOADING SYSTEM"

;do something

mov ax, 1000h ;ES:BX = 1000h:0000h
mov es, ax
mov bx, 0000h

read: mov ah, 02h ;read the boot sector from HD
mov al, 01h ;number of sector
mov ch, 00h ;cylinder
mov cl, 01h ;sector
mov dh, 01h ;head
mov dl, 80h ;drive
int 13h
jc err
jmp [es:bx]

err: ;print error message
hang: jmp hang

msg0 db 'LOADING SYSTEM...'
msg1 db 'disk error...'

times 512-($-$$)-2 db 0
dw 0AA55h
;===END===================================================

Comments

  • It seems you have two errors (I just glanced at the source). First, you must move your code to somewhere else then load the HD's bootsector at 0000h:7C00h. The second is the "JMP [ES:BX]". Instead of jumping to 1000h:0000h, it jumps to the location that 1000h:0000h holds. JMP BX and JMP [BX] are completely different. The first does this: MOV IP,BX while the second does this: MOV IP,[BX]. All your jump did was to add an ES segment prefix to JMP [BX]. Sucks, huh? Anyway, use this to solve it and jump to 0000h:7C00h:
    [code]
    db 0EAh ;JMP FAR imm.
    dd 00007C00h ;0000h:7C00h
    [/code]
  • Change it to this:

    Try it first on a floppy and then change the value to
    boot off the hard drive:

    [mov dl,80h] ; hard drive = [mov dl,0h] ; floppy drive

    ;===START=================================================
    [bits 16]
    [org 0]

    start: mov ax, 07c0h ;update DS, ES
    mov ds, ax
    mov es, ax

    mov si,msg0
    call print

    mov ax, 1000h ;ES:BX = 1000h:0000h
    mov es, ax
    mov bx, 0000h

    read: mov ah, 02h ;read disk function
    mov al, 01h ;number of sector to read
    mov ch, 00h ;cylinder 0
    mov cl, 01h ;sector 1
    mov dh, 01h ;head 1
    mov dl, [bootdrv];floppy drive

    int 13h
    jc err

    jmp 1000h:0000h

    err: mov si,msg1
    call print

    call hang

    hang: jmp hang

    print: lodsb
    or al,al
    jz done

    mov ah,0eh
    mov bx,7

    int 10h
    jmp print

    done: ret

    bootdrv db 0
    msg0 db 'LOADING SYSTEM...',13,10,0
    msg1 db 'disk error...',13,10,0

    times 510-($-$$) db 0
    dw 0AA55h

    ;===END===================================================


  • .....
    : read: mov ah, 02h ;read disk function
    : mov al, 01h ;number of sector to read
    : mov ch, 00h ;cylinder 0
    : mov cl, 01h ;sector 1
    : mov dh, 01h ;head 1
    : mov dl, [bootdrv];floppy drive
    .......

    Are you sure about head number!!!

    bootsector of floppy is on cyl:0 head:0 sector:1 next sector is cyl:0 head:0 sector:2!!!

    also hard discs have partitions and partition table. In fact your code reads from random location.

    Well i don't know how to bootstrap from hdd, i mean it's not so easy like floppy boot.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories