Reputation: 154
I am trying to develop a very simple bootloader and I am following a tutorial for that purpose. My question is about addressing and the org
command.
I understand why the org
command is needed to fetch data at a given address. Since the boot sector will be loaded at address 0x7c00 it is needed to inform the compiler that this address should be added to any reference. For instance the code below would not work without the [org 0x7c00]
command because it would fetch data at address 0x0012 instead of 0x7c12.
[org 0x7c00]
; print String
mov ah, 0x0e
mov bx, text
printLoop:
mov al,[bx]
cmp al,0
je loop
int 0x10
inc bx
jmp printLoop
loop:
jmp loop
text: ; at address 0x0012 in the file but will be loaded at 0x7c12
db 'Loading OSiris operating system...',10,13,0
times 510-($-$$) db 0
dw 0xaa55
Now what I do not understand is why it is not the same for the jmp
command? In short I do not understand why the code below works (infinitely prints ?, org
command commented).
;[org 0x7c00]
mov ah, 0x0e
mov al, '?'
loop: ; as I understood 0x0004 in the file but 0x7c04 in the memory
int 0x10
jmp loop ; hence this should jump at 0x0004 and the code should not work (but it actually works)
times 510-($-$$) db 0
dw 0xaa55
To my understanding the jump should be done to address 0x0004 . Hence the computer should boot infinitely and not just print "?". Is it something do to with local jumps and code segments?
Upvotes: 0
Views: 77
Reputation: 58762
That form of a short jmp
is encoded using relative offset. It basically says "jump back 4 bytes". It is thus position independent and works regardless. The actual machine code is EB FC
where EB
is the opcode for a short jump, and FC
is -4
, the offset counted from the address of the following instruction where execution would normally continue.
Also note that the entry point is only guaranteed to be at physical address 0x7c00, you shouldn't rely on that being 0:0x7c00
. Some BIOSes might use 0x7c0:0
. To be safe, you should initialize your segment registers, including CS
(use a far jump for this).
Upvotes: 2