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.
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:
[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.
Caution: Enjoy above remarks responsibly. Overuse may be hazardous to your mental health.
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.
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?
Caution: Enjoy above remarks responsibly. Overuse may be hazardous to your mental health.
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' .
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
; 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
; 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
Caution: Enjoy above remarks responsibly. Overuse may be hazardous to your mental health.
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.