get offset and segment address of string
Example:-
char st[4096]="Stack Over Flow\0";
char 开发者_高级运维st1[4096]="Knowledge beyond boundaries";
void main (void)
{
}
""Stack Over Flow\0"
"Knowledge beyond boundaries"
these are two strings, i want to calculate the offset and segment address of these strings using c program and print it. Is their any way to do this *Please answer it *
Segments and offsets are antiquated features from before protected mode came to x86 chips.
Once they gained protected mode, they started using selectors to get away from the fact that memory addresses were now virtual rather than physical. And they generally use a flat model nowadays, which has very few selectors, each with a large (and sometimes the same) backing memory block.
The way you get an address of a variable in C is with the &
operator. For example:
#include <stdio.h>
int main (void) {
int xyzzy = 42;
printf ("Address is %p\n", &xyzzy);
return 0;
}
outputs Address is 0xbfe8232c
on my system.
Since the C language doesn't directly support this antiquated segment/offset stuff, it was left to the implementation and depended greatly on the memory model in use. For example, some compilers provides functions like getDS()
to give you the data segment register, others had to resort to inline assembly.
Depending on the model (tiny, small, medium, large and so on), there were various ways to work it out, none of which I'll detail here due to the fact they're useless to the vast majority of developers around nowadays :-)
One example of the hoops we had to jump through can be found in the 80x86 16-bit Compiling How-to, by Alexei A. Frounze, an article detailing how all the segmentation stuff worked under real mode and the various memory models used to support it.
精彩评论