odd/wrong generated code?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Environment: Atmel Studio 7.0, Atmel-ICE, ATMega328P (at 1Mhz, internal clock)

 

I couldn't figure out what was happening in my code until I looked at the disassembled code.

 

In the snippet below, I have a "while(1) {...}" loop. Inside that loop I check to see if there is no data available and then send more data.

 

What is odd, is the "branch if not equal" (at 0450) does not jump back to the CALL, it only jumps back to (04FF) the instruction testing the return from the call.

 

I have written this could a few different ways and it always gets optimized (-O1) the same way. I can turn off optimization but then the code is huge.

 

While (1) {
    ...

if (tx_data_available() == 0) {                              
0000044D  CALL 0x00000166	Call subroutine
0000044F  SBIW R24,0x00		Subtract immediate from word
00000450  BRNE PC-0x01		Branch if not equal
	lifi_send(buff);                                    
00000451  MOVW R24,R28		Copy register pair
00000452  ADIW R24,0x01		Add immediate to word
00000453  CALL 0x0000017C	Call subroutine
00000455  RJMP PC-0x0008	Relative jump 

...
}

 

If there is no obvious reason for the bad code, Is there an option to turn off optimization for this small section of code?

 

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

Show the .s or the LSS not this confused disassembly.

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

Thanks clawson. I had not looked at the LSS. The snippet from LSS shows the same issue:

 

// wait for the message to be sent before sending it again
rtn = tx_data_available();
 89c:	0e 94 66 01 	call	0x2cc	        ; 0x2cc <tx_data_available>
if (rtn == 0) {
 8a0:	00 97       	sbiw	r24, 0x00	; 0
 8a2:	f1 f7       	brne	.-4      	; 0x8a0 <main+0x4e>
    lifi_send(buff);
 8a4:	ce 01       	movw	r24, r28
 8a6:	01 96       	adiw	r24, 0x01	; 1
 8a8:	0e 94 7d 01 	call	0x2fa	        ; 0x2fa <lifi_send>
 8ac:	f7 cf       	rjmp	.-18     	; 0x89c <main+0x4a>

 

Even if I code the return value into a local variable (rtn) and then perform the test, the resulting code is the same.

 

 

Update:

If I move that snippet into a separate function rather than inline with the "while (1) {...}" loop, then the generated code is very different which works as expected.

 

void conditionally_reload(char* buff) {
 852:	cf 93       	push	r28
 854:	df 93       	push	r29
 856:	ec 01       	movw	r28, r24
    uint8_t rtn;
    rtn = tx_data_available();
 858:	0e 94 66 01 	call	0x2cc	    ; 0x2cc <tx_data_available>
    if (rtn == 0) {
 85c:	81 11       	cpse	r24, r1
 85e:	03 c0       	rjmp	.+6         ; 0x866 <conditionally_reload+0x14>
        lifi_send(buff);
 860:	ce 01       	movw	r24, r28
 862:	0e 94 7d 01 	call	0x2fa	    ; 0x2fa <lifi_send>
    }
}
 866:	df 91       	pop	r29
 868:	cf 91       	pop	r28
 86a:	08 95       	ret

 

 

 

Last Edited: Tue. Aug 13, 2019 - 05:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Need more context.  Show code for tx_data_available().

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

The code for tx_data_available() is very simple:

int8_t tx_data_available() {
if (frame_index >= 0)
return 1;
return 0;
}

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

Are we going to have to ask for every little piece?

 

What is frame_index?  Where/how is it declared?  Where/how is it defined?  Where/how is it modified?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Mark frame_index as volatile - That'll solve it.