Odd ASM from WinAVR

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

I have the following ISR in C:

ISR(TIMER1_COMPA_vect)
{
	PORTF ^= (1 << 5);
	if (steps)
	{
		PORTF &= !(1 << 4);  //step pulse
		steps--;
		PORTF |= (1 << 4);
	}
	
}

This compiles to:

00000348 <__vector_7>:
 348:	1f 92       	push	r1
 34a:	0f 92       	push	r0
 34c:	0f b6       	in	r0, 0x3f	; 63
 34e:	0f 92       	push	r0
 350:	11 24       	eor	r1, r1
 352:	8f 93       	push	r24
 354:	9f 93       	push	r25
 356:	81 b3       	in	r24, 0x11	; 17
 358:	90 e2       	ldi	r25, 0x20	; 32
 35a:	89 27       	eor	r24, r25
 35c:	81 bb       	out	0x11, r24	; 17
 35e:	80 91 3b 01 	lds	r24, 0x013B
 362:	90 91 3c 01 	lds	r25, 0x013C
 366:	89 2b       	or	r24, r25
 368:	61 f0       	breq	.+24     	; 0x382 <__vector_7+0x3a>
 36a:	81 b3       	in	r24, 0x11	; 17
 36c:	11 ba       	out	0x11, r1	; 17
 36e:	80 91 3b 01 	lds	r24, 0x013B
 372:	90 91 3c 01 	lds	r25, 0x013C
 376:	01 97       	sbiw	r24, 0x01	; 1
 378:	90 93 3c 01 	sts	0x013C, r25
 37c:	80 93 3b 01 	sts	0x013B, r24
 380:	8c 9a       	sbi	0x11, 4	; 17
 382:	9f 91       	pop	r25
 384:	8f 91       	pop	r24
 386:	0f 90       	pop	r0
 388:	0f be       	out	0x3f, r0	; 63
 38a:	0f 90       	pop	r0
 38c:	1f 90       	pop	r1
 38e:	18 95       	reti

If you look through all the out statements, there is one writing to Port F (0x11) from r1. A few lines previous to this, there is an eor r1, r1, clearing r1. I changed the order of some of the statements in the ISR without changing this behavior in the ASM.

If I change the C to:

ISR(TIMER1_COMPA_vect)
{
	PORTF ^= (1 << 5);
	if (steps)
	{
		PORTF ^= (1 << 4); //&= !(1 << 4);  //step pulse
		steps--;
		PORTF ^= (1 << 4); //|= (1 << 4);
		//steps--;
	}
}

The problem disappears, and the following ASM results:

00000348 <__vector_7>:
 348:	1f 92       	push	r1
 34a:	0f 92       	push	r0
 34c:	0f b6       	in	r0, 0x3f	; 63
 34e:	0f 92       	push	r0
 350:	11 24       	eor	r1, r1
 352:	2f 93       	push	r18
 354:	8f 93       	push	r24
 356:	9f 93       	push	r25
 358:	81 b3       	in	r24, 0x11	; 17
 35a:	90 e2       	ldi	r25, 0x20	; 32
 35c:	89 27       	eor	r24, r25
 35e:	81 bb       	out	0x11, r24	; 17
 360:	80 91 3b 01 	lds	r24, 0x013B
 364:	90 91 3c 01 	lds	r25, 0x013C
 368:	89 2b       	or	r24, r25
 36a:	81 f0       	breq	.+32     	; 0x38c <__vector_7+0x44>
 36c:	81 b3       	in	r24, 0x11	; 17
 36e:	20 e1       	ldi	r18, 0x10	; 16
 370:	82 27       	eor	r24, r18
 372:	81 bb       	out	0x11, r24	; 17
 374:	80 91 3b 01 	lds	r24, 0x013B
 378:	90 91 3c 01 	lds	r25, 0x013C
 37c:	01 97       	sbiw	r24, 0x01	; 1
 37e:	90 93 3c 01 	sts	0x013C, r25
 382:	80 93 3b 01 	sts	0x013B, r24
 386:	81 b3       	in	r24, 0x11	; 17
 388:	82 27       	eor	r24, r18
 38a:	81 bb       	out	0x11, r24	; 17
 38c:	9f 91       	pop	r25
 38e:	8f 91       	pop	r24
 390:	2f 91       	pop	r18
 392:	0f 90       	pop	r0
 394:	0f be       	out	0x3f, r0	; 63
 396:	0f 90       	pop	r0
 398:	1f 90       	pop	r1
 39a:	18 95       	reti

Note that the Port F, pin 5 was just for debugging purposes. The presence of that statement had no effect on the behavior of the port other than pin 5.

So does any one have an idea of what would be causing this? I'd rather know that I'm definitely setting the pin low and then high, just in case for some reason the pin was set low elsewhere somehow!

Thanks!

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

I suppose you are aware that pf4-pf7 are used by the JTAG in some processors and will not do anything you want unless JTAG is off.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Check to see if ! the same as ~. The compiler may be just doing what it was told. ;)

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

Yeah, JTAG is disabled and the I/O bits themselves work fine. Thanks for the heads up, though.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
!(1 << 4)

This always results in 0, which I don't think is what you intended. Try this instead:

~(1 << 4)

Regards,
Steve A.

The Board helps those that help themselves.

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

Sorry nanovate, didn't see your comment when I replied. Ok, I'm obviously mistaken about what ! is doing. I thought it was a bitwise not, I'm guessing that it's a "global" not, ie if the value is > 0, result is 0, or if value == 0, result is 1?

Thanks for help!

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

Quote:
I'm guessing that it's a "global" not

It is a "logical" not. !A is false (or 0) if A is 0. !A is true (or 1) if A != 0. "~" is the bitwise operator.

Regards,
Steve A.

The Board helps those that help themselves.

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

Time to purchase a copy of K&R to remove the "guessing" perhaps?

In the meantime the 101 tutorial may be useful:

https://www.avrfreaks.net/index.p...

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

Koshchi wrote:
It is a "logical" not. !A is false (or 0) if A is 0. !A is true (or 1) if A != 0. "~" is the bitwise operator.
Ummm, I think that is backwards. (!A) will be true (or 1) if A == 0 and is false (or 0) if A != 0.

Stu (but then I can be soOOOooo confused! :wink: )

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

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

I have confused ~ and ! on more than (!0) occasion!

-Tony

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

Quote:

Time to purchase a copy of K&R to remove the "guessing" perhaps?

And while waiting for the book to arrive, why not google "C operators" to find, in less than three minutes, page upon page describing the difference between ! (logical NOT) and ~ (bitwise NOT, a.k.a. ones complement).

And for me the memorizing rule was this: The symbol for logical NOT is the same as "the NOT part of not equal", ie. != (there is no ~= operator).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]