3

I am going through the protected mode part of x86. I just learnt about GDT. Before, I have studied that to get into Protected mode( ie : Using all 32bit address line) A20 gate must be enabled. So, the code to enable A20 must be in 16bit right? Recently when I went through the wiki.osdev site, I found that the code to enable A20 is written in x86 assembly. X86 assembly produces 32bit opcodes that cannot be loaded in 16bit mode right?

please explain if possible. Thank you.

Panther Coder
  • 1,058
  • 1
  • 16
  • 43
  • there is both 16 bit and 32 bit x86 assembly. You can assemble x86 assembly for either mode. – fuz Jul 18 '16 at 18:24
  • The kind of code produced depends on the assembler and assembler directives and assembler flags as much as it does on the actual opcodes used. – David Hoelzer Jul 18 '16 at 20:26

2 Answers2

12

The 8086 addressing model contemplated a 16 bits segment and a 16 bits offset combined as segment * 16 + offset.
The minimum address is 000000h, the maximum one is 10ffefh.
While the latter is technically a 21 bits value, the CPU had only 20 bits of address bus, so the biggest address accessible was 0fffffh1

The addresses above 0fffffh simply wrapped around2, so 10ffefh is an alias for 0ffefh.
Some program began to rely on that.

When the 80286 came out it had 24 bits for the address bus.
An address like 10ffefh didn't wrap around any more.
Emulating the old behavior required too much transistors at the time (10ffefh cannot be masked with an AND) so the A20 mask was introduced.

As the name suggest the address line 20, the 21st bit, was ANDed with a specific bit of a specific register in the 8055/8042 chip.
The BIOS cleared that bit on startup, thereby forcing the 21st bit to zero, emulating the old behavior.

If you don't enable the A20, the 21st bit of every physical address will always be zero.


It is possible to enable the A20 in protected mode with a flat model, which is the closest thing to "32 bit mode", but it requires to be careful with placing the code in memory.

x86 assembly can be used equally for producing 16 or 32 bits code by just telling the assembler the target size.


1 Given by, for example, a segment of 0f000h and an offset of 0ffffh.
2 The 21st bit was simply discarded.
3 Simply put if you are writing 16 or 32 bits code.

Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124
  • 4
    Although it is implied in your answer I just wanted to say this for clarity. Without the A20 line enabled ALL memory regions starting on an odd numbered megabyte (1024k) boundary will map to the even numbered megabyte region just below it. The side effect of course is that half the available physical memory addresses will not be accessible. – Michael Petch Jul 18 '16 at 20:41
  • Can you explain in detail @Michael Petch? – Panther Coder Jul 19 '16 at 02:18
  • 2
    @PantherCoder Michael is saying that the range *100000h* - *1FFFFFh* will be mapped to *000000h* - *0FFFFFh*, the range *300000h* - *3FFFFFh* to *200000h* - *2FFFFFh*, the range *500000h* - *5FFFFFh* to *400000h* - *4FFFFFh* and so on. The ranges *000000h* - *0FFFFFh*, *200000h* - *2FFFFFh*, *400000h* - *4FFFFFh* and so on are unaffected. This is due the A20 being forced to zero, effectively leaving a bit out of the address (32 bits -> 31 bits) thus halving the address space. Take some random 32 bits and see what happens when you mask the bit 20. – Margaret Bloom Jul 19 '16 at 07:15
-2

The problem is that if the A20 Gate is closed the 20th address bit is set to zero. For example if you want to access address 0x000100ab you're actually access 0x000000ab or if you want to access 0x06570021 you're actually access 0x0656021, regardless of whether you're in protected mode or in real mode. The reason for this is that in real mode, you're could produce a physical address that takes more them 20 bits like 0x001000ab using e.g. the segment:effective address combination FFFF:00bb, but the 8086 truncated that to 0x000ab while a 80286 or newer would access 0x001000ab if the A20 Gate is opened, witch is bad for programs (like some BIOS codes) that relied on that wrapping around "feature".

Of course a closed A20 Gate messes up totally with you more them 20 bit address in protected mode witch is why you have to make sure it's opened if you switch to protected mode.

sannaj
  • 360
  • 2
  • 8