Introduction to Memory Segmentation in 8086
Memory segmentation addresses the problem of managing large memory spaces in the 8086 microprocessor, enabling programmers to handle programs and data securely and efficiently. For foundational concepts, see Understanding Computer Memory: RAM, ROM, HDD, SSD, and More.
The Memory Problem Before Segmentation
- Prior microprocessors like 80085 lacked segmentation, making it difficult to manage large memory.
- 8086 accesses 1 MB memory, where programs (code) grow sequentially downward, stacks grow upward, and data grows unpredictably.
- Without segmentation, code, data, and stack areas can overlap, leading to potential memory override errors.
What is Segmentation?
- Segmentation divides memory into four types of segments:
- Code Segment (CS)
- Stack Segment (SS)
- Data Segment (DS)
- Extra Segment (ES) for additional data
- Segments are defined by the programmer but managed dynamically by the microprocessor to prevent overlaps. Understanding this concept is enhanced by reading Understanding Minimum Mode Operation of the 8086 Microprocessor.
Advantages of Segmentation
- Memory organization: Code, data, and stack reside in separate segments, avoiding overrides.
- Compatibility: Allows use of 16-bit addresses (segment + offset) instead of a single 20-bit physical address, improving instruction size compatibility.
- Foundation for modern files: Segments conceptually led to file systems where files (segments) do not override each other.
Physical vs Virtual Addressing
- Physical Address: Real 20-bit address (required to address 1 MB memory), awkward and incompatible directly with 16-bit instruction formats.
- Virtual Address: Combination of two 16-bit addresses:
- Segment address (indicates the start of a segment)
- Offset address (distance from segment start to desired location)
- Segment addresses are set once; offset addresses are used continuously. For a broader understanding of address computation, refer to Understanding the Register Modifier in Memory Hierarchy.
Address Calculation
- Physical address calculation formula:
Physical Address = (Segment Address 16) + Offset Address - Example: If CS = 1000h, IP = 2345h, physical address = 10000h + 2345h = 12345h.
Segment Size Constraints
- Maximum segment size: 64 KB (due to 16-bit offset addressing).
- Minimum segment size: 16 bytes (segments start at addresses multiple of 16).
Segment and Offset Registers
- Segment Registers (16-bit): CS, SS, DS, ES holding starting addresses of respective segments.
- Offset Registers (16-bit):
- IP (Instruction Pointer) for code segment
- SP (Stack Pointer) and BP (Base Pointer) for stack segment
- SI (Source Index) for data segment
- DI (Destination Index) for extra segment
Stack Management
- SP points to the top of the stack (used for push/pop).
- BP enables random access within the stack, enhancing usability.
Summary
- 8086 segmentation is essential for working efficiently with 1 MB memory using 16-bit instructions.
- Segmentation prevents memory overlaps between code, data, and stack.
- The processor dynamically combines segment and offset addresses to compute physical addresses.
- Understanding segment and offset registers is crucial for programming in assembly on 8086.
For an in-depth study, refer to the book "Microprocessors" by Vadaya, covering 8086 architecture, programming, and circuit diagrams with comprehensive details. Additionally, consider reviewing the Comprehensive Guide to Computer Architecture and RISC-V Overview for broader architectural context.
Hello people, welcome to my video. I'm Batara and today I'm going to teach you this topic called as memory
segmentation. Segmentation in 80086. Extremely important topic. You want to learn programming, you want to learn
instructions, you need to understand segmentation a lot. In fact, in many of my further videos, I'm going to keep uh
mentioning about this. If you don't know segmentation, you can't understand architecture, you can't understand
addressing modes. Yeah, you can mug them up, but you can't understand those topics. Okay, now very interesting
topic. You're going to enjoy learning it. I'll try to keep it as small and crisp as possible. Now, memory
segmentation is actually a solution to a problem. 80085, the predecessor to 8086. It didn't have a segmented memory. The
problem started with large memories. That's when segmentation is necessary. That's why we are learning it. Okay,
let's start. First, we'll understand what the problem is. Now, this is the memory accessed by 8086. I hope you
understand this is the processor. This is the memory. Memory is not inside the processor. It's a separate entity
altogether. 8086 can access a total of 1 MB memory. What do you do with memory? Memory is used to store. What do you
store in memory? You store programs. You store data. Okay. Programs are called as code. Data is called as data. How do you
store them? Programs are stored sequentially not randomly. Sequentially after every instruction the address goes
on getting incremented. So if the memory addresses are 0 0 0 0 0 up to fff fff f after every instruction the address will
get incremented. So the program will grow in this direction downwards. Similarly data data is stored anyhow you
can store it sequentially you can store it randomly. So it can increment upwards it can go downwards and in the memory
you also have the stack. I hope you know what is the stack. If you don't know, uh I have I'm going to be making a video
for push and pop. You can check them out. That video shows how stacks work. Anyways, so in the stack you store data.
This is also data. So is this. This is random unstructured random form of data. This is a structured form of data. The
simplest example of stack is your SMS folder. Your text folder inside your phone. All the SMSs that you get, they
are stored as a structure. The structure works in the manner last in first out. So if you store 1 2 3 you get back 3 2
1. That means it always gives you the latest data first because you want to see your latest SMS first. So that's one
example. There are various examples of stacks. Now so this is the stack. As you go on storing data in the stack, the
stack grows upwards. The address goes on getting decremented. So programs grow downwards, stack grows upwards. Data can
grow anyhow. Come on, look at this. You don't have to be a genius to understand. This is a recipe for disaster.
Eventually, they are all going to override on each other. No matter how much careful you are, no human being can
really remember what is stored in 1 million memory locations. Today, for instance, suppose you're in a in a frame
of mind to write programs. Today, you want to write programs. Sometimes it happens. You just get too excited. You
want to write lots of programs. You're going on writing code and you're very happy about it. What you're not
realizing is that you're overwriting on your own stack. Today you go on pushing data in the stack. The stack will
override on code. The worst culprit is data because it can go in every direction. So there's always a
possibility things might override. Now this used to happen in H085. But because the environment was very small, it could
be prevented by this careful programming with ED6. As the memory increased so much, it's virtually impossible for a
person to remember what's stored in so many memory locations. So they came up with this idea. Why not create sections
in the memory? Why not automatically prevent these overrides? That gave the birth to the concept of segmentation. So
you have a code segment, you have a stack segment, data segment and an extra segment. As the name suggests, it
contains extra data. Once your data segment is full, if you want to store more data, you store it in extra
segment. So as you can see, there are four segments. Now, who creates these segments? program. When do you do it?
When you start a program, take the print out or just take a look at any AD86 program, the first thing that you do is
you initialize your segments. Once you have done that, forget about it. The processor will see to it that they never
overwrite. Their segments are created by the programmer, but they are managed by the mut by the processor, by the
microprocessor. So once you created a segment, just go on writing as much code as you want to. It will stop here, but
will not come into the stack segment. stack will not go into code segment so on and so forth. So on the face of it,
the first apparent advantage of segmentation is that it prevents override. It organizes the memory. You
want to really understand, if you want to understand it superficially, it's done. If you want to really understand,
you know what you're learning. You'll be amazed to hear what I'm going to say. This is the birth of the concept of
files. Okay? I'm taking you back all the way into ' 70s. When computers started, there were no files and folders. there
was one raw memory when information was stored. Now in that case whatever you store you always worried whe whether I'm
overwriting or something else. Today that's not how the world works. Today in computers everything that you store is
stored in a file. Tell me do you agree or not? Now when you're writing information inside a file let's say it's
a word document. You're going on typing text into the document. You are never worried that this document will override
on the other document because files don't override on each other. What are these files? These files are nothing
else but the modern version of these segments. Nobody. It's not like somebody was dreaming in the night and realized
that's made files. Computers have evolved. The way you see them today is not how they were made initially. It's
evolution that has finally reached to the modern form of computing. So what you see as files today originally was
segments. It's this idea that created files. All your programs, all your applications are code segments. Each
application that you have is an independent code segment. All your data files like your songs, your videos, your
images, they are all data segments. So you have so many data segments, so many code segments in your computer. The
program you're running right now becomes the active code segment. The data that you're watching right now becomes the
active data segment. So again, it's the whole idea just like files don't override on each other. Segments don't.
Who creates files? We who manages them? The processor. We create the segments, but they are managed by the processor.
So coming back the first obvious advantage of segmentation is that it prevents overrides. So it organizes the
memory. But surprisingly this is not the reason why segmentation is done. You think something so simple would be such
a big question. No this is just an adding advantage. It's a byproduct. The real reason for segmentation is far more
crazy. Okay. For that you got to pay attention. It's in terms of time. Yeah it's a very small concept but it's a
very interesting concept to understand. I'm sure you heard the word virtual memory. You know inside computers you
use something called as virtual memory. Now you'll understand what that really means. Okay
look this is your physical form of memory. This is your 1 MB memory of 8086. As we just discussed the memory
has four segments created by us. Code segment stack segment data segment extra segment. They can be present anywhere in
the memory. Okay. We choose where we want them to be. Now to understand the whole concept of segmentation we take an
example. Suppose we look at the code segment. We look at one location within the code segment. Okay. How do you
identify every memory location? Every memory location must have its own unique address. Tell me, do you agree? So,
addresses have to be duplicate or distinct. Distinct. If they are duplicate, there's no point of having an
address. That unique address of every memory location is called its physical address. The real address. The actual
address is called its physical address. I'm stressing so much on this word. Please understand this is the real
address. Very soon we going to create a virtual address. Why? We bored of real addresses. No, there is a serious
problem. Look here. This physical address is of 20 bits. Why is it of 20 bits? Because the memory is of 1 MB. 1
MB is 2 to 20. So only if you use a 20 bit number you will satisfy 1 MB memory with unique addresses. Please tell me
did you understand this? If you use an 8 bit address listen carefully if addresses are 8 bits you will get 2 to
8 you will get 2 to 8. 2 to 8 is 256. So what will happen for 256 locations you will get unique addresses. then the same
set will repeat. Similarly, if you use 16 bit addresses, you will get 2 to 16 different addresses. 2 to 16 is
64k. 2 6 into 2 to 10 64 into 1k 64k. So if you had 64 KB memory, a 16 bit address would have been sufficient. But
here 64 KB after first 64 KB the same addresses will go on getting repeated. You don't want that either. If you take
a 17 bit address, still insufficient 18 still 19 still 20 perfect. Why? Because 2 to 20 is 1 MB. So if you want 1 M 1 MB
memory, you need to use a 20 bit address. That 20 bit address is called physical address. Example 1 2 3 4 5. As
you can see, this is a 20 bit number. I hope you know that 1 is 0 0 0 1 2 is 0 0 1 0. Each digit requires four bits. So
it's a 20 bit number. The good thing about physical addresses is that they are unique, which they should be. The
bad thing about physical address is its size. It's a stupid number. Okay. Uh 20 bits are not compatible with computers.
Why? 20 bits are 2 and 1/2 bytes. Tell me do you understand? One bite is 8 bits. Two bytes are 16 bits. 20 bits
means 2 and 1/2 bytes. So what happens? Any instruction if you try to write this address then instruction will have an
awkward half bite in it. All instructions are stored in the memory. In the memory one location can carry one
bite. If your instruction has a half bite, it will occupy half that location and the other half of that location will
get wasted. Now, will that happen only once? If it happens only once, who cares? Half bite has no value in terms
of money or in terms of anything in today's world. But it won't happen only once. It will happen every time. Every
instruction of every program, wherever you try to write this address, you'll waste half a bite of memory. So, in
totality, you'll have massive wastage of memory. So, I hope you understand the point I'm trying to make. You cannot
work with a 20 bit address. 20 bit is not bite compatible. So after this point we will never use the physical
address. Students say how did this problem come up? This problem was created by us because we have a 20 bit
address bus. 886 has a 20 bit address bus. If you have a 20 bit address bus, you will have a 20 bit address. Let's
reduce the address bus. Let's go back to ATD5 environment which had only a 16 bit address bus. If you have a 16 bit
address bus, you have 64 KB memory. Every address is 16 bits. We are happy. Why did we make a 20 bit address bus?
Because we want to access more memory. 64 KB is too less. We want to access more memory. So we made a memory of 1
MB. Now to access 1 MB memory, you need a 20 bit address bus. So we got a 20 bit address. But the problem is we also have
a 20 bit address. What do you want? 20 bit. Okay. Listen to this very carefully. The next 20 seconds is the
crux of this whole matter. You want a 20 bit address bus or a 16 bit address bus 20 bit. Why? Because you want 1 MB
memory. You want to work with a 20 bit address or a 16 bit address. 16 bit address because they are compatible. So
say these two sentences and you will understand the whole point. You want to work with a 20 bit address bus, but you
don't want to work with a 20 bit address. You want to work with a 16 bit address. Now, on the face of it, this
sounds absurd. If you're thinking what the hell is going on, you are you are at the same page where I want you to be. We
are at the same page now. Because this seems to be stupid. If you have a 20 bit address bus, you're bound to have a 20
bit address. That is the real address. We not going to use it anymore. We going to use a 16 bit address. Will that be
the real address? No. It'll be a virtual address. So, we are now going to create a virtual address. Why? Because we like
to do things in a funny way. No. Because 16 bit addresses are compatible. Using a 16- bit number can be written in
instructions, can be given in registers. They are perfectly bite compatible. Everything is back to our comfort zone.
So, now we're going to abolish using physical addresses and create a virtual address. A virtual address is a
combination of two addresses. They are called segment address and offset address. First thing first, both of them
are 16 bit numbers. Are 16 bit numbers compatible? Yes, we are back in our comfort zone. Now, what these two
addresses give? The segment address gives the starting address of the segment. Get this clear. You wanted to
access some location. That location is some location in some segment. I repeat some location in some segment like at
some part of some song, some part of some movie, some part of some word document which you want to access. Am I
right or not? So this is so and so location of so and so segment. First we give the segment address which
identifies the segment that you want to access. Then we give the offset address. The offset address is the distance from
the starting of the segment to the location you want to access. Are we clear? I repeat first we give the
segment address then we give the offset address I'll draw a simpler picture look here this is your 1 and memory you want
to access this location its real address is which address physical address do you want to give it no why because you have
some attitude problem no because it's a 20 bit number you can't give 20 bit number so what do you do you realize
this is basically some location of some segment so if you give the segment address and the offset address you can
still go to this location. Advantage of getting both these addresses are both of them are 16 bit number companies. 16-
bit numbers are compatible with computers. So that's what we have stopped using physical address. Instead
of that we have created a virtual address which is a segment address and an offset address. To give you another
simple example, there are tons of examples that a person can create. Assuming you are in some
college, if your college has 1,000 students and I give everybody a unique ID, you will have an ID, a number like
425 as an example. Now I can identify you by that number 425. Another way of identifying you, maybe you are role
number 25 in your class. So I can tell somebody I want to access someone from second year role number 25. Second year
rule number 25 is segment address and offset address. Second year is your segment
address which identifies the class where you belong belong and 25 within that is an offset address. So you are second
year role number 25. Now the point is I'm not in your class I'm outside. So I have to identify the class also and the
role number also. But if somebody is standing inside your class and is taking a roll call, an attendance call, that
person will not keep saying second year role number one, second year role number two, that person will only keep telling
the role numbers. What did you understand from this? Every time you don't have to give segment address and
offset address, segment addresses are given only once in the beginning of the program to identify where the segments
are located. Thereafter, what changes for all locations is only an offset address. So every time you give two
addresses or only one address, one address, which one offset address, how many bits is it? 16 bit. So what you
have achieved is something which was virtually impossible 10 minutes back. Till now we thought if you want to
access 1 MB memory, you have to work with a 20 bit address. Now we have created a virtual address which is two
addresses segment address and offset address. Segment addresses are given only once. What you give all the time is
just an offset address. So you do your whole programming using only 16 bit addresses that is offset address. This
is the whole reason why segmentation is done. Tell me, I hope you understood this. Now, just to test you, just to
test you, the predecessor to this 80085 did not have segmentation. Why? Why? Because its address bus was 16 bits. So,
its physical address itself was a compatible number. It did not need to create a virtual address. In
80086 is segmentation optional or compulsory? It is compulsory. If there was no segmentation, you would have to
work with 20 bit numbers, which means everywhere you would have that half bite problem. Now you can work unbelievably
you can work with a 1 MB memory using only 16 bit addresses. That is the offset address. So for the last time
there are three addresses physical address, segment address, offset address. Which address you will never
give? Physical address. Which address you give only once? Segment address that is in the beginning of the program to
identify the segments. Which address you give all the time in programming? Offset address. All your programming is done
only using offset addresses. Tell me did you understand this? Now you understood the concept. Now it's just a little of
formalities and we are done. Suppose we look at code segment. Okay. The first location in the code segment, the
beginning of code segment will have an offset address 0 0 0. Tell me did you understand this? Should it be five zeros
or four zeros? Four zeros because offset addresses are of 16 bits. Okay. Thereafter as you proceed ahead in the
segment for every location, what will remain the same? What will go on changing? The segment address will
remain the same. The offset address will go on changing. So listen, you're going on giving offset address. You're going
on giving offset address. You're going on incrementing it. Hear me out. Just hear me out patiently. Suppose today you
are in the mood for writing programs. You're very excited. You go on writing instructions. Programs has import
segment. You go on writing instructions. Which address going on incrementing? Offset address goes on incrementing.
Suppose you're not paying attention. Will code come in override on stack? Please answer this question. If it does,
then we are wasting our time. The whole idea of segmentation was to prevent overrides. Will it override? No. Why?
Because after a point of time you will run out of offset addresses. Get this clear. Offset address is only 16 bits.
That means it can only be from 000000 to fff. Which means you are confined in this much space. Even if you want you
cannot exceed this boundary. This is how segments do not override on each other. When I say code never comes into stack
it's not because there is a line drawn in the memory. Come on we are not fools. This these are virtual boundaries
created by a whole mechanism. I say again in case you're not paying attention offset addresses are limited.
They go from 000000 to fff. There cannot be an offset address less than 000 or more than fff. Which means whatever you
do if you're working on code segment you will be confined in this space. Same goes with stack. Stack as I said grows
upwards. So as you go on pushing your address will go on getting decremented. Which address? Tell me again. The
segment address or the offset address? The offset address will go on getting decremented. Suppose you're not paying
attention. Suppose you have been pushed by mistake in a loop, a big loop. Your stack will go on increasing ferociously.
Will it come and override on the code? No. Because there is no offset address less than 00000 or more than f. So
whatever happens, stack will be in stack segment. Data will be in the data segment. This is how files do not
overwrite on each other. They have virtual boundaries created by the system. Platinum. Did you understand? So
the two advantages of segmentation first we can work with 16 bit addresses second they never override on each other this
is why segmentation is done now as I said the concept is over just a few little bit of formalities remaining this
just a few minutes 0000 to fff is the maximum range of a segment this is 2 to 16 because you're using 16 bit numbers 2
to 16 is 2 to 6 * 2 to 10 that is 64 * 1 k that is 64 KB becomes the maximum size of a segment. Okay, pay
attention to my words. I didn't say every segment is 64 KB. 64 KB is the maximum size. Segments can be big, can
be small, can be very big, very small like files. They can be big and small. So in this system, the maximum size of a
segment is 64 KB. That comes because your offset addresses a 16 bit. So this is a range in which a segment can lie.
That's the maximum range. Okay. Now look okay this is your MU
886 inside the MUP there are registers. Okay if you want to understand the architecture first know this
point. They are called segment registers and offset registers. Segment registers are of 16 bits. Offset
registers are also 16 bits. CS, SS, DS, ES are called segment registers. Now what do they do? They contain segment
addresses. Offset registers contain offset addresses. For code segment, the segment address is given by a register
called as CS. For stack segment, SS. For data segment, DS. For extra segment, ES. For the last time, people get confused
over it. They think CS, SS, DS, DS are the segments. No, no, no, no, no. Segments are present
in the memory. This is the processor. These are just registers. These are not the segments. They give the addresses of
the four segments. Code CS register tells you where the code segment begins. SS tells you where the stack segment
begins. So on and so forth. Are you clear? Just like there are segment registers, there are also offset
registers. For code segment, offset register is called IP. IP stands for instruction pointer. I repeat,
instruction pointer. Now, why such a name? This is code segment. What do you store inside code segment?
Instructions. So, these are instructions. What does the processor do? Goes on fetching instructions one by
one. I'm sure you know that's the whole purpose of processor. Fetch, decode and execute instructions. Now, as it goes on
fetching instructions, the address goes on getting incremented. Which address? the segment address or offset address.
The offset address that means who goes on incrementing? IP. IP goes on pointing to the next instruction. The moment you
fetch an instruction, IP gets incremented. So it always points to the next instruction. So IP gives offset
address. CS gives a segment address. Then did you understand CS and IP? If you understand for this these this
segment you'll automatically understand for just pay attention for 2 minutes you're done. So CS gives a segment
address. I gives the offset address. Now look here suppose I tell you W CS is 1,000 so 1,000 is my segment address
valid yes because it's a 16 bit number H of course everything is X address I tell the MP IP is 2 3 4 5
again 16 bit number so I have given the MP the segment address and the offset address I have told the MP now go and
fetch this instruction I have given you segment address and offset address Go fetch the instruction for me. To go to
the memory, processor has to put the address on the address bus. Come on, this is the last point that you need to
understand. Address bus is of how many bits? 20 bits. So which address will the MUP put on the address bus? The segment
address, the offset address or the physical address? Let me ask you in a better way. The virtual address or the
real address? Absolutely. The real address. Virtual address is only for convenience. What is virtual address?
It's your folder name and your file name. In a computer, everything that you store is stored in a folder inside some
file. You give the folder name and file name and you expect the processor to open the file. You have chosen the
folder name. You have chosen the file name. They are not the real address. Don't be naive. You think the processor
is going to put the folder name on the address bus and look for that folder name or the file name. No, everything
has a unique physical address, the real address which you don't know. and neither are you interested in knowing
but that doesn't mean it doesn't exist. Every location has a physical address which is calculated by the processor.
You give segment address and offset address for the sake of convenience. It's a job the processor to combine the
two and generate a physical address. That is done by a small formula. Physical address is calculated as
segment address multiplied by 10 plus offset address. I say segment address multiplied by 10 plus offset address.
Segment address as in CS register multiplied by 10. Offset register as in IP register. So
CS is 1,000. IP is 2 3 4 5. 1,000 * 10 becomes 10,000 plus 2 3 4 5 becomes 1 2 3 4 5. This is how the processor
calculates the physical address. I repeat my words. Who calculates the physical address? the processor. You
don't calculate. You use your computer for so many years. You've never bothered to calculate physical address. But
everything has a physical address. It's calculated by the processor. You give segment address and offset address.
Processor combines the two. So once again, what's the formula? Segment address multiplied by 10 plus offset
address. Let's take a few examples. Suppose segment address is 1,000. Offset address is 5,000. Physical address will
be 1,000 * 10. That is 10,000 + 5,000 makes it 15,000. Tell me did you understand this segment address is 4,000
offset address is 1,000 physical address will be 4,000 into 10 40,000 plus 1,000 that is 41,000 so on and so forth let me
just clear with you a little bit more suppose CS is 1,000 what is the physical address of the starting of code segment
the physical address of the starting of code segment 1,000 * 10 that is 10,000 plus offset offset in the beginning is
0. So that means 10,000. So that means if CS is 1,000, the code segment actually begins from 10,000. If SS is
3,000, the stack segment actually begins from 30,000. Please tell me to understand this. Come on, it's too
simple. Suppose DS is 5,000, the data segment actually begins at 50,000. 5,000 into 10 50,000 plus 0. What will be the
ending address? 50,000 plus the last offset address. The last offset address is FF FFF. So the ending address will be
5 FFF. So bottom line if ES is 7,000 the range of extra segment will go from 70,000 up to 7 F FFF. Tell me did you
understand this? Now I'm going to make a point over here. They don't always have to be such
nice round numbers. They can also be random numbers. So let's take an example of a
random number. Suppose we coming to a point, okay? We're not just doing this for fun. Suppose DS is
5134. Then your data segment will begin at the location 513 4. Tell me, did you understand this? Did you understand
this? 5134 multiplied by 10, 51 4. Suppose DS is 5237. Your data segment begins at
52370. I'm taking a reverse example. Suppose I want to start and the program I want to start my data segment at 52 3
4 0. I will put 52 34 in the SL register and that's how I dictate that I want my data segment to begin at 52 3 4 0. Did
you understand this? One more example. Come on. Come on. Come on. I will start my data segment at
51370. I will put 5137. Did you understand this? Last question. I want to begin my data segment at 52
345. Not possible. A segment cannot begin at any location you want. Why? It has to begin at a
location which is a multiple of 10. Please tell me you understood this. How can I start the data segment at 52 345?
I cannot put 52 34.5 inside. DS doesn't work. I'll have to put 5234 which means my data segment can begin at 523 4 0 or
it can begin at 52350 or it can begin at 52360 by looking 5236. So once I start a segment at 523 4 as an
example 523 4 0 the nearest location where the next segment can begin is 52350 which means the minimum gap
between two segments will be of 10 h bytes. 10 h bytes that is the minimum size of a segment. point to understand
over here. Maximum size of a segment is 64 KB. Similarly, segments have a minimum size. If anybody did not
understand, look here. If I want to begin a segment at 52 3 4 0, I will put 5 2 3 4 inside the
segment register. Tell me, did you understand this? So, I've started my segment over here. Now, I want this to
be the smallest segment possible. I want to store only one bite. I have stored one bite. I'm done. Now I want to start
the next segment as soon as I can. Can I start the next segment at 52 3 4 1? No, because I cannot put 52 3 4.1 over here.
Can I start the next segment at 42? No. 43? No. Blah blah blah. 49? No. Can I start at the next location? No, because
then will come 4 A. Remember the hex decimal numbers. 4 B 4 C 4 D 4 E 4 F. The nearest location where I can start
the next segment will be 5 2 3 5 0. So this becomes the minimum size of a segment. These are 16
locations. There are 10 h 10 hexa decimal is 16. So 16 bytes 16 locations one location by 16 bytes becomes the
minimum size of the segment and 64 kb becomes a maximum size of segment. Tell me did you understand this? Give me the
reason for both and I'll be happy. Maximum size of a segment is 64 KB because the range of offset addresses
goes from 0000 0 to FF that is a 16 bit number 2 to 16 is 64 KB. The minimum size of a segment is 10 H bytes. Why?
Because the formula for physical address is segment address multiplied by 10 plus offset. Initially offset is zero which
means every segment can only begin at locations which are multiple of 10. 10 H. 10 H is 16. Difference between this
location and this location is not of 10. of 16 if you see properly 4 0 up to 4 F. So this becomes the minimum size of a
segment. This becomes the maximum size. That's it. Now you just have to draw three more registers and you're
done. CS SSDs. What are they? They are segment registers. Similarly, there are offset registers. For code segment
offset register is called IP. For stack segment is called SP. SP stands for stack pointer. I say again stack
pointer. This is your stack. It grows like this. This is the top of stack. The top of stack is pointed by stack
pointer. The two things that you do on the stack are push and pop. Whether you do a push or you do a pop, it will
happen at the top of stack. The address will be given by stack pointer. Okay. Similarly for data segment the offset
register the register that gives the offset address is called SI. SI stands for source index. For extra segment, the
register that gives the offset address is called DI. Stands for destination index. Now hear this. Hear this. After
every instruction, IP will get incremented. After every push operation, SP will get decremented. After every
data operation, SI may get incremented or decremented. Same goes with DI. So what does that mean? Program still grows
downwards. Stack still grows upwards. Data is still random. But they are all confined in their own spaces. Though
they grow in different directions, they will never override on each other. Tell me. I hope you understood this. The last
piece of the puzzle. The last and final register. Every segment has one segment register and one offset register. Only
stack segments has two offset registers. They are called SP and BP. Now why does stack require two offset registers? So
easy to understand. Look here. Suppose this is the top of stack. The remaining elements of the stack are
here. This is stack pointer. As I told you, it always points to the top of stack. These are all empty locations.
Okay. Suppose I ask you push 20 numbers into this stack. Do this job for me. Please push 20 numbers into the stack.
Very obediently you start pushing first, second, third, blah, blah, blah. 20. As you go on pushing, SP keeps chasing the
top of stack. So by the time your 20 pushes are SP will point over here. Can we did you understand this? Now suppose
after pushing 20 numbers you realize you goofed up
problem. The second number that you push is wrong by mistake. You're a human. Humans make mistakes. So by mistake the
second number that you pushed is wrong. What do you do? What do you do? You can't go inside the stag and change
that. So what do you have to do? Pop out 18 numbers. Change that. Push back 18 numbers. Would you enjoy doing that? For
us it's a joke. For an 886 programmer, this was it. So, should there be a better way to use the stack? Yes, you
have two pointers for the stack. A stack pointer and a base pointer. Stack pointer always points to the top of
stack. Base pointer can point anywhere in the stack. Stack pointer is used for push and pop operations. Push and pop
still happen at the top. That's how it is a stack. That's how it works in the manner last. So, stack pointer is used
for push and pop. Base pointer BT stand for base pointer is used for random access of the stack. I repeat what
random access of the stack. You can read any data from a stack. You can modify the data from a stack. Tell me does this
make the stack far more usable. Uh if you've heard my adi architecture lecture, it's there in the playlist.
I've given more examples of the stack. Simplest example is your SMS folder. I think I just said it also some time
back. So tell me suppose right now you get an SMS. Okay. one, then you get a second one, then you get a third one,
then you get a fourth one. You're a popular person, you've got four SMSs. Uh when you want to check them out, you
open the SMS folder. Which message do you see first? The latest one. That means you understand this structure is a
stack. Last in, first out. Okay. So, this is the latest SMS that shows up first. Now, for some reason, you want to
check out this message first for any reason. I will not get into that. That's your personal call. You want to check
out this SMS first. Does your phone insist? No. First you have to go through these messages. No. You can scroll down
and check this message. That means you're using a second pointer. This is when you're using base pointer. Next
time you do that, tell in your mind this is BP. Base pointer has come into picture right now. Okay. Now tell me
when you're reading this message, these messages are still unread. You're reading this message. In the meantime,
you get a fifth message. You're very popular. You get a fifth message. Where does it come? It doesn't come sandwiched
between the two. It comes right on the top. That means it uses another pointer to get stored over here. That is your
stack pointer. So both your pointers are now coming into play. Stack pointer points to the top of stack and it's used
for push and pop. Base pointer can point to any element of the stack. So it's used for random access of the stack.
These are all the registers that take part in segmentation. If you want to understand architecture and programming,
you keep coming across these registers. So instead of being confused all the time, many students texted me to make a
video for this first. This makes you understand everything else. So I hope you understood it. Just one minute recap
and then we go ahead. Uh this is the microp processor. This is the memory. In the memory there are four segments. Code
segments, stack segment, data segment, extra segment. The maximum size of a segment
is 64 KB. The minimum size of a segment is 16 bytes. For port segment the segment address is given by CS. For
stack segment it's SS, DS and ES. The offset addresses are given by IP, SP, SI and DI. For stack segment, there are two
offset registers SP and DP. SP always points to the top of the stack. DB can point anywhere in the whole stack
segment. Okay, that's about it. Now, a detailed explanation of this with the full theory answer not only of this
topic. I keep saying this in every video. I'll keep doing it because this book is made out of a lot of love. It's
18 years of refinement. I've been teaching since 18 years. Uh so it contains everything that a person would
want to know in 1886 with diagrams, with explanation, with programming, with circuit diagrams, everything. The book
is there. It's called microcessors by Vadaya. It's there on Amazon. It's there on Kindle if you like the soft copy. Uh
get hold of it. You'll get much more than everything that is there. The book is 250 pages of hardcore technical
stuff. All of this cannot be covered in the videos. Impossible. Take more than 100 videos to do that. So whatever the
basic topics a person needs to understand 86. I'm making videos on those. The remaining whatever you would
like refer to the book. Feel free to contact me. My contact numbers are there in the book. Feel free to get in touch
with me. Thank you so much for watching my videos. Uh if you like the video, hit the like button. If you want to be
updated about more uh videos that I keep putting up, please hit the subscribe button. You know how things work on
YouTube. And uh any suggestions that you have, you most welcome. Thank you so much.
[Music]
Memory segmentation in the 8086 microprocessor divides the 1 MB memory into separate segments—code, stack, data, and extra segments—to manage programs and data securely and efficiently. This is important because it prevents overlap between code, data, and stack areas, allowing the processor to handle large memory spaces with 16-bit instructions while avoiding memory override errors.
The 8086 calculates a 20-bit physical address by combining a 16-bit segment address and a 16-bit offset address using the formula: Physical Address = (Segment Address × 16) + Offset Address. For example, if the Code Segment (CS) register is 1000h and the Instruction Pointer (IP) is 2345h, the physical address will be 10000h + 2345h = 12345h.
Each segment in the 8086 can be up to 64 KB in size due to the 16-bit offset limit. Segments must start at addresses that are multiples of 16 bytes to maintain proper alignment. The minimum segment size is 16 bytes, ensuring segment addresses align with the processor's addressing scheme.
The 8086 uses four 16-bit segment registers: Code Segment (CS), Stack Segment (SS), Data Segment (DS), and Extra Segment (ES) to hold the base addresses of respective memory segments. Offset registers include Instruction Pointer (IP) for code, Stack Pointer (SP) and Base Pointer (BP) for stack, and Source Index (SI) and Destination Index (DI) for data and extra segments.
Segmentation allows the use of two 16-bit addresses—segment and offset—instead of a single 20-bit physical address, making address handling compatible with 16-bit instruction formats. This approach reduces instruction size and complexity while still enabling access to the 1 MB memory space, which was not feasible with a flat 20-bit addressing scheme alone.
In the 8086 architecture, the Stack Pointer (SP) register points to the top of the stack and is used for push and pop operations to manage stack frames sequentially. The Base Pointer (BP) register allows random access within the stack, enabling more flexible referencing of parameters and local variables during program execution.
Heads up!
This summary and transcript were automatically generated using AI with the Free YouTube Transcript Summary Tool by LunaNotes.
Generate a summary for freeRelated Summaries
Understanding Static Variables and Memory Segments in C Programming
This detailed explanation explores the behavior of static variables in C, focusing on how their initialization affects storage in different memory segments like BSS and data segments. It also covers hexadecimal notation, format specifiers, and the memory layout of C programs, with practical examples and compilation insights for better comprehension.
Understanding Minimum Mode Operation of the 8086 Microprocessor
This video lecture explains the minimum mode operation of the 8086 microprocessor, detailing the control signal generation and data transmission processes. It provides a step-by-step guide for medical students and others interested in microprocessor functionality, including pin diagrams and control signals.
Understanding Computer Memory: RAM, ROM, HDD, SSD, and More
Explore the fundamental types of computer memory and storage, including RAM, ROM, HDD, SSD, and flash drives. Learn how each type functions, their advantages, and their role in computing performance.
Understanding the Register Modifier in Memory Hierarchy
Explore the concept of the register modifier in programming, its role in memory hierarchy, and how it optimizes variable access by hinting the compiler to use fast register memory. Learn why register variables improve performance and the compiler's role in managing them.
Understanding Operating System Structures: A Comprehensive Overview
This lecture delves into the various structures of operating systems, including simple, monolithic, layered, microkernel, and modular approaches. Each structure is analyzed for its advantages and disadvantages, providing a clear understanding of how operating systems are designed and function.
Most Viewed Summaries
Kolonyalismo at Imperyalismo: Ang Kasaysayan ng Pagsakop sa Pilipinas
Tuklasin ang kasaysayan ng kolonyalismo at imperyalismo sa Pilipinas sa pamamagitan ni Ferdinand Magellan.
A Comprehensive Guide to Using Stable Diffusion Forge UI
Explore the Stable Diffusion Forge UI, customizable settings, models, and more to enhance your image generation experience.
Mastering Inpainting with Stable Diffusion: Fix Mistakes and Enhance Your Images
Learn to fix mistakes and enhance images with Stable Diffusion's inpainting features effectively.
Pamamaraan at Patakarang Kolonyal ng mga Espanyol sa Pilipinas
Tuklasin ang mga pamamaraan at patakaran ng mga Espanyol sa Pilipinas, at ang epekto nito sa mga Pilipino.
How to Install and Configure Forge: A New Stable Diffusion Web UI
Learn to install and configure the new Forge web UI for Stable Diffusion, with tips on models and settings.

