Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing a specific memory location

Status
Not open for further replies.

Praetoriansys

Technical User
Apr 14, 2003
2
0
0
US
How can I access a specic memory location, and what are the rules and regs to follow when doing this(ie. can't access certain addresses, addressing scheme, ect..)


Thanks
 
The best way of accessing certain memory locations is in assembly code, but unless it is neccessary I suggest not doing it. The operating system manages memory, and you have C functions such as malloc.
To give an example of how assembly code can be used:
Code:
void main (void)
{
  asm {
    push es
    mov ax,0b800h
    mov es,ax
    mov di,00h
    mov ah,'*'
    mov es:[di],ah
    pop es
  }
}
The program above accesses memory B800:0000, which is the start of the display memory. Accessing the display memory in this way can be useful, for example, in displaying a message on screen from a TSR program.
My advice is to get a book on assembly because:
1) It is programming at its lowest level.
2) It will enable you to do cool stuff in C(++).
I hope some of that is meaningful. :)

Adonai
 
but remember that the example above is 16 bit and applies to text modes only. If you try direct memory accesses in more protected situations you may come unstuck. Good multi-user operating systems don't appreciate attempts to manipulate memory outside that allocated to the program.
 
int memory_adderss;
...
char* x;
x = memory_adderss;
cout<< &quot;addredd of byte on &quot; << (int)x<< &quot; is&quot;<< *x<< endl;

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
>> IonFilipski

Almost:

int address = 0xffffffff;
char * dangerous = (char*)address; // cast necessary

>> adholioshake

It doesn't matter whether you use ASM or C here. What *does* matter is the operating system you're running. Under a protected mode OS such as Windows, dereferencing the pointer will raise an access violation (unless you get lucky ;) ), and the program will crash.

Try this for fun(??):

char text[] = &quot;Let's go surfing!&quot;;

char * p = text;

while(1)printf(&quot;%c&quot;, *(p++) );
 
>>Sebastiani
Praetoriansys asked how to do it, no if it is dangerous or not. To read memory always is dangerous.


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
This all seems a bit theroretical. Just what do you want to accomplish by accessing a specific adress?

For instance if you want to &quot;access a spefic memory location&quot; where you have a variable located, just use the & operator.

/Per
[sub]
if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
[/sub]
 
I have also tried to access memory beyond my process space. The address I tried to access were 0xE0000 and 0xF0000 this where the system copies all the bios information.

Under Unix you can treat memory as a file device and open it as /dev/mem

Protected Mode OS like Windows will not allow this. So you have to use Native API of Win NT like

NtOpenSection() to open a section of memory

NtMapViewofSection() to map a section of memory to your process space.


All this is very tedious and dangerous. So if there is any easier method of just reading the data b/w 0xE0000 and 0xFFFFF I would like to know it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top