Page 1 of 1

Improved safe init for boot sector code

Posted: Tue Dec 22, 2015 6:51 am
by 0b00000000
OK, so putting everything together from various sources and recent discussions here's what we have for a safe initiation for boot sector code:

Code: Select all

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.

Re: Improved safe init for boot sector code

Posted: Tue Dec 22, 2015 7:05 am
by Techel
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.

Re: Improved safe init for boot sector code

Posted: Tue Dec 22, 2015 7:14 am
by 0b00000000
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

Re: Improved safe init for boot sector code

Posted: Tue Dec 22, 2015 7:16 am
by 0b00000000
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.

Re: Improved safe init for boot sector code

Posted: Tue Dec 22, 2015 7:55 am
by 0b00000000

Code: Select all

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.

Re: Improved safe init for boot sector code

Posted: Wed Dec 23, 2015 3:54 pm
by onlyonemac
0b00000000 wrote:
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.