An odd line of code:

Go To Last Post
19 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
	ScheduleIndex += 0 ;

What do you thing?

A) This line is probably optimized out.
B) This line is probably why it doesn't work

hehe

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Back when I was your age and microcontrollers weren't what we think of them today, it was common to have memory-mapped peripheral circuitry. Recalling some of the instances from past lives they'd carry out a function such as input capture that we now take for granted as "built in".

So if that variable on an AVR8 with one of the common C compilers was memory-mapped to SRAM space and was volatile, the access should not be optimized away and the act of reading that location could trigger some function.

So much for A). Re B) that depends on what "it" is. Depending on how many advanced degrees "it" has earned, "it" might reject certain work as being beneath him/her/it.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Define: "it"
Define: "doesn't work" ??

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That could be the loc you write to when you want to increment the schedule index you think? Maybe its a byte in a dual port ram that someone else is reading?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In any case, you need to punch the original programmer square on the nose for not commenting the code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wonder if it's simply a (benign as it turns out) typo/bug and it was supposed to be "10" or something?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kk6gm wrote:
In any case, you need to punch the original programmer square on the nose for not commenting the code.

+1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

+1

Did you mean +=0 ?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Was supposed to be += 1. I even noticed it before I tried to execute it.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, I meant +1 for a good reply. (at least a punch is more restrained than shooting)

Personally, I never use += 1 because ++ is far more obvious.

Of course, += 0 for a volatile variable should ensure a read and a write. But since this is an unusual operation, it deserves a comment.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Personally, I never use += 1 because ++ is far more obvious.

?
Only if you're familiar with the idiom. Ask someone who's never written a line of code in their life which one is more obvious. I think they'd find both equally confusing, and ask why

 ScheduleIndex+;

doesn't work.

Four legs good, two legs bad, three legs stable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, point taken.

I come from a M68000 world where you could post-increment in byte, word or long with single ASM opcodes.

So the concept of a post-increment, predecrement etc is perfectly normal.

Of course an AVR is a lot less endowed. It implements the language but needs several instructions.

The original question was "why this unusual statement?"

It turns out that it was a typo and the intended statement was un-natural (to me).
Of course, you pick up language by context. If the author used +=1 extensively, you would understand the intention.

Fortunately Tom did not post the whole program!
So we could not see the context.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It has nothing to do with the 68000. K&R had B and C working at Bell Labs in '73 on a pdp8 then a pdp11 which have pre and post increment and decrement on the data and index registers. The 68000 came along in the mid late 70s. K&R claim their goal was to make a hi level assembler that would allow machine instruction access.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes I know that C came before M68000. So I guess that Motorola provided suitable instructions to suit C.

I always understood that the PDP8 had ASM increment instructions. And C followed the PDP8. After all, pre-decrement and post-increment are pretty cool in ASM. Pretty cool in C too !

If punters don't like a C operator, they don't have to use it. There are always multiple ways to skin cats.

As long as you have the same intended result, the Optimiser will generally produce exactly the same code. So it is always best to use the simplest and clearest C statements for the benefit of the human.
The Compiler benefits too. It can optimise the clear intention better thn a convoluted intention.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I always understood that the PDP8 had ASM increment instructions. And C followed the PDP8. After all, pre-decrement and post-increment are pretty cool in ASM. Pretty cool in C too !

I think you meant PDP11. (PDP8 had a 3-bit op code.)

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I also used 6809 and 68000 in assembler, both of which had post-increment and pre-decrement, but that was indicated with a single plus or minus sign, not a double one. In fact, without getting the book down off the shelf, I think the 6502 had a post increment as well. Still a single plus sign.
And that was my point. I am sure there is some reason why K&R decided not to use ScheduleIndex+; I just don't know what it is.

Four legs good, two legs bad, three legs stable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Quote:

I always understood that the PDP8 had ASM increment instructions. And C followed the PDP8. After all, pre-decrement and post-increment are pretty cool in ASM. Pretty cool in C too !

I think you meant PDP11. (PDP8 had a 3-bit op code.)


http://compilers.iecc.com/compar...
http://cm.bell-labs.com/cm/cs/wh...
Quote:
Thompson went a step further by inventing the ++ and -- operators, which increment or decrement; their prefix or postfix position determines whether the alteration occurs before or after noting the value of the operand. They were not in the earliest versions of B, but appeared along the way. People often guess that they were created to use the auto-increment and auto-decrement address modes provided by the DEC PDP-11 on which C and Unix first became popular. This is historically impossible, since there was no PDP-11 when B was developed. The PDP-7, however, did have a few `auto-increment' memory cells, with the property that an indirect memory reference through them incremented the cell. This feature probably suggested such operators to Thompson; the generalization to make them both prefix and postfix was his own. Indeed, the auto-increment cells were not used directly in implementation of the operators, and a stronger motivation for the innovation was probably his observation that the translation of ++x was smaller than that of x=x+1.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

John_A_Brown wrote:
And that was my point. I am sure there is some reason why K&R decided not to use ScheduleIndex+; I just don't know what it is.

Probably because "-variable" already had meaning (arithmetic negation), and they wanted to have some consistency between increment and decrement in both their pre and post forms.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

christop wrote:
John_A_Brown wrote:
And that was my point. I am sure there is some reason why K&R decided not to use ScheduleIndex+; I just don't know what it is.

Probably because "-variable" already had meaning (arithmetic negation), and they wanted to have some consistency between increment and decrement in both their pre and post forms.

Thanks. That sounds very likely.

John

Four legs good, two legs bad, three legs stable.