Why did it emit this code?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//clear timer
  TCNT1=0;
    714c:	10 92 85 00 	sts	0x0085, r1	; 0x800085 <__TEXT_REGION_LENGTH__+0x7e0085>
    7150:	10 92 84 00 	sts	0x0084, r1	; 0x800084 <__TEXT_REGION_LENGTH__+0x7e0084>

  while ((!(PIN(BOOTLOADER_PORT) & _BV(BOOTLOADER_PIN))) && (!(TIFR0 & _BV(OCF0A))))
    7154:	4c 99       	sbic	0x09, 4	; 9
    7156:	02 c0       	rjmp	.+4      	; 0x715c <get+0x34>
    7158:	a9 9b       	sbis	0x15, 1	; 21
    715a:	fc cf       	rjmp	.-8      	; 0x7154 <get+0x2c>
    ;

  //disable TCNT1 so it has our count
  TCCR1B=0;
    715c:	10 92 81 00 	sts	0x0081, r1	; 0x800081 <__TEXT_REGION_LENGTH__+0x7e0081>

  //here we can check for the timerout flag to see if it is set and exit if so
  if (TIFR0 & _BV(OCF0A))
    7160:	95 b3       	in	r25, 0x15	; 21
    7162:	89 2f       	mov	r24, r25
    7164:	82 70       	andi	r24, 0x02	; 2
    7166:	91 ff       	sbrs	r25, 1
    7168:	02 c0       	rjmp	.+4      	; 0x716e <get+0x46>
    return 0;
    716a:	80 e0       	ldi	r24, 0x00	; 0
    716c:	50 c0       	rjmp	.+160    	; 0x720e <get+0xe6>

7158/715a are generated by the !(TIFR0 & _BV(OCF0A)) test.

 

But then 7160-7168 are generated by the if (TIFR0 & _BV(OCF0A)) statement.  What is it doing with R24?  Why not just do a sbic/rjmp?

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

What happens if you add !! to it?

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

!! creates the same output too.

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

A single one uses sbis/jmp:   if (!(TIFR0 & _BV(OCF0A)))

Even this does sbis/jmp:    if ((~TIFR0 & _BV(OCF0A)))

 

This goes back to the 5 instructions:   if ((~~TIFR0 & _BV(OCF0A)))

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

In another thread, it was noted "tough crowd today".

 

What is an "it"?  (from the thread title)

 

What AVR model are we dealing with?

 

If "it" is GCC, then tell toolchain, version, and optimization settings.

 

alank2 wrote:
This goes back to the 5 instructions: if ((~~TIFR0 & _BV(OCF0A)))

In that quote, that is the only '~~' I see on the page.  So what are we going 'back" to?

 

alank2 wrote:
What is it doing with R24?

And thus more context might be needed.  Ideally complete test program.

 

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

Sorry, I'm bad about leaving out the context.  It is a 32U4 on Atmel Studio 7.0.1188.

 

After more code changes it went away and I can't get it to come back by trying to undo some things, so for now it has corrected itself.

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

My guess is that in the code following the conditional return, the value of R24 (=2) was being used. The optimiser seized on that fact and wouldn't give it up, even though the size cost is greater than the more optimal:

//here we can check for the timerout flag to see if it is set and exit if so
  if (TIFR0 & _BV(OCF0A))
    7160:	a9 9b       	sbis	0x15, 1	; 21
    7162:	02 c0       	rjmp	.+4      	; 0x7168 <get+0x46>
    return 0;
    7164:	80 e0       	ldi	r24, 0x00	; 0
    7166:	50 c0       	rjmp	.+160    	; 0x720e <get+0xe6>

    7168:	80 e0       	ldi	r24, 0x02	; 2

Anyway I've given up routinely inspecting compiler output. it's always been correct (That's not to say there are never any errors though) and always leads to a colossal waste of time.

 

More important to me is the question "Do I like the code I've just written ?" To that, the answer is rarely yes; and rewriting stuff because I don't like a variable name or function signature; that's where the time goes.

 

--Nigel