Page 1 of 1

Boot loader explanation?

Posted: Sun Sep 13, 2009 6:12 am
by Aurdal
I found this tutorial about boot loaders, located at http://inglorion.net/documents/tutorial ... ootsector/, but it left me with some questions:
Here's the bootloader source. I've commented every action with what I gather from the tutorial it does. Please do let me know if I assume wrong somewhere.
However, I know I don't understand the following:
  • The"xor bx, bx" command
  • Why CL is set to 1
  • Why DH is set to 0

Code: Select all

[BITS 16]		;tell the assembler that its a 16 bit code
[ORG 0x7C00]	;Origin, tell the assembler that where the code will
				;be in memory after it is been loaded


; Kernel info
KERNEL_START equ 1			; Disk block where kernel starts
KERNEL_SIZE equ 1			; Kernel size in disk blocks
KERNEL_SEGMENT equ 1000h	; Segment where kernel will be loaded


; Load kernel
mov ax, 200h + KERNEL_SIZE	; Set AH to 2 (OPcode to read), and AL to KERNEL_SIZE
push word KERNEL_SEGMENT	; Send KERNEL_SEGMENT to stack

pop es		; Retrieve KERNEL_SEGMENT from stack and store in ES,
			; which is where "int 13h" will look for the segment to read from.

xor bx, bx	; I don't understand this, but since BX is the offset,
			; I assume this somehow sets BX = 0?

mov cx, KERNEL_START + 1	; Set CH = KERNEL_START, Set CL = 1 (Why?)

mov dx, 0	; Set head = 0 (Why?), Set drive number = 0 (Floppy A)
int 13h		; Invoke BIOS to OPcode(2:Read), flag if error.
jnc ok		; Jump to ok: if not carry flag
jmp $		; Infinite loop if carry flag

ok:
jmp KERNEL_SEGMENT:0		; Jump to Kernel, located at 1000h with offset 0


TIMES 510 - ($ - $$) db 0	; Fill the rest of sector with 0
DW 0xAA55					; Add boot signature at the end of bootloader
Thanks in advance. :)

Re: Boot loader explanation?

Posted: Sun Sep 13, 2009 6:32 am
by Cjreek
Hi,

You should read Int 13h and CHS on Wikipedia or I guess the osdev wiki contains similar articles.

Code: Select all

xor bx, bx
writes 0 to bx.

do you know how the xor composition works? if you've got 2 binary numbers, like

1001 and 1101

then

Code: Select all

     1001
xor 1101
--------
     0100
because if you xor 2 equal digits you'll get 0 else 1. So if you xor a number with itself you get always zero.

I hope I explained it good enough :mrgreen:

Re: Boot loader explanation?

Posted: Sun Sep 13, 2009 6:39 am
by Aurdal
Thanks for the quick response, Cjreek.
I'll make sure to read the wikipedia articles you provided. And yes your explanation of xor was satisfactory, I believe I understand it now. Thanks. :mrgreen:

EDIT:

Just to make sure:

Code: Select all

xor gives 1                       while or gives 1
if and only if                    if either of the 
only one of the                   numbers are 1:
numbers are 1: 

    1001                                1001
xor 0101                            or  0101
--------                            --------
    1100                                1101
Am I right?

Re: Boot loader explanation?

Posted: Sun Sep 13, 2009 7:02 am
by Creature
Aurdal wrote: Just to make sure:

Code: Select all

xor gives 1                       while or gives 1
if and only if                    if either of the 
only one of the                   numbers are 1:
numbers are 1: 

    1001                                1001
xor 0101                            or  0101
--------                            --------
    1100                                1101
Am I right?
I believe that is correct. I also hope you're referring to the bits with 'numbers' :).

Re: Boot loader explanation?

Posted: Sun Sep 13, 2009 7:23 am
by Aurdal
Yes, I was refering to the individual bits. (I just realised the connection digits in decimal and bits in binary. lalalala... :p )

Also is there a reason to use "xor bx, bx" instead of just "mov bx,0"?

EDIT: Am I right in believing these two pieces of code do the same thing?
Code1:

Code: Select all

; Load Kernel
mov ah, 02h             ; Tell int 13h to read
mov al, KERNEL_SIZE     ; How much to read
mov ch, KERNEL_START    ; Track to read
mov cl, 1               ; Sector to read
mov dh, 0               ; Head to read
mov dl, 0               ; Device to read (0 = Floppy A)
mov es, KERNEL_SEGMENT  ; ES:BX - Where to load
xor bx, bx              ; Set BX = 0
int 13h                 ; Invoke read, flag if error
jnc ok                  ; Jump to "ok:" if no error
jmp $                   ; Infinite loop if error
Code2:

Code: Select all

; Load kernel
mov ax, 200h + KERNEL_SIZE ; Set AH to 2 (OPcode to read), and AL to KERNEL_SIZE
push word KERNEL_SEGMENT   ; Send KERNEL_SEGMENT to stack
pop es                     ; Retrieve KERNEL_SEGMENT from stack and store in ES,
xor bx, bx                 ; Set BX = 0;
mov cx, KERNEL_START + 1   ; Set CH = KERNEL_START, Set CL = 1 (Why?)
mov dx, 0                  ; Set head (DH) = 0 (Why?), Set drive number (DL) = 0 (Floppy A)
int 13h                    ; Invoke BIOS to OPcode(2:Read), flag if error.
jnc ok                     ; Jump to ok: if not carry flag
jmp $                      ; Infinite loop if carry flag

Re: Boot loader explanation?

Posted: Sun Sep 13, 2009 10:05 am
by DLBuunk
Thes two pieces of code do the same thing, however, code1 is, once compiled, larger. The xor bx,bx is the same reason: xor bx,bx is 2 bytes machinecode, while mov bx,0 results in 3 bytes machinecode.

Re: Boot loader explanation?

Posted: Sun Sep 13, 2009 10:16 am
by Aurdal
I get it now. My thanks go out to everyone who has replied in this thread. :mrgreen: