0

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.

Antarus
  • 1,573
  • 2
  • 15
  • 32
user2590769
  • 75
  • 1
  • 10
  • How are you combining the two pieces of code after you've built them? – Michael Jul 17 '13 at 12:12
  • I am combining thats on image – user2590769 Jul 17 '13 at 12:22
  • Well, you posted one big blob of code that doesn't compile. So I'm assuming that they are two separate assembly files, and that you are combining the output somehow. Do you just concatenate them with `copy /b`? In which order? – Michael Jul 17 '13 at 12:27
  • I was just used copy /b – user2590769 Jul 17 '13 at 12:30
  • Thanks Antarus!!! An MBR includes a partition table. Where's yours? What an MBR wants to do, generally, is to move itself out of the way, parse the partition table to find the "active" (bootable) partition, read that partition's bootsector to 7C00h, then jump to 7C00h. I don't even understand the question. Have a "rescue disk" handy - you're going to trash your computer the way you're going! – Frank Kotler Jul 17 '13 at 12:31
  • I wanna save mbr on hdd without Partion table – user2590769 Jul 17 '13 at 12:32
  • When I was change mbr Org and writing buffer adress it was writing. But it was not booting .because of origins. – user2590769 Jul 17 '13 at 12:35
  • Can anyone give example source code about this? – user2590769 Jul 17 '13 at 12:39
  • If you're just concatenating them in the order in which they appear in your question, then the code starting with the `xor ax, ax` is what's going to be loaded at 0x7c00, and the code in mbr.asm is going to loaded at 0x7c00 + sizeof(the first piece of code) – Michael Jul 17 '13 at 12:45
  • Michael can you give an example source code? – user2590769 Jul 17 '13 at 12:52

1 Answers1

0

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
user35443
  • 6,309
  • 12
  • 52
  • 75
  • 2.3 MBR Must Write - 0x7c00 Bios load adress – user2590769 Jul 17 '13 at 19:56
  • But why are you 'writing' MBR to disk **again**, when it is already there? – user35443 Jul 18 '13 at 05:47
  • I was Writing This On Boot,I Must be. I was Understand what you say. One time i want read disk if mbr in sector 1 but it was not worked – user2590769 Jul 18 '13 at 05:56
  • And Where is The Writing Operation? – user2590769 Jul 18 '13 at 05:59
  • First stage is mbr, second stage is the thing that usually loads kernel. MBR and bootloader are practically same things. MBR is that thing on the start of hdd/floppy - first sector. Bootloader can be located in MBR, but unbootable devices have zeroes/`0x90`s there. MBR is first sector, bootloader is code in that first sector (or code somewhere else). – user35443 Jul 18 '13 at 06:01
  • I didn't understand your Writing Operation, so I didn't put it there. You must explain it more, I don't see it's point. – user35443 Jul 18 '13 at 06:02
  • u can Write tome Mbr Writer? – user2590769 Jul 18 '13 at 06:02
  • Sorry, but I don't understand you. Why do you want to write MBR to disk while the code is being executed? If you want to write MBR and then run the code, use `nasm -f bin -o boot.bin boot.asm` for compilation (remember to check if it's exactly 1024 bytes), and `copy boot.bin boot.img` for 'creating' disk image. Then you can mount resulting image to some virtual machine, like VirtualBox (in virtulabox it's pretty simple). – user35443 Jul 18 '13 at 06:05
  • on CD boot When Bios Boot Bootloader Bootloader will write MBR (Not OS in this example) – user2590769 Jul 18 '13 at 06:06
  • On CD there's **NO** MBR. For CDs there is special El Torito norm/standard. – user35443 Jul 18 '13 at 06:07
  • Would you be so kind and started creating sentences that have some sense? – user35443 Jul 18 '13 at 06:09