cancel
Showing results for 
Search instead for 
Did you mean: 

Direct Register addressing in C

md21028
Associate II
Posted on February 25, 2004 at 18:29

Direct Register addressing in C

5 REPLIES 5
md21028
Associate II
Posted on February 23, 2004 at 16:04

This should be an easy one.... I am trying to write a function to modify physical registers by their address (not their name in the IO header file). How can I directly affect an address in C (assuming a Cosmic compiler)? Something like.....

function(char address, char modifier)

{

//Stub to modify value in address

}
peterharris9
Associate II
Posted on February 23, 2004 at 17:18

Hi Drew

function(const unsigned int address, char modifier)

{

// Note address is now int

char * pointer_to_register;

pointer_to_register = address; // point pointer at the required register

*pointer_to_register = modifier; // modify contents of address

}

// Another method would be to pass the address as a pointer

function(char *address, char modifier)

{

*address = modifier; // modify contents of address

}

BM
md21028
Associate II
Posted on February 23, 2004 at 18:15

Hmmm... I got an invalid indirection operand message. Here is what my code looks like:

function()

{

char temp1, temp2;

temp1 = array[offset]; //Array contains address to be modified

temp2 = *temp1;

}

Shouldn't temp2 show the contents of the address stored in temp1?

BTW, thanks for the quick response Battman.

peterharris9
Associate II
Posted on February 23, 2004 at 19:19

temp1 & temp2 are both local variables of type char.

The line temp2 = *temp1; assumes that temp1 is a char pointer eg its was declared as:

char * temp1;

Try this:

function()

{

char * temp1

char temp2;

temp1 = &array[offset]; //Array contains address to be modified

temp2 = *temp1;

}

If you have access to a copy of 'The C programming language' 2nd edition by K&R there is a really handy code fragment & explaination on page 94 .

BM
md21028
Associate II
Posted on February 25, 2004 at 18:29

Battman,

You would have been 100% correct assuming that my array was initialized as the correct data type. All I had to do to your code was type cast the array. Thanks for your help!