Page 1 of 1

NOP instruction

Posted: Wed Mar 14, 2012 6:02 am
by GraveMind
Hi,

Trying to get myself more acquainted with bootloaders and x86 encoding/system programming. Am I correct in thinking that the use of a NOP before a OEM parameter block would be done so the first executed instruction was aligned to an even byte in memory?

I think I counted this correctly, with the OEM block being 41 bytes. If NOP was not there the first instruction executed would be 43 bytes after but is instead 44 bytes.

If not then any ideas? Reading the bootloader code from MikeOS which states:

Code: Select all

jmp short bootloader_start	; Jump past disk description section
nop				; Pad out before disk description
OEMLabel		db "MIKEBOOT"	; Disk label
BytesPerSector		dw 512		; Bytes per sector
SectorsPerCluster	db 1		; Sectors per cluster
ReservedForBoot		dw 1		; Reserved sectors for boot record
NumberOfFats		db 2		; Number of copies of the FAT
RootDirEntries		dw 224		; Number of entries in root dir
LogicalSectors		dw 2880		; Number of logical sectors
MediumByte		db 0F0h		; Medium descriptor byte
SectorsPerFat		dw 9		; Sectors per FAT
SectorsPerTrack		dw 18		; Sectors per track (36/cylinder)
Sides			dw 2		; Number of sides/heads
HiddenSectors		dd 0		; Number of hidden sectors
LargeSectors		dd 0		; Number of LBA sectors
DriveNo			dw 0		; Drive No: 0
Signature		db 41		; Drive signature: 41 for floppy
VolumeID		dd 00000000h	; Volume ID: any number
VolumeLabel		db "MIKEOS     "; Volume Label: any 11 chars
FileSystem		db "FAT12   "	; File system type: don't change!
Thanks,
Jim

Re: NOP instruction

Posted: Wed Mar 14, 2012 6:14 am
by Yoda
NOP is here because the BIOS Parameter Block (BPB) must have definite placement (yes, alignment aware). NOP is inserted in case the first instruction is short jump, so that totally there should be 3 bytes. If you use near jump, there won't be NOP in that place.
You may ask, if the NOP may be replaced by any other byte? The answer is "no". According to an official FAT documentation NOP must be there in the case of short jump used. And many programs rely on that fact to detect that this is the FAT12 file system.

Re: NOP instruction

Posted: Wed Mar 14, 2012 6:42 am
by GraveMind
Yoda wrote:NOP is here because the BIOS Parameter Block (BPB) must have definite placement (yes, alignment aware). NOP is inserted in case the first instruction is short jump, so that totally there should be 3 bytes. If you use near jump, there won't be NOP in that place.
You may ask, if the NOP may be replaced by any other byte? The answer is "no". According to an official FAT documentation NOP must be there in the case of short jump used. And many programs rely on that fact to detect that this is the FAT12 file system.
http://wiki.osdev.org/FAT#BPB_.28BIOS_P ... r_Block.29

Great, that clears it up completely :-), thanks!

Re: NOP instruction

Posted: Thu Mar 15, 2012 9:18 am
by qw
The [url=http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/fatgen103.doc]Microsoft Extensible Firmware Initiative FAT32 File System Specification[/url] wrote:Jump instruction to boot code. This field has two allowed forms:
jmpBoot[0] = 0xEB, jmpBoot[1] = 0x??, jmpBoot[2] = 0x90
and
jmpBoot[0] = 0xE9, jmpBoot[1] = 0x??, jmpBoot[2] = 0x??
The first being the short, the latter being the near jump.