‫Let's now talk about the segmentation memory model in the segmentation memory model, memories accessed
‫by a segment and an offset due to the nature of the segmentation memory model programs can be loaded
‫in different areas of memory, but run without any problems.
‫Multiple segments are available for the use of segment registers.
‫Let's now explain a little more about what I've just described here in real mode, or the 086 intel
‫processor.
‫You have what's known as segment registers.
‫So we have the code segment, the STACK segment, the data segment and the extra segment.
‫These are the only segment registers you need to concern yourself with.
‫We use segment registers as a way to help us access memory in realtime.
‫With the use of a segment register and an offset, we can calculate an absolute position in RAM where
‫a particular piece of data is you do this by taking the segment register, such as the data segment,
‫the extra segment and so on.
‫And then you multiply it by 16, you then add the offset.
‫This is the absolute position in RAM that a particular byte is that through the combination of the second
‫register and our offset, we get an absolute position.
‫It's this combination that allows us to address up to one megabyte of RAM in real mode, even though
‫registers can only hold sixty five thousand five hundred thirty five.
‫Let's do a little example.
‫Let's pretend our code segment is at zero seven six zero.
‫And our assembly programs origin is zero, if you remember, in the Hello World tutorial, we went all
‫zero seven six zero zero, right.
‫Let's pretend we now set it to zero.
‫This will mean that, one, your assembly program is assembled.
‫It will offset all of the labels by zero rather than zero seven zero zero, as it was shown in the Hello
‫World tutorial.
‫So let's assume we have one instruction in our program, right?
‫This very first instruction is that origin zero.
‫So our offset is zero.
‫So to calculate the absolute position of the first instruction in our program, we do zero seven zero
‫multiplied by sixteen.
‫That gives us zero seven zero zero.
‫And then we plus our instructions offset, which is zero, which gives us an absolute offset of zero
‫seven six zero zero.
‫That is where our first instruction is.
‫So there are many different ways to make up an absolute address whilst using the segmentation memory
‫model, for example, to make the address 067 see if we could do it in a number of ways.
‫Our segment could be zero and our offset could be zero seven CFF.
‫That's one way our segment could be zero seven zero and our offset could be F.
‫That's another way a segment could be zero seven KfW and our offset could be zero zero F.
‫There are so many different combinations of addressing memory, less now do a calculation for segment
‫067 Sieff and offset 004.
‫So we do 067 KfW multiply by 16, which gives us CEREC seven.
‫KfW zero.
‫And then we add on the Zuckoff, which gives us 067 CFF different instructions in the assembly instructions
‫said, use different register combinations.
‫For example, the Lord Instruction uses our data segment as our segment register and the I register
‫as our offset register.
‫Let's take our example program here.
‫Our origin is zero, so our assembler will treat labels as offsetting from zero.
‫We move zero seven zero into the actual register.
‫We then move the register into the data segment register.
‫Our data segment has now been changed to zero seven zero.
‫We then move the address CLX one F into our Assai register.
‫Now, in real life, this would be a label in your assembly program pointing to a message, something
‫like that, right.
‫As we did in the hallowed example program.
‫We then call Lawder Speed.
‫So let's now see what happens.
‫The process that takes our data segment value, which is zero seven zero and multiply that by sixteen
‫to give us zero seven six zero zero.
‫We then add on the SC register, which is zero one F load.
‫SPE now knows that the bike that it needs to read is that address zero seven C one F.
‫This is what it looks like in the manual.
‫Programs can be loaded in different areas of memory but run without problems.
‫So I want you to imagine that we have two programs loaded into memory.
‫Both were assembled with the origin being zero.
‫Program one uses Segment zero seven zero for all its segment registers.
‫So that's the data segment, the extra segment stack segment, everything program one is loaded and
‫address zero six seven six zero zero.
‫Hence the reason why our segment is zero seven zero program to use this segment zero seven zero.
‫For all of its segment registers, can you guess what address is loaded at?
‫You got it, zero seven zero zero, we can swap our segment registers when switching to the other process.
‫We restore all the registers of the process, we are switching to this, then leads our program to resume
‫as if we never switched process at all.
‫Multiple segments are available through the use of segment registers.
‫Take this assembly instruction, for example.
‫Move by Al is 32.
‫So this is our extra segment.
‫So the the absolute offset in RAM that it will get won by the data from will be the extra segment multiplied
‫by sixteen plus decimal 32.
‫We can do the same for the data segment or the stack segment.
‫Let's now describe the stack segment margin.
‫We said the stacked segment to zero.
‫We now set our stack pointed to zero seven zero zero before continue in note that the stack pointer
‫points to a place in memory and when we do stack operations, it acts on the address that we provided.
‫The address that we provide is based on the stack segment and the stack pointer.
‫The absolute address is calculated by the STACK segment multiplied by sixteen plus the stack pointer.
‫Let's now continue so I can explain this further.
‫Let's imagine we run the instruction push zero x five.
‫So this will obviously push some data to the stack, as I'm sure you're aware in your windows.
‫Thirty two programs or your Linux programs.
‫When you've written assembler, you have to use the stack at one point, right.
‫So what happens here is a decrements.
‫The stack pointer by two stack pointer then becomes 067 BFE because with the combination of the stack
‫segment and the stack pointer stack point of zero seven zero zero.
‫Right when we push it becomes 067 BFE.
‫OK, we then set the bytes zero seven BFE and 067 B F to zero if.
‫Because we push our word here, right?
‫As for FCI, right, that a Pushtuns of those two bites says it's important, you understand that's
‫how the stack works.
‫And obviously when we pop, it does the opposite.
‫Right?
‫See, I hope that briefly explains the stack.
‫We're not going to explain the stack too much because you should already know this being an assembly
‫programmer.
‫However, I did want to show you the stack segment in the stack pointer because you might not be as
‫familiar with those things.
‫So, yeah, that is how it works in real mode.
‫And obviously, because we're in 16 bit mode, that's why we're working with 16 bit numbers, two bytes
‫at a time.
‫You know, in a 32 bit system, our push would push for bytes to the stack because we're in a 16 bit
‫system.
‫Only two bytes are pushed.