load 2nd sector from bootloader

412 views Asked by At

I am trying to load 2nd sector of floppy disk

I test this code with fasm 1.7 and windows 7, VMware. to make floppy image file, I used dd from Ubuntu 13.04 I also use dd to write .bin into .img

here's my code

org 0x7c00

;load 2nd sector to physical ram address 0xf00
;(load '7' to 0xf00)
mov ah, 02h
mov al, 1
mov ch, 0
mov cl, 2;1~63
mov dh, 0
mov dl, 0
mov bx, 0xf00
push 0
pop es
int 13h

;check whether it is loaded correctly
;by printing a ascii character in 0xf00
mov ah,0fh
int 10h
mov ah,0ah
mov al, [0xf00];
mov cx, 1
int 10h

;pause
jmp $

times 510-($-$$) db 0h
dw 0xaa55

second_sector:
db '7'

result = don't print '7' at all what's wrong with me? thanks

1

There are 1 answers

0
Michael Petch On

Frank Kotler is correct in his assessment that the issue is with the line:

mov al, [0xf00];

In the absence of a segment being set, [0xf00] is implicitly [ds:0xf00]. In this StackOverflow answer I give some general bootloader development tips. Tip #1:

When the BIOS jumps to your code you can't rely on CS,DS,ES,SS,SP registers having valid or expected values. They should be set up appropriately when your bootloader starts. You can only be guaranteed that your bootloader will be loaded and run from physical address 0x00007c00 and that the boot drive number is loaded into the DL register.

You do set ES to 0:

push 0
pop es

You have the option of explicitly setting the segment register on the memory operand like this:

mov al, [es:0xf00]

Or setting DS to 0 at startup. Remove these:

push 0
pop es

And add this to the top of your code:

xor ax, ax    ; Zero out ax
mov es, ax
mov ds, ax

You should assume the location of SS:SP (stack) conflicts with the memory you read disk sectors to, thus it's good practice to set SS:SP to a location that won't be interfered with. Reading disk data on top of the active stack will lead to failures. See the link to my Stackoveflow answer mention earlier for an example.