ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My question is simple:
how (if) can i access directly and modify a memory block or register by knowing it's address
ex.
0x0af67
what i want
Code:
#include<iostream>
using namespace std;
class addresser{
public:
addresser(int &daddress){addr=daddress;
reg[]="eax";}
~addresser();
int addr;
int function_which_gives_address_contents(int &address);
char reg[3];
int function_which_gives_reg_contents(char ®ister[]);
}myaddresser;
int addresser::function_which_gives_address_contents(void){
//HOW:confused:
return contents;
}
int addresser::function_which_gives_reg_contents(void)
//HOW :confused:
return address_contents;
}
int main(){
int select_a;
cout<<"Please give your address"<<endl;
cin>>select_a;
addresser myaddresser(&select_a);
cout<<select_a<<"="<<myaddresser.int function_which_gives_address_contents(void)<<endl;
cout<<"Eax = "<<myaddresser.function_which_gives_reg_contents(void)<<endl;
return 0;
}
Note:the two function don't take parameters cause they will use by default
the addr var and reg array.
Thanks for any recommendations!
One of the cornerstones of modern computer is protected memory and there is no easy way to access the whole virtual memory space. There is no other easy access to system memory AFAIK.
You will probably have to drop into assembler but even them the protected memory will get in the way. You'll probably have to talk to the kernel to get access.
There's another way, though I don't remember the specifics. Read about mmap (man mmap). A long time ago I had to use it to open a "window" into real memory to read and write values to a device's registers. I actually came across the solution by doing a search on google. As I recall, the author used the term "silver bullet" in his text. So maybe search for "silve bullet mmap". In fact, I just tried it, and hit this: Linux Journal Article
Quote:
Accessing Memory-Mapped Devices
Many devices are memory mapped for two reasons. First, for x86 architectures, the memory map is more expansive than the I/O map. Most other architectures only have a memory-mapped architecture (in other words, you don't get a choice); only the x86 architecture has the second address space for peripherals. Again, there are methods to penetrate the memory protection mechanism to access the hardware. The other reason is that arguably, more powerful addressing modes allow faster and more flexible methods of accessing the memory. The x86 port commands are extremely limited in features.
To access your memory-mapped hardware, the device /dev/mem is the silver bullet, and mmap() is the method to select the memory physical base address and the block size to access. To open the peripheral memory, open /dev/mem. Use the opened file descriptor in mmap(), with the appropriate address and the block size (in bytes) to open. mmap() returns an address that is mapped to the physical base address; cast it to the appropriate data type and use it like any other pointer, array, structure pointer or your favorite method.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.