+= Operator

What is the use of that?

Page 1 of 1

9 Replies - 746 Views - Last Post: 27 June 2010 - 01:11 PM Rate Topic: -----

#1 nathanpc  Icon User is offline

  • SCIENCE!
  • member icon

Reputation: 112
  • View blog
  • Posts: 1,171
  • Joined: 31-July 09

+= Operator

Posted 26 June 2010 - 02:30 PM

Hello,
I'm taking a look on ARM Assembly for my new project that I'm in(port my own Linux distro to a Integrator-CP, that uses ARM). For this I'm reading the book ARM System Developer's Guide: Designing and Optimizing System Software, but I'm also reading some articles over the internet, and as the ARM land is a place that C/C++ rules, because Assembly is a kind of human impossible, then on the tutorials of ARM Assembly, they usually show C/C++ code and then the equivalent in Assembly(commented line-by-line with the corresponding C/C++ line), then I saw this:
ldr      r0, [r1, r2]    @ Pre-indexed.             r0= *(u32*)(r1+r2)
ldr      r0, [r1, r2]!   @ Pre-indexed,  writeback. r0= *(u32*)(r1 += r2)
ldr      r0, [r1], r2    @ Post-indexed, writeback. r0= *(u32*)r1; r1 += r2;

Then this questions pop-uped on my mind:
  • What is the += operator?
  • What is the use of that operator?
  • Why not just =, or just +?

Note that r0, r1 and r2, are ARM registers.

Best Regards,
Nathan Paulino Campos

This post has been edited by nathanpc: 26 June 2010 - 02:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: += Operator

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: += Operator

Posted 26 June 2010 - 02:34 PM

I'm confused is this a general operator question, a C/C++ question, or an Assembly question?

If I may take a stab in the broadest sense of the question:

The += operator is used to add a value to the value in place.

int val = 5;
val += 5;
//is equivalent to
val = val + 5; 



In high level languages it is syntactic sugar (at least in languages that support it), however I can see there being value in choosing one over the other when it comes to assembly.

Based on the comments in your sample, it would come in handy when you want to add two terms together to pass to another slot, but perhaps keep one value the same.

We really don't know anything about the snippet in question; as with nearly all things in the programming world, trying to speculate without context can be frivolous at best and dangerous at worst.
Was This Post Helpful? 0
  • +
  • -

#3 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: += Operator

Posted 26 June 2010 - 02:50 PM

It is sugar. But I would say (stretching for reasons to use it, here)

1) it's shorter. Get used to terseness and you'll be fine with assembly.

2) it's a more accurate reflection of what's actually happening at the cpu register level. It shows you that whatever is in register r2 is being picked up by the big robotic arm inside your CPU and put into the r1 bucket. The expression r1 = r1 + r2 (and any fully developed expression, really) gives more of a mental image of setting out the buckets of data on the table and moving things around before putting the resulting beans in bucket r1. The load-to-register assembly instructions (ldr) are going to be faster (take fewer cycles) than something swapping variables around, so the terseness of the operator helps reinforce that in your mind. Maybe. Maybe I'm stretching too far. I think I've possibly had some sort of metaphor meltdown.
Was This Post Helpful? 1
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: += Operator

Posted 26 June 2010 - 03:09 PM

View Postmoopet, on 26 June 2010 - 02:50 PM, said:

It shows you that whatever is in register r2 is being picked up by the big robotic arm inside your CPU and put into the r1 bucket.


This is fantastic.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: += Operator

Posted 26 June 2010 - 03:19 PM

What the instruction mean are.
ldr      r0, [r1, r2]    @ Pre-indexed.             r0= *(u32*)(r1+r2)


The value at r1 + r2 is placed into r0

ldr      r0, [r1, r2]!   @ Pre-indexed,  writeback. r0= *(u32*)(r1 += r2)


r1 = r1 + r2 , value at r1 is placed into r0

ldr      r0, [r1], r2    @ Post-indexed, writeback. r0= *(u32*)r1; r1 += r2;

value at r1 is placed into r0, r1 = r1 + r2.
Was This Post Helpful? 0
  • +
  • -

#6 Banfa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 109
  • Joined: 07-June 10

Re: += Operator

Posted 27 June 2010 - 03:51 AM

I am not sure about other languages but I know for a fact that in C (may be C++) r1 += r2 is not syntactic sugar for r1 = r1 + r2.

In my experience they compile to different assembly the the second syntax having more instructions. I have to qualify that with the additional information that the the case in question was using a fairly basic compiler and I suspect that under good optimisation I would have expected them to be the same. At the time I was surprised they were different.

So I guess it boils down to that favourite adage, its platform dependent :D
Was This Post Helpful? 2
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: += Operator

Posted 27 June 2010 - 06:43 AM

I was getting all my ducks in a row to start a good argument on this... but then I found that Banfa is correct. The standard does define a difference:

Quote

The behavior of an expression of the form E1 op = E2 is equivalent to E1 = E1 op E2 except that E1 is evaluated only
once.


Even though a single variable is a very simple expression, the standard does seem to say that it is evaluated twice in r1 = r1 + r2. The only time I can see this knowledge really being useful is in an expression like this:

*ptr++ += value; //valid by operator precedence
*ptr++ = *ptr++ + value; //Invalid expression with undefined behavior. 


(example from here)

Also since this operator can be overloaded one has to be really careful in assuming what it may do when applied to classes.

<Edit>odd little bug with the code tags there... that should read: "//Invalid expression with undefined behavior." but the code tags don't seem to like it.</edit>
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: += Operator

Posted 27 June 2010 - 06:57 AM

I suppose that another example would be this:

array[x] += 5;
array[x] = array[x] + 5;

here "array[x]" is an expression *(array+x) and it will be evaluated only once with the assignment operator version but twice with the other version. While an optimizing compiler might optimize the second evaluation out, technically it should be there.

so the assignment version is probably slightly more efficient.
Was This Post Helpful? 0
  • +
  • -

#9 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: += Operator

Posted 27 June 2010 - 12:25 PM

The "+=" operator is a trucated operator, i.e. it is a shortcut.

This post has been edited by Aphex19: 27 June 2010 - 12:25 PM

Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: += Operator

Posted 27 June 2010 - 01:11 PM

Quote

The "+=" operator is a trucated operator, i.e. it is a shortcut.
-- as stated above that is not entirely accurate. that does capture *some* of the truth but not ALL of the truth. Read banfa's and my posts above.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1