confusion about segment offset addressing

If you are new to development, plan on spending some time here before visiting the other forums.

Moderator:Moderators

Post Reply
brainbarshan
Posts:9
Joined:Fri Feb 19, 2010 12:58 pm
confusion about segment offset addressing

Post by brainbarshan » Fri Feb 19, 2010 1:20 pm

hello...i am new to this site
Here is my query:
In Tutorial 4 of Operating System Development series (Bootloaders 2) there is a portion in segment offset addressing

Code: Select all

base address = base address * segment size (16) + offset
			07C0:0000 = 07C0 * 16 (decimal) + 0
                                  = 07C00 + 0 = 0x7C00
And in tutorial 5 at the Reading and loading a sector part:

Code: Select all

.Read:
	mov		ah, 0x02				; function 2
	mov		al, 1					; read 1 sector
	mov		ch, 1					; we are reading the second sector past us, so its still on track 1
	mov		cl, 2					; sector to read (The second sector)
	mov		dh, 0					; head number
	mov		dl, 0					; drive number. Remember Drive 0 is floppy drive.
	int		0x13					; call BIOS - Read the sector
	jc		.Read					; Error, so try again

	jmp		0x1000:0x0				; jump to execute the sector!
i think according to segment offset addressing 0x1000:0x0 should be converted to address 0x10000. But if that happens it is going to be copied at 1MB th position of RAM. please correct me. i am confused.

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: confusion about segment offset addressing

Post by Andyhhp » Fri Feb 19, 2010 2:51 pm

You are correct in that "jmp 0x1000:0x0" will jump to the 1Mib'th byte in memory and start executing code.

What is confusing about this?

~Andrew
Image

brainbarshan
Posts:9
Joined:Fri Feb 19, 2010 12:58 pm

Re: confusion about segment offset addressing

Post by brainbarshan » Fri Feb 19, 2010 4:55 pm

we are yet in real mode. how can we cross the 1mb th limit? will not it generate a fault? that is my confusion.

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: confusion about segment offset addressing

Post by Andyhhp » Fri Feb 19, 2010 5:36 pm

Wait sorry - i was wrong - i was out by a factor of 4. (i cant count my 0's)

0x1000:0x0 will map to linear address 0x10000 which is 65536 in decimal.

the memory address 1MiB is 0x100000 (notice the extra 0) which is 1048576 in decimal.

In practice, if you havnt enabled the A20 line, writing data to the 1MiB mark, it will loop around memory and overwrite the Interrupt Vector Table, causing all futher interrupts to execute arbitrary instructions/data which you have just loaded there.

However, in terms of legallity, its perfectly fine. The reason that 16bit mode is thought to only have 1MiB of valid memory is that you have a segment (16 bits) which is multiplied by 0x10 or 16 (base 10) which is 4 bits, making the theoretical memory range be 20 bits in total. This was true for very very early processors which only had 20 bits on the address line.

However, from the 386 onwards, processors have had 32 or 64 bit address lines.

Now think back to segment:offset. the linear address is segment*16 + offset.

Take segment to be 0xFFFF which is a valid segment address, and take offset to be 0xFFFF which is also a valid offset address.

in this case, the linear address pointed to by 0xFFFF:0xFFFF is 0x10FFEF which is quite definatly above the 1MiB mark
Image

brainbarshan
Posts:9
Joined:Fri Feb 19, 2010 12:58 pm

Re: confusion about segment offset addressing

Post by brainbarshan » Fri Feb 19, 2010 6:05 pm

thank you

Post Reply