Improved safe init for boot sector code

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.
Post Reply
0b00000000
Member
Member
Posts: 50
Joined: Sun Dec 20, 2015 4:00 pm
Libera.chat IRC: 0b00000000

Improved safe init for boot sector code

Post 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.
0x00
Techel
Member
Member
Posts: 215
Joined: Fri Jan 30, 2015 4:57 pm
Location: Germany
Contact:

Re: Improved safe init for boot sector code

Post 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.
Last edited by Techel on Tue Dec 22, 2015 7:22 am, edited 1 time in total.
0b00000000
Member
Member
Posts: 50
Joined: Sun Dec 20, 2015 4:00 pm
Libera.chat IRC: 0b00000000

Re: Improved safe init for boot sector code

Post 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
0x00
0b00000000
Member
Member
Posts: 50
Joined: Sun Dec 20, 2015 4:00 pm
Libera.chat IRC: 0b00000000

Re: Improved safe init for boot sector code

Post 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.
0x00
0b00000000
Member
Member
Posts: 50
Joined: Sun Dec 20, 2015 4:00 pm
Libera.chat IRC: 0b00000000

Re: Improved safe init for boot sector code

Post 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.
0x00
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Improved safe init for boot sector code

Post 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.
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.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Post Reply