Reputation: 1941
The bootloader will anyway load it at that address? Why specify this to the program?
Upvotes: 7
Views: 5361
Reputation: 172
I also very confused about it. It looks like it depends on your code and link arguments. If you look at the boot.S in grub2, it has no ".org 0" or ".org 0x7c00".
I also wrote a simple bootloader code. What I use is ".org 0", but I can remove it without breaking the function. My code is build like this:
all:
i686-elf-as -o boot.o boot.S
i686-elf-ld --oformat=binary -Ttext=0x0 -o boot.bin boot.o
Upvotes: -2
Reputation: 6753
You don't have a choice here. Read this article:
http://en.wikibooks.org/wiki/X86_Assembly/Bootloaders
From the above URL, BIOS (which is effectively PC hardware) will make the jump to memory at 0000:7c00 to continue execution in 16-bit mode.
And to quote from above:
A bootloader runs under certain conditions that the programmer must appreciate in order to make a successful bootloader. The following pertains to bootloaders initiated by the PC BIOS:
- The first sector of a drive contains its boot loader.
- One sector is 512 bytes — the last two bytes of which must be 0xAA55 (i.e. 0x55 followed by 0xAA), or else the BIOS will treat the drive as unbootable.
- If everything is in order, said first sector will be placed at RAM address 0000:7C00, and the BIOS's role is over as it transfers control to 0000:7C00. (I.e. it JMPs to that address)
So from bootup, if u want the CPU to start executing your code, it has to be located in memory at 0000:7c00. And this part of the code is loaded from the first sector the harddisk - also done by hardware. And it is only the first sector which is loaded, the remaining of other parts of the code then have to be loaded by this initial "bootloader".
More information here (on harddisk bootup sector and the 7c00 feature):
http://www.ata-atapi.com/hiwdos.html
http://www.ata-atapi.com/hiwmbr.html
Please don't confuse with the starting up mode of the CPU - the first instruction it will fetch and execute is at physical address 0xfffffff0 (see page 9-5):
and at this stage it is executing non-volatile (meaning you cannot reprogram it easily, and thus not part of bootloader's responsibility) BIOS code.
Update: 6 Oct 2015
But this BIOS code does have some variation (as highlighted by Michael Petch) - some BIOS will load from 07c0:0000 instead of 0000:7c00, and as extracted from "grub" bootloader source code as well:
.globl _start; _start:
/*
* _start is loaded at 0x7c00 and is jumped to with CS:IP 0:0x7c00
*/
/*
* Beginning of the sector is compatible with the FAT/HPFS BIOS
* parameter block.
*/
jmp after_BPB
nop /* do I care about this ??? */
after_BPB:
/* general setup */
cli /* we're not safe here! */
boot_drive_check:
jmp 1f
testb $0x80, %dl
jnz 1f
movb $0x80, %dl
1:
/*
* ljmp to the next instruction because some bogus BIOSes
* jump to 07C0:0000 instead of 0000:7C00.
*/
ljmp $0, $ABS(real_start)
Upvotes: 2
Reputation: 1761
What the ORG pseudo instruction does is to tell the assembler to add this offset to all absolute addresses mentioned. For example if you write "MOV AX,my_string" and my_string gets located 1234 bytes into the code, then the assembler generates "MOV AX,7c00h+1234". Less commonly it is also used the other way around to calculate a relative address (such as for a short jump) from a an absolute address given.
Upvotes: 2
Reputation: 2531
You have not given any context for this question. But I will attempt to give some form of answer.
When the program is executed after being loaded into memory the program must be located somewhere, imagine this is address 7C00. The processor starts excuting somewhere, in your case most likely 7C00. The ORG statement is telling the assembler that addresses for the following instructions will appear at this address.
Why not 0? Well, your processor may want other things at those addresses, ie. interrupt vectors.
The datasheet for your processor will give you more information on its startup sequence.
Good luck.
From a quick goodle...
http://f.osdev.org/viewtopic.php?f=1&t=9543
Upvotes: 0