4

If this bit is zero, then memory is addressed byte by byte? And if it is 1, then memory is addressed 4Kb by 4Kb?

So for example, if this bit was set to 0, and i addressed memory location a000h, then i would be addressing the byte at that location right? And if i addressed the next location a001h, then that would be the next byte up in memory right?

But if this bit was 1, will i be addressing 4Kb chunks?

So would addressing a000h give me a 4Kb chunk, and a001 the next 4Kb up in memory?

user247702
  • 23,641
  • 15
  • 110
  • 157
kbzombie
  • 322
  • 2
  • 12

1 Answers1

8

The answer by kbzombie is almost correct. Various resources on the internet make the same mistake actually, likely because one copies the other.

When the granularity bit is set, the limit is indeed shifted left by 12 bits, but it's important to note that one-bits are inserted. So 0xfffff results in a limit of 0xffffffff, and 0x00000 results in a limit of 0x00000fff.

The 80386 Programmer's Reference Manual has this to say:

6.3.1.2 LIMIT CHECKING

The limit field of a segment descriptor is used by the processor to prevent programs from addressing outside the segment. The processor's interpretation of the limit depends on the setting of the G (granularity) bit. For data segments, the processor's interpretation of the limit depends also on the E-bit (expansion-direction bit) and the B-bit (big bit) (refer to Table 6-2).

When G= 0, the actual limit is the value of the 20-bit limit field as it appears in the descriptor. In this case, the limit may range from 0 to 0FFFFFH (220 - 1 or 1 megabyte). When G= 1, the processor appends 12 low-order one-bits to the value in the limit field. In this case the actual limit may range from 0FFFH (212 - 1 or 4 kilobytes) to 0FFFFFFFFH (232 - 1 or 4 gigabytes).

Table 6-2

user247702
  • 23,641
  • 15
  • 110
  • 157
  • It's actually even simpler than that: as the name implies *granularity* simply selects the measurement unit of the *limit* field. *G* = 0: use bytes. *G* = 1: use 4KiB. The rest (the shift, the expand down, the min size) are all either independent of the granularity or just implementation details. – Margaret Bloom Sep 20 '17 at 22:07
  • @MargaretBloom I found the "use 4KiB" description to be a bit confusing, since it implies that a limit of `0x00000` actually means 0. Isn't the implementation of the shift an important detail then? Admittedly, I still have to reach the point where I implement paging, so I have a limited view on things. – user247702 Sep 20 '17 at 22:12
  • 1
    Yeah, you are right. It's better seen as a shift, though the modern version of the manual says that the lower 12 bits are not tested. It's amazing how Intel can make confusing statements on such a basic topic! – Margaret Bloom Sep 20 '17 at 22:17
  • @Stijn, the limit of 0x00000 (with G=1) means that the highest 4 KB page that is within the segment limit is page 0. – prl Sep 21 '17 at 03:31