Memory segmentation in 8086
How does files, folders store data in present computers. Data in one file doesn’t overlap with another file, even though they both are in one memory , is all because of memory segmentation in 8086.
Memory of 8086 has 4 segments
- Data segment
- Program/code segment
- Stack segment ( read more about stack )
- Extra segment
Data is the information collected while the programs are executing, and Data can be stored in random locations or sequentially in 1MB memory.
Program is stored in a sequential manner, because this should be executed sequentially downwards.
Stack is a type of data structure for first in last out storage type. Whenever information is being saved in stack, its size keeps on incrementing upwords.
Extra , this is used to store any other extra information which could be data, program or any other.
However the memory is not divided into these segments physically. Users can divide the segments according to their requirements and those segments are managed by the processor.
However in present day computers, the files and folders are of not fixed size, and the user can use them according to user needs.
Every memory has few segments for data ,Programs and stack. In general terms data is RAM, which means present data is stored, Program means ROM ,where programs are stored.
The 8085 processor could interface 64Kb of memory, Memory would get overwritten, but as it was small in size, over writing could be avoided by careful programming. But in present memory size ( in GBs and TBs ) , programmers cannot take care of each byte. This was one of the necessities that gave birth to memory segmentation.
The 8086 microprocessor has 20 address lines, which means 8086 can interface 1MB of memory. So every location in the memory has a unique physical address, which is a 20-bit ( 2½ byte) address. So whenever an instruction is given with a physical address, the size of that opcode is not rounded, it has ½ byte extra, which makes the other ½ byte in the same location to be wasted. If it is only one instruction, only 4-bits of memory is wasted. But this happens 1000s of times, which results in huge memory wastage.
As we cannot use physical addresses because of its 20-bit size, which means we are using memory for 24 bits ( 3 bytes ), but we are accessing only 2½ bytes only. So we create virtual addresses , which are of only 16 bits.
This is the 16-bit address of the starting location of every segment.
This is also a 16-bit address which is away from segment address. If Offset address = 0012H , it means add 12H to segment address. Offset address can be from 0000 to FFFF , which means the length of any segment could be a maximum of 65,535 locations in each segment.
Note: These two addresses are virtual, they aren’t real.
In any set of instructions, once we give segment address and then we work only with offset address, unless we need another form of memory.
We give virtual addresses to processors, but the processor converts the virtual address into a physical address and then fetching is done. However there are 20 lines, so fetching is done using 20-bits physical address. Processor puts the physical address on the buses, virtual address is for data saving and convenience.
PA = ( segment address x 10H ) + offset address
If segment address = 1265H and offset address = 1F30H
PA = 12650H + 1F20H
This is the actual physical address.
If physical address is 14250H then segment address = 1425H ( segment address = PA/10 )
If the physical address is 35637H , no segment address!!
It shows segment addresses should be a multiple of 10 only if it is a physical address.
If one segment starts with PA = 12450H , then the minimum gap after the next segment starts is at 12460H . Which means the minimum gap between two segment address locations is 10H which is 16 locations.
How does the user give a virtual address?
8086 internally has many 16-bit registers for this purpose.
It is known as code segment. User gives the 16-bit segment address for the code segment to the CS register.
This stands for instruction pointer. It is an offset address for the code segment.
PA = ( CS x 10H ) + IP
SS stands for stack segment, this register stores the stack segment address.
Push = entering the data into stack
Pop = removing the data from stack
There is only one opening of such a structure.
Eg of stack: SMS in mobile ( latest messages comes on top )
Social media feed.
SP stands for stack pointer , this register stores the address of the top of the stack. SP is a 16-bit register.
Before we PUSH into stack , we have to increment SP, or else we would overwrite on the existing data at SP address.
After POP operation, we have to decrement the value of SP. At the 07 location of internal RAM SP is located inbuilt. Programmers can change its location.
BP stands for Base pointer. There is one more 16-bit register only in case of stack. As seen above SP only points the top of the stack. If the user needs to access/overwrite the location which is not on the top, we have to POP one by one until we reach that location. To avoid the problem, register stores the address of the random location in the stack, and this address is given by the user.
It stands for data segment. This 16-bit register stores the address of the data segment.
Source index, this acts like an offset address pointer in the data segment of memory. It stores the offset address of the data segment. This is also a 16-bit register.
Extra segment is also a 16-bit register, that stores the segment address of an extra segment in memory.
Destination Index is a 16-bit register, this acts like an offset address pointer in the data segment of memory. It stores the offset address of the data segment.