LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2005, 12:54 PM   #1
NCC-1701&NCC-1701-D
Member
 
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88

Rep: Reputation: 16
Question Direct memory access through C/C++


Hi all,

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 &register[]);
}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!
 
Old 09-13-2005, 01:25 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
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.
 
Old 09-14-2005, 09:03 AM   #3
mic
Member
 
Registered: Jun 2005
Posts: 44

Rep: Reputation: 16
It is possible to access an address with outb, outw, inb, inw and other similar functions.

Examples:

value = inb(port);
outb(value, port);

Of course your program will then have to run as root. It is prefered and usually much safer to use a driver (if available) instead.
 
Old 09-14-2005, 03:20 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SUSE 9.3 and the PC's DMA (Direct Memory Access) langbein SUSE / openSUSE 26 11-22-2005 12:06 PM
Direct access to sound device not possible bootsy Linux - Hardware 1 08-06-2005 06:02 AM
Direct Memory Access with C NCC-1701&NCC-1701-D Programming 12 06-20-2005 12:26 AM
Jack or alsa issue - direct access to soundcard not possible bootsy Linux - Software 11 01-24-2005 08:04 PM
RH 8.0 Mapping Virtual Memory to get access to VMIC Reflective Memory PCI card. Merlin53 Linux - Hardware 0 05-05-2003 12:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration