Assembly bootloader

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
roban100
Posts: 9
Joined: Wed Aug 07, 2013 10:05 am

Assembly bootloader

Post by roban100 »

So I'm trying to get a FAT32 bootloader to work but it doesn't go as planned.

One of the problems is that when i try to read the directory entries, it doesn't really work it reads all zeros.

here is the read code ( BTW I just started programming in assembly => I'm no good ) :

Code: Select all

     ;*************************************************************************
     ; PROCEDURE ReadSectors
	 ; Input: Valid Disk Address Packet
     ;*************************************************************************
     ReadSectors:
     .MAIN:
          mov     di, 0x0005                          ; five retries for error
     .SECTORLOOP:
	 pusha
		 mov ax, 0x0000
		 mov ds, ax
		 mov si, DAP.SIZE
		 mov dl, byte[DriveNumber]
		 mov ah, 0x42
		 xor al, al
		 int 0x13
		 jnc .SUCCESS
		 xor ax, ax
		 int 0x13
	 popa	 
		 dec di
		 jnz .SECTORLOOP
		 int 0x18
	 .SUCCESS:
	 popa
		ret
Which I'm calling with this code:

Code: Select all

     ; compute location of the begining of the Data area and store in ax
          mov     al, BYTE [TotalFATs]                ; Total number of FATs
          mul     WORD[BigSectorsPerFAT]                ; Number of sectors for a FAT
          add     ax, WORD [ReservedSectors]          ; Find the start of the Data area
          mov     WORD [datasector], ax               ; Store the begining of the Data area



     ; read 1st data cluster into memory
          mov     ax, WORD[datasector]
		  mov Word[DAP.START], ax
		  mov Word[DAP.OFFSET], 0x7E00
		  mov Word[DAP.SEGMENT], 0x0000
		  mov Word[DAP.AMOUNT], 0x0001
          call    ReadSectors
But with this code I ( think ) tested that it read all zeros into memory:

Code: Select all

          mov     di, 0x7E00 + 0x20                         
		  mov ah, 0x0E
		  mov al, Byte[di]
		  add al, 48
		  int 0x10
          hlt
		  jmp short $
The output of that

Code: Select all

int 0x10
is: 0

But as stupid as I'm I think that a part of the problems is how i execute it:

1. Convert an iso that contains the bootloader, and one file ( the 2:nd stage bootloader I want to load ), to a virtualbox .vdi file.

2. Make that .vdi file as the hard drive for the virtual machine.

3. Boot from hard drive.

So my question: Why is it reading all zeros? Is the way I'm taking it to virtualbox that is the problem?
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: Assembly bootloader

Post by Antti »

Code: Select all

	; compute location of the begining of the Data area and store in ax
	mov     al, BYTE [TotalFATs]               ; Total number of FATs
	mul     WORD[BigSectorsPerFAT]             ; Number of sectors for a FAT
Did you clear the ah register before this? Remember the MUL description:

Code: Select all

	MUL r/m16        DX:AX <- AX * r/m16
Also, you set the ds segment register in ReadSectors. Does that mean it was different before calling the procedure or was it just a safety precaution? If it was different, you should probably set it back when returning from the procedure. Did you take into account that "mov dl, byte[DriveNumber]" implicitly uses this ds segment?

Even if these notes are not related to your problem, I would recommend to study these things carefully. You might get bugs if you do not know exactly in which segment you are working on.
roban100
Posts: 9
Joined: Wed Aug 07, 2013 10:05 am

Re: Assembly bootloader

Post by roban100 »

@Antti
THANKS! I'm so stupid I had forgoten that. Now it works perfectly thanks!!!!! =D>
Post Reply