Pregunta

My code works perfectly in virtualbox but not when booted on a real PC (from a USB pen drive which BIOS detects as a USB Hard Drive.)

In virtualbox; the code reads sector 2&3 of the disk to memory, prints the first 128 bytes (as a debug step) then executes the code stored in those sectors.

On my real PC, it successfully prints the correct bytes to screen (so obviously is reading the disk correctly, and writing it to the expected place in memory) but then stops execution at that point rather than jumping.

Why would it be different, and what could I be doing wrong?

ORG 0x7C00;

; Load Sector 2&3 from disk to 0x1000

mov bx , 0x1000             
mov ah , 0x02   
mov al , 0x02   
mov ch , 0x00   
mov dh , 0x00   
mov cl , 0x02   
int 0x13;


;Print 0x1000 + 128 bytes

mov ah, 0x0e        
mov bx ,0x1000;
loop2:          

mov al, [bx]        
cmp bx, 0x1000+128  

je end2
int 0x10        
add bx , 1;     
jmp loop2;

end2:

; Run our code


call 0x1000

jmp $;

TIMES 510 - ($ - $$) DB 0
DW 0xAA55
¿Fue útil?

Solución

There are lots of problems with this code that you might find on real hardware:

  1. The state of most of the registers when you enter your bootloader is undefined - but the registers need to be valid when you call into interrupt routines. Make sure you set up your segment registers immediately on starting the bootsector. For example, if ES != CS, your jump to the second stage will land in the wrong place.

  2. Ensure you have a valid stack before calling into interrupt routines.

  3. Do not rely on interrupt routines being available. Many hardware vendors recognise build their hardware to work for Windows and Linux (since that's what 99.999% of their customers want), and don't bother implementing interrupt routines they know Windows and Linux won't call.

  4. Before doing disk-accesses, try doing something simpler, like printing a string out to the video display. Next, write a way of printing a register to the screen. Only then will you be in a position where you can start writing a bootloader in a way that you can debug.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top