Segmentation in 8086
Segmentation:
Segmentation is a solution to a problem. It is a very important topic. If you can’t understand segmentation you can’t understand architecture. 8086 microprocessor has memory size is 1MB i.e. 2 rise to 20. Thus we use a 20-bit physical address (actual address) to satisfy 1MB memory with unique addresses. The unique address of every memory location is called a physical address. Every memory location should have a unique address. The physical address is 20 bit, and it is not a byte compatible number. 20 bit would be 2 and half bytes, which means half a byte, will be wasted in every instruction. Thus there will be a massive wastage of memory because of the remaining half byte.
Why we didn’t need segmentation in 8085?
The thing is you want a 20-bit address bus because you want to use 1MB memory. You also want to work with a 16-bit address bus because it is byte compatible. This problem didn’t aroused in 8085(previous microprocessor) because 8085 has a 16-bit address bus, which can address up to 64KB. We know, a 16-bit address is a byte compatible number. 8086 has 1MB memory so that we have more space.
Virtual Address:
If you have a 20-bit address bus then you are bound to have a 20-bit address but 20 bit is not byte compatible. To solve this we use16-bit address which is named virtual address so that we don’t have to use a physical address. The virtual address is a combination of segment address and offsets address. The segment address shows the segment you want to access. Offset address is the distance from the starting of a segment to the location you want to access.
PHYSICAL ADDRESS = SEGMENT ADDRESS X 10H + OFFSET ADDRESS
For example, if we have segment address 1000 and offset address as 2345. Physical address will be:
Physical address = 1000 X 10 + 2345
Physical address = 12345H
Now when you are programming, you will give the segment address once and then you give only the 16 bit offset address. As both segment address and offset address is byte compatible number, the programmer can easily use it. So, we never use physical addresses in our program. Offset address is limited from 0000 to FFFF.
Segmentation solves another problem, i.e. overriding-
8085 didn’t have segmentation because it wasn’t necessary. We could have carefully programmed and avoided the problem. The problem started with 8086 processes that have large memory. No human being can remember 2 raise to 20 memory locations, it is bound to be overridden.
Program is stored sequentially, i.e. one after another. Stack is stored in LIFO (Last in First out) form. Data is stored anyhow. It can be stored sequentially, randomly or in LIFO form. This can cause overriding, even if we are extra careful. To solve this problem, they came with the idea of dividing the memory into various sections. These sections are called a segment. The added advantage of segmentation is that it solves the problem of overriding.
8086 is divided into 4 segments i.e. Code, Stack, Data and Extra segment. Now when the data will be stored in code segment it will be in sequential order from 0000 to FFFF. The stack segment will be stored in the last in first out order. In stack, the data will be stored from FFFF to 0000 and when it comes to 0000 it will again start from FFFF. So there is no chance of overriding to Code segment or data segment. The data will be stored in the desired segment and not exceed other segments.
This gave birth to the concept of files and folders. Files and folders are modern versions of segments. Files cannot override because they are virtually bound. This not only stops overriding but also organizes the data.
Advantages of Segmentation-
- Prevents Overriding
- We can access 1MB memory using a 16 bit offset address.