473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

expression evaluation doubt!

Hello,

I am using a C compiler on ARM processor based system (32 bit).

I m executing following code on this system:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Foo (void)
{
unsigned long base_addr = 0x3001E000; // sizeof (unsigned long)
== 4
unsigned long temp;

temp = ((volatile t_uint16*) base_addr + 0x0220); // STATEMENT 3
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

I thought that the value of temp would be 0x3001E220. But when I
execute this function, I get the value of temp as 0x3001E440.

Can anyone please explain why this is happening?

I tried to see the assembly language (ARM) code for STATEMENT 3.
It is generated as following:

///////////////////////////////////////////////////////////////////////////
LDR r0,0x37548 // &base_addr = 0x37548
LDR r0,[r0]
ADD r4,r0, #0x440 // ??????
///////////////////////////////////////////////////////////////////////////

The compiler directly generates the code to add 0x440.

Please help me understand why this happens.

Thanks in advance.

Apr 5 '07 #1
4 1203
Bhavik wrote:
Hello,

I am using a C compiler on ARM processor based system (32 bit).

I m executing following code on this system:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Foo (void)
{
unsigned long base_addr = 0x3001E000; // sizeof (unsigned long)
== 4
unsigned long temp;

temp = ((volatile t_uint16*) base_addr + 0x0220); // STATEMENT 3
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

I thought that the value of temp would be 0x3001E220. But when I
execute this function, I get the value of temp as 0x3001E440.

Can anyone please explain why this is happening?
What's the relationship between 0x0220 and 0x440? How many bytes in a
16 bit int?

--
Ian Collins.
Apr 5 '07 #2
"Bhavik" <bh**********@gmail.comha scritto nel messaggio
news:11**********************@p77g2000hsh.googlegr oups.com...
temp = ((volatile t_uint16*) base_addr + 0x0220); // STATEMENT 3
I thought that the value of temp would be 0x3001E220. But when I
execute this function, I get the value of temp as 0x3001E440.
That casts base_addr to a pointer to a 16-bit unsigned integer, *then* adds
0x0220 to it according to pointer arithmetics (i.e., it finds out where
would be the 0x0221st element of an array of t_uint16 whose first element's
address is base_addr).

To get 0x3001E220 you had to write (volatile t_uint16*)(base_addr + 0x0220),
casting is more binding than sum.
Apr 5 '07 #3
On 5 Apr 2007 04:27:26 -0700, "Bhavik" <bh**********@gmail.comwrote:
>Hello,

I am using a C compiler on ARM processor based system (32 bit).

I m executing following code on this system:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Foo (void)
{
unsigned long base_addr = 0x3001E000; // sizeof (unsigned long)
== 4
unsigned long temp;

temp = ((volatile t_uint16*) base_addr + 0x0220); // STATEMENT 3
}
Even after you correct the problem with the pointer arithmetic, your
compiler should still be issuing a diagnostic for this. The right
hand side evaluates to a value with type pointer to something. The
left hand side has type unsigned long. There is no implicit
conversion between these types.
Remove del for email
Apr 5 '07 #4
Bhavik wrote:
>
I am using a C compiler on ARM processor based system (32 bit).
I m executing following code on this system:

void Foo (void)
{
unsigned long base_addr = 0x3001E000; // sizeof (unsigned long)
== 4
unsigned long temp;

temp = ((volatile t_uint16*) base_addr + 0x0220); // STATEMENT 3
}

I thought that the value of temp would be 0x3001E220. But when I
execute this function, I get the value of temp as 0x3001E440.

Can anyone please explain why this is happening?
Overlong lines snipped. Do not exceed 72 char line length in
Usenet, 67 is better. Also, do no use // comments in usenet, they
do not survive line wrapping.

You don't give the definition of t_uint16 nor of a pointer to it.
It seems the compiler considers sizeof (t_uint16) to be 2, in which
case the result is correct. However assigning a pointer to an
integral type results in implementation defined behaviour, and is
not portable, thus is off-topic here.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

--
Posted via a free Usenet account from http://www.teranews.com

Apr 7 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Simon Wigzell | last post by:
A client wants have acess to an online databases records controlled by group strings and evaluation strings e.g., each use would have in his client record a group string like this: And each...
14
by: John Temples | last post by:
Given this code: extern volatile unsigned char v; int main(void) { v; return 0; }
4
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues....
8
by: manan.kathuria | last post by:
hi all , the expression in question is ++i&&++j||++k most sources say that since the result of the || operation is decided by the LHS itself , the right side is not computed my point of...
2
by: Jeffrey Ganping Chen | last post by:
I'm trying to build a generic runtime C# expression evaluation engine to allow the user to evaluate any C# code blocks at runtime within the application's context. For example, obj4.p4 =...
8
by: Brian Blais | last post by:
Hello, I have a string input from the user, and want to parse it to a number, and would like to know how to do it. I would like to be able to accept arithmetic operations, like: '5+5'...
8
by: junky_fellow | last post by:
Hi, Sorry, for asking similar questions again and again. 1) I want to know how should we reslove the ambiguities in a c expression ? Should we use precedence table as mentioned in K&R book ...
21
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
2
by: Tim Johnson | last post by:
I'm having trouble understanding this part of the expression evaluation rules ('6.5 Expressions', second item, C99 spec; the C++ wording is presumably identical): What precisely does the...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.