0

I searched for loading second stage from boot loader first stage. I found a code and formatted it. I compiled it and ran with VirtualBox but it does not do anything. Is there a mistake about the code?

Code:

bits 16

org 7c00h

mov ah, 02h
mov al, 1
mov dl, 80h
mov ch, 0
mov dh, 0
mov cl, 2
mov bx, stage2
int 13h

jmp stage2

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

stage2:
mov ax, 0e61h
;mov ah, 0eh
;mov al, 61
int 10h

cli
hlt

times 1024 - ($ - $$) db 0

I found the code from here.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Nafe Bon
  • 33
  • 7
  • 1
    You didn't set ES to 0 (Where you read to is specified by ES:BX with in 13h/AH=2). You hard code DL to 0x80 which is the first hard drive. Did you make a proper hard drive image that you can boot with in Virtualbox? PS: It is usually a good idea to use the value in DL that the BIOS sets just before it transfers control to your bootloader. DL will already contain the drive number that was booted from (you shouldn't hard code it). I assume you intended to print `a` to the screen? – Michael Petch Jan 15 '20 at 13:57
  • 1
    As well you don't know where the BIOS put the stack. To avoid reading on top of the current stack you should set SS:SP to a value that is out of the way. Since you are reading to memory after 0x0000:0x7e00 you can set SS:SP to 0x0000:0x7c00 so the stack grows down from just below the bootloader. – Michael Petch Jan 15 '20 at 13:59
  • @MichaelPetch I added `mov ax, 0h mov ss, ax mov sp, 7c00h`. Is it OK? And yes I intended to print `a` to the screen. – Nafe Bon Jan 15 '20 at 18:30
  • 2
    That looks okay. but you should also be doing a `mov es, ax` before the `mov ss, ax` so that ES is also set to 0. Remove the `mov dl, 80h` entirely. The code from the other stackoverflow answer is not very good and very buggy. – Michael Petch Jan 15 '20 at 18:35
  • Thank you, it worked. I was searching writing after boot signature for a few days and now, I found. – Nafe Bon Jan 15 '20 at 18:38
  • I assume it probably worked after you removed setting DL to 80h. If that was the case, it probably didn't work properly was because you were booting as a floppy when 80h happens to be the first hard drive. Were you booting a a floppy disk? – Michael Petch Jan 15 '20 at 18:39
  • I compile my code and change its extension to `.img`. I add floppy disk to my VirtualBox. I boot it. I think I am booting a floppy disk, am I? – Nafe Bon Jan 15 '20 at 18:43
  • 1
    Yep, that would be booting a floppy disk. So if that's the case then `mov dl, 80h` failed to read properly because 80h is the first hard drive. The first floppy is 00h usually. By removing the `mov dl, 80h` you use the value of the boot drive given by the BIOS to your bootloader. If you are curious how to make a bootable VDI hard drive image for virtualbox I have this answer: https://stackoverflow.com/a/43566982/3857942 – Michael Petch Jan 15 '20 at 18:48

0 Answers0