Question

I wanna write my own MBR on my os boot.Mbr in diffrent file Bootloader in diffrent file I want to load mbr in memory with int 13h but it loads only bootloader.i think that was because of org Sample Code:

;----------------------------
;AFC OS Bootloader
;see afcosblog.blogspot.com
;-----------------------------
bits 16
org 0x7c00

start:
mov ah,00
mov al,12h;640x480 16 color
int 10h
;----------------
mov ah,0x0E
mov al,'A';print 'A'
int 10h
;------------------
;

I wanna Write Mbr in there but it was writing Bootload

xor ax, ax
mov es, ax    
mov cx, 1    
mov dx, 0080h ;0th Hdd
mov bx, 7c00h ;I think problem was in there
mov ax, 0301h 
int 13h
;Read
xor ax, ax
mov es, ax    
mov cx, 1    
mov dx, 0080h ;0th Hdd
mov bx, 7c00h 
mov ax, 0201h 
int 13h
cmp ah,00 ;AH:Status
jne error
jmp 0h:7c00h
error:
mov ah,0x0E
mov al,'E'
int 10h
mov ah,10h
int 16h
int 19h
ret
times 510-($-$$) db 0
dw 0xAA55
;---------------------------
;MBR.asm
;AFC OS MBR
;see afcosblog.blogspot.com
;This sample is 16 bit  arch:x86
bits 16
org 0x7c00 ;<----
start:
push ax
mov ax,0a00h
mov es,ax
pop ax
mov ah,0x0E
mov al,'O'
int 10h
mov ah,10h
int 16h ;Keystroke
int 19h ;Reboot
times 510-($-$$) db 0
dw 0xAA55
;End of MBR

I was compaling in Windows nasm and I prepare iso miso.exe and copy.exe Thanks.

Was it helpful?

Solution

There are several problems in your code:

  1. In your first code sample:

    1.1. You should locate code you expect to have some output somwhere else than on 0x7c00. use for example 0x8000 instead.

    1.2. You set mode 0x12, that's good, but why are you trying to output text A? Mode 0x12 is used for pixel writing, BIOS won't write character to VGA memory - or it will, but VGA expects different data, so it will have some unreadable output.

    1.3. You should stop code execution, or you'll get invalid opcode/lock prefix not allowed exceptions.

  2. In your second code sample, first part:

    2.1. You want to read track0,sector1 on HDD. That's good, but track0,sector1 is place where bootloader is located.

    2.2. On the line mov bx, 0x7c00 - why are you loading everything on 0x7c00?

    2.3. Function 3 of interrupt 0x13 writes sectors, why are you writing?

    2.4. You should actually check carry flag if it's set, before checking AH.

    2.5. Again, why are you jumping on the start of bootloader (0x7c00)?

  3. Second code sample, second part

    3.1. Pushing something in bootloaders code is actually pushing value to small stack set up by BIOS. You don't retrieve value ofAXlater, so be careful if you want tocall` this code in future. 3.2. Finally, I don't understand your rebooting. Why are you doing it?

Here I have working example:

;---------------
;BOOTLOADER
; - loads second sector from disk and executes it
;---------------
start:
    xor ax, ax
    mov es, ax
    mov bx, 0x8000  ;loading to 0x8000
    mov cx, 2       ;reading second sector, track 0
    mov al, 1       ;just one sector
    mov ah, 02      ;function 02: read sectors from disk (CHS)
    mov dx, 0x80    ;1st HDD, head 0
    int 0x13

    jmp 0x800:0
times 510-$ db 0
dw 0xAA55

;---------------
;SECOND STAGE
; - prints some characters
; - stops execution
;---------------
stage2:
    mov ah, 0x0E    ;function 14: teletype output
    mov al, 'A'     ;printing 'A' (0x41)
    mov bl, 0x0F    ;white text on black background
    mov bh, 0x0     ;page 0
    int 0x10        ;print!

    jmp $            ;stop execution

times 1024-$ db 0   ;align
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top