# Protected mode and Real mode

Real mode and Protected mode are two kind of status of CPU. In different status, the CPU will have different addressing methods.

## Real mode

In the early 8086CPU, there is only one kind of mode, the real mode. In real mode the date bus has only 16-bit and address bus has 20-bit. So the max size of each segment is $2^{16}=64$KB, and the maximum addressing range is $2^{20}=1$MB. So in practice, we use the “Segment base address + segment offset” to find the address. The segment base address is a 16-bit sgement register shifting 4 bits to left.

In this case, 1MB memory is devided into $2^{16}=65,536$ segments and each segment is $2^4=16$ bytes. For example, when we turn on the computer, the CS (code segment register) will be initiated as 0xFFFF, and IP (instruction pointer register) will be set as 0x0000. By shifting the CS 4 bits to the left and plus the IP, we will get CS:IP and at this time, CS:IP points to 0xFFFF0, which means the computer will run from this address.

## Protected mode

After entering the protected mode, all of the data bus, address bus and other registers of the CPU are 32 bits. Compared to the real mode, the addressing method has changed a lot. The protected mode uses the “segment selector + segment offset” to find the address. The structure of segment selector is shown below:

So there are at most $2^{13}$ segment selectors. In protected mode, as the segment offset is 32 bits. So, in theory, the maximum addressing range is $2^{13}\times 2^{32} \times 2=64$TB (the last two comes from the GDT and LDT). Through the segment selector we can find its segment descriptor, the structure of segment descriptor is shown below:

From segment descriptor, we can find a 32-bit base address, and then we plus the base address with a 32-bit segment offset (note: directly add up the two 32-bit register, not connect them into a 64-bit address). Then we can get the final 32-bit address. While Paging is not enabled, this address is physical address.

The advantage of this segmentation is that every process, has its own segment descriptor and memory space, which can strengthen the security. In some special cases, different process can share the same segment descriptor.

Directory