D
D
devalone2016-11-07 19:38:01
Programming
devalone, 2016-11-07 19:38:01

How to load code from disk into RAM in real assembler mode?

Hello. I'm trying to write a program that will load its code from disk (since bios only loads 512 bytes) into memory and transfer control there. The code turned out like this:

use16
org 0x7c00
section .text
start:
    mov al, '&'
    mov ah, 0x0E; номер функции BIOS
    mov bh, 0; страница видеопамяти
    int 0x10; выводим символ
 
    push  es
    push  ds
    mov   ax, 0x100         ; сегмент куда пишем
    mov   es, ax
    mov bx, 0; адрес куда пишем
    mov   ch, 0; дорожка 0
    mov   cl, 2   ; начиная с сектора 2
    mov   dl, 0x80; номер диска
    mov   dh, 1; номер головки
    mov   ah, 2; номер функции
    mov   al, 1;считать 1 сектор
    int   0x13
    
    jnc .no_error
    mov al, '!'
    mov ah, 0x0E; номер функции BIOS
    mov bh, 0; страница видеопамяти
    int 0x10; выводим символ
    jmp $
    .no_error:
    
    pop   ds
    pop   es
    
    
    jmp 0x100:0
   
finish:
    times 0x1FE-finish+start db 0; тут выравниваем блок кода до размера сектора(512 байт)
    db 0x55, 0xAA; сигнатура загрузочного сектора
    
main:
    mov al, '.'
    mov ah, 0x0E; номер функции BIOS
    mov bh, 0; страница видеопамяти
    int 0x10; выводим символ
    jmp 0:0x7c00

but it throws an error (carry flag = 1) after trying to read the second sector from the disk, where the code after the main label should be. Error code 12 (case ah) whatever that means. I also found a code for reading in LBA mode, as I understand it, it should read the first sector from the disk (i.e. itself), but there is also an error:
use16
org 0x7c00
 
section .text
start:
load:
    mov al, '&'
    mov ah, 0x0E; номер функции BIOS
    mov bh, 0; страница видеопамяти
    int 0x10; выводим символ
 
        
    mov ax, 0x100
    mov ds, ax
    mov ah, 0x42; читать в LBA режиме
    mov dl, 0x80
    mov si, 0x0
    int 0x13
    
    jnc no_error    
    mov al, '!'
    mov ah, 0x0E; номер функции BIOS
    mov bh, 0; страница видеопамяти
    int 0x10; выводим символ
    jmp $
    no_error:
    
    
    
    jmp 0x100:0; прыгаем туда, куда загрузили 
    
finish:
    times 0x1FE-finish+start db 0
    db 0x55, 0xAA; сигнатура загрузочного сектора
    
    
main:
    mov al, '.'
    mov ah, 0x0E; номер функции BIOS
    mov bh, 0; страница видеопамяти
    int 0x10; выводим символ

I compile it like this: nasm -f bin code.asm I run
it like this: qemu-system-i386 code
I tried to connect it as a floppy disk and write 0 to dl, the same thing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2016-11-07
@devalone

Heads are numbered from 0. Correct mov dh, 1 in the first example to mov dh, 0 and you will see your &.&.&.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question