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.
BITS 16 ;put in 16 bit mode
ORG 0x7c00
; next line serves purpose other than to satisfy legacy requirement of some machines that require first instruction to be a short jump
jmp short $ + 2 ; or jmp short init ; or jmp short 0x7c02
init:
mov ax, 0 ;getting ready to zero some registers
mov ss, ax ;zero the SS
mov sp, 0x7c00 ;set the beginning of the stack to free memory before 0x07c00
mov ds, ax ;set DS to zero
mov es, ax ;set ES to zero
JMP 0x0000:start ; serves no purpose other than to set CS to zero in the correct manner
start:
...
TIMES 510 - ($ - $$) db 0
DW 0xAA55
Of course others may be willing to point out why you wouldn't want DS or ES to 0 but to be set to some other value. So don't use this if you don't fully understand why DS or ES should be zero.
If you don't use a bpb for fat filesystem you dont need the short jump there. To save a byte use xor ax, ax instead of mov ax, 0, which does the same but requires less bytes.
Last edited by Techel on Tue Dec 22, 2015 7:22 am, edited 1 time in total.
Roflo wrote:If your dont use a bpb for fat filesystem you dont need the short jump there. To save a byte use xor ax, ax instead of mov ax, 0, which does the same but requires less bytes.
I thought we said that certain old BIOS requires first instruction to be short jump
Roflo wrote:If your dont use a bpb for fat filesystem you dont need the short jump there. To save a byte use xor ax, ax instead of mov ax, 0, which does the same but requires less bytes.
Right you are. Just tried this. It does indeed save a byte. xor takes two bytes, mov takes three. Very good. Like it.
BITS 16 ;put in 16 bit mode
ORG 0x7c00
; next line serves purpose other than to satisfy legacy requirement of some machines that require first instruction to be a short jump
jmp short $ + 2 ; or jmp short init ; or jmp short 0x7c02
init:
mov ax, 0 ;getting ready to zero some registers
mov ss, ax ;zero the SS
mov sp, 0x7c00 ;set the beginning of the stack to free memory before 0x07c00
mov ds, ax ;set DS to zero
mov es, ax ;set ES to zero
jmp word 0x0000:start ; serves no purpose other than to set CS to zero in the correct manner
start:
...
TIMES 510 - ($ - $$) db 0
DW 0xAA55
Small improvement: use of jmp word for far jump makes the assumption that 16bit offset will be supplied more explicit.
Roflo wrote:If your dont use a bpb for fat filesystem you dont need the short jump there. To save a byte use xor ax, ax instead of mov ax, 0, which does the same but requires less bytes.
I thought we said that certain old BIOS requires first instruction to be short jump
I may be wrong, but I believe that old BIOSes sometimes required only that the first byte of the disk was not 0x00 as that was assumed to be an invalid instruction (actually it's an opcode for some addition operation, but it was assumed that no bootloader would ever start by performing addition without initialising anything first, and 0x00 was a nice value to choose which very conveniently happened to come out as an addition operation); I don't remember any requirement for the boot sector to start with a short jump. In any case, all modern BIOSes are supposed to use the 0x55AA (or is it 0xAA55???) signature to determine if a disk is bootable.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.