Thanks a lot Joel! This is a great explanation.<br>Just one more question. I used to think that the compiler always <br>assigns/generates starting from 0 as Mulyadi has also mentioned.<br>In the case when Segmentation(intel-style) is being used how does the compiler assigns<br>
addresses?<br><br>Thanks<br>Vaibhav Jain<br><br><div class="gmail_quote">On Sat, Jul 16, 2011 at 11:13 PM, Joel A Fernandes <span dir="ltr"><<a href="mailto:agnel.joel@gmail.com">agnel.joel@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On Sat, Jul 16, 2011 at 3:47 PM, Vaibhav Jain <<a href="mailto:vjoss197@gmail.com">vjoss197@gmail.com</a>> wrote:<br>
> Hi Mulyadi,<br>
><br>
> Thanks for the explanation.Its really nice! But<br>
> what I was referring to the was this article on virtual address layout of<br>
> program :<br>
><br>
> <a href="http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory" target="_blank">http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory</a><br>
><br>
> which mentions : "The distinct bands in the address space correspond to<br>
> memory segments like the heap, stack, and so on. Keep in mind these segments<br>
> are simply a range of memory addresses and have nothing to do with<br>
> Intel-style segments."<br>
><br>
> This gave rise to all the confusion.I used to think that code, stack and<br>
> heap segments in the virtual address layout of a program<br>
> are the same as the segments which we talk about when referring to hardware<br>
> provided Segmentation. But seems like this is not the case.<br>
<br>
</div>Segmentation and the virtual address layout are independent of each<br>
other so you shouldn't confuse the two. You can choose to have<br>
segmentation and then virtual addressing.<br>
<br>
The way it works is:<br>
<br>
logical address (segmented) -> virtual address -> physical address<br>
<br>
The logical to virtual conversion is called segmentation, and virtual<br>
to physical is called paging. The Software always uses logical<br>
addresses.<br>
<br>
The article explains "heap", "stack" etc are segments created by the<br>
operating system and have nothing to do with traditional Intel-style<br>
"segments". Infact the hardware would not be even aware of the<br>
presence of the heap. The HW only knows virtual addresses.<br>
<div class="im"><br>
> I also read a little about real and protected mode and came to know that in<br>
> 32-bit protected mode all the Segment registers point to the same address<br>
<br>
</div>It happens so that Linux sets up the processor tables in such a way<br>
that the logical addresses map to the same as virtual addresses. IIRC,<br>
For x86 there's a "Global Descriptor table" that Linux manipulates to<br>
produce this one-to-one mapping.<br>
<div class="im"><br>
> This confused me even more. So I need an explanation of how all these work<br>
> together. I am Sorry if the question is<br>
> not clear or if it sounds confusing.<br>
<br>
</div>Hope this clears it, do read the introduction chapters of<br>
Understanding the Linux Kernel which touches on a lot of these topics.<br>
Feel free to ask more questions.<br>
<br>
Thanks,<br>
<font color="#888888">Joel<br>
</font></blockquote></div><br>