Unsigned Int and Variable at address

J

John T.

Hi NG.
I have two questions.
First, could I address some bit of an unsigned integer? lets say the integer
"mrtest" holds the value 0xFFFF0000 and I would like to do something like
(this is wirtten in some pseudo code)
if(mrtest(bit7,bit6,bit5,bit4 and bit 3 == 0b00000)
{
do a lot of things;
}


Second, how do I store a variable at a specific address?
for example
Store mrtest at address 0xFFFF1166.

Best Regards
John
 
D

Dan Pop

In said:
First, could I address some bit of an unsigned integer? lets say the integer
"mrtest" holds the value 0xFFFF0000 and I would like to do something like
(this is wirtten in some pseudo code)
if(mrtest(bit7,bit6,bit5,bit4 and bit 3 == 0b00000)
{
do a lot of things;
}

Try to use some kind of pseudo code that makes sense to other people, too.
If you want to test that all these bits are 0, build an unsigned mask
containing ones only in the corresponding positions: 0xF8U should do the
job (assumming bit0 is the least significant one). A bitwise and between
the value to be tested and the mask will tell you if all the relevant
bits are zero:

if ((mrtest & 0xF8U) == 0) {
/* do a lot of things */ ;
}

The extra pair of parentheses is needed because the & operator has the
"wrong" precedence WRT the == operator.
Second, how do I store a variable at a specific address?
for example
Store mrtest at address 0xFFFF1166.

This is a very unwise thing to do, unless you have a *good* reason for
doing it. Most modern platforms use virtual memory, which makes such
an exercise pointless.

The answer is to define mrtest as a macro:

#define mrtest (*(unsigned int *)0xFFFF1166)

Now, the identifier mrtest will give you an unsigned int stored at address
0xFFFF1166, assuming that this address is suitable for storing an unsigned
int at all. If it isn't, your program may crash at the first attempt to
use mrtest...

Dan
 
D

dbtid

Hi NG.
I have two questions.
First, could I address some bit of an unsigned integer? lets say the
integer
"mrtest" holds the value 0xFFFF0000 and I would like to do something like
(this is wirtten in some pseudo code)
if(mrtest(bit7,bit6,bit5,bit4 and bit 3 == 0b00000)
{
do a lot of things;
}

I'm not quite sure I understand what you're asking, but I'll state some
assumptions and you can go from there...

I assume by 0b00000 you mean binary 5 bits worth of 0's.

If that's the case, then, yes, this is possible, and even better, it's
easy.

if ((mrtest & 0x000000fc) == 0)
{
// do whatever.
}

If any of the bits in mrtest[7:3] is a 1, the result of the bitwise and
will be non-zero.

Second, how do I store a variable at a specific address?
for example
Store mrtest at address 0xFFFF1166.

Declare a pointer of the appropriate type and set it to the address you
want.
Then assign the value through that pointer.

a) more traditionally:

unsigned int *xFFFF1166 = (unsigned int *) 0xFFFF1166;
*xFFFF1166 = mrtest;

b) other way:

*((unsigned int *) 0xFFFF1166) = mrtest;

HTH
 
D

Dan Pop

In said:
I assume by 0b00000 you mean binary 5 bits worth of 0's.

If that's the case, then, yes, this is possible, and even better, it's
easy.

if ((mrtest & 0x000000fc) == 0)
{
// do whatever.
}

If any of the bits in mrtest[7:3] is a 1, the result of the bitwise and
will be non-zero.

Nope, your mask contains *six* set bits (0xC == 8 + 4), so the result
can be non-zero even if *all* the five "target" bits are zero.

Dan
 
D

dbtid

I assume by 0b00000 you mean binary 5 bits worth of 0's.

If that's the case, then, yes, this is possible, and even better, it's
easy.

if ((mrtest & 0x000000fc) == 0)
{
// do whatever.
}

If any of the bits in mrtest[7:3] is a 1, the result of the bitwise and
will be non-zero.

Nope, your mask contains *six* set bits (0xC == 8 + 4), so the result
can be non-zero even if *all* the five "target" bits are zero.

Dan

To quote Homer Simpson, "Doh!"

Thanks, Dan, for pointing that out.

To the OP: sorry to have slightly incorrectly answered your question.

The bitmask ought to be 0x000000f8.

Humble apologies.
 
E

Emmanuel Delahaye

In said:
The answer is to define mrtest as a macro:

#define mrtest (*(unsigned int *)0xFFFF1166)

If it's a hardware address, I'll add 'volatile'

#define mrtest (*(unsigned volatile int *)0xFFFF1166)
 
C

CBFalconer

Dan said:
.... snip ...

It's supposed to be a memory address and last time I checked,
memory counted as hardware. Yet, values stored in memory are
not supposed to change by themselves, therefore volatile is not,
a priori, justified.

The mere fact that you are defining such a location carries a
strong implication that something else knows about that location,
and thus may be modifying it. The only other reasonable
interpretation I see is that it participates in memory-mapped i/o,
in which case the volatile is again needed to prevent optimizing
out repeated output events.
 
D

Dan Pop

In said:
The mere fact that you are defining such a location carries a
strong implication that something else knows about that location,
and thus may be modifying it. The only other reasonable
interpretation I see is that it participates in memory-mapped i/o,
in which case the volatile is again needed to prevent optimizing
out repeated output events.

Your imagination is limited (but, at least, you haven't left your brain in
"neutral" this time ;-) Suppose that you have two types of memory
(e.g. slow/fast or cached/uncached or volatile/backed up) and you
want to force this particular object to be allocated on a certain
type of memory, hence the explicit address.

Dan
 
C

CBFalconer

Dan said:
Your imagination is limited (but, at least, you haven't left your
brain in "neutral" this time ;-) Suppose that you have two types
of memory (e.g. slow/fast or cached/uncached or volatile/backed up)
and you want to force this particular object to be allocated on a
certain type of memory, hence the explicit address.

That smells highly of 'kluge'. Such a job is better left to the
linker/loader or whatever you want to call that phase of
executable creation. Of course the language limitations of a
particular implementation may force you to such a dirty kluge.
There certainly is no standard way of handling it, including the
original pegged address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top