AVR assembler and clearing bits...

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

Hi,

Please excuse this simple question - I'm a bit new to AVR assembler, but have many many years of other assemblers and languages.

In the ATmega48 manual (2545-AVR-09/07) on page 52 the follow code is commented as "Clear WDRF in MCUSR":

in   r16, MCUSR
andi r16,(0xff & (0<<WDRF))
out  MCUSR, r16

Am I missing something here? The above looks like it sets MCUSR to zero. In my ignorance, I would have expected:

...
andi r16, ~(1<<WDRF)
...

...or similar which only clears the specified bit.

Thanks

Nick

Nicko

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

It's a typo. Your version is correct.

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

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

if you look at the C-Code example below the assembler example
MCUSR &= ~(1<<WDRF);
you see exactly what you have suggested.

Send a mail to Atmel. So they can do a correction of the datasheet.

Regards
Sebastian

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

Quote:
Send a mail to Atmel. So they can do a correction of the datasheet.
I think it is correct, the (0xff & 0<<bitname) is used frequently and I have the feeling it is the IAR assembler syntax.

Does ANYONE out there still uses the IAR assembler to clarify this?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Quote:
the (0xff & 0<<bitname) is used frequently and I have the feeling it is the IAR assembler syntax

This is no IAR specific syntax but general C. It's usually used for OR operation:

DDRD = (1<<PD7) | (0<<PD6) | (1<<PD5) | (0<<PD4) | (1<<PD3) | (0<<PD2) | (1<<PD1) | (0<<PD0);

(0 << bit) will always result 0 and thus will have no effect in the example above. This is usefull if you later need to change a bit in a SFR. And for legibility this is an improvement, too. All bits in the SFR are listed this way.

But in an AND operation the expression (0 << bit) will clear the complete register.

Regards
Sebastian

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

S-Sohn wrote:
This is no IAR specific syntax but general C
....
But in an AND operation the expression (0 << bit) will clear the complete register.

I'm familiar with C syntax (in the dim and distant past I was part of the ANSI C X3 J11 standardisation process and have written two full standards-compliant compilers myself) - the question was whether this was some arcane artifact of the AVR assembler or not.

It seems that its a straightforward typo, which is interesting as I've seen it quoted verbatim in production code and it just "looked wrong". Code snippets in data sheets should hopefully be reliable...

FWIW, I just check the list file for the above code snippets in AVR Studio 4 SP1, and the code snippet in the manual is definitely wrong. It should be the example I provided... (as per the C code).

Cheers

Nick

Nicko

Last Edited: Tue. Nov 20, 2007 - 11:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:
I think it is correct, the (0xff & 0<<bitname) is used frequently and I have the feeling it is the IAR assembler syntax.

Does ANYONE out there still uses the IAR assembler to clarify this?


I don't think that'd be valid in ANY assembler. 0 shifted by anything is always 0 and anything ANDd with 0 is always 0. So the above could never deliver anything but 0.

As for the question of anyone using the IAR assembler (which is completely free to all by the way - the full, unlimited version comes with the Kickstart version of the C compiler) the fact is that everyone using IAR C is using it - like almost all C compilers it works by converting C to Asm source then assembling it.

Cliff

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

Quote:
I don't think that'd be valid in ANY assembler.

It is perfectly valid. Just not useful.

Regards,
Steve A.

The Board helps those that help themselves.

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

Steve,

Apologies, "valid FOR CLEARING A SINGLE BIT"

(it does a cracking job of clearing 8 though)

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

As this has come up before, and in order to clear it up in my mind once and for all whether it is a typo or some weird IAR assembler syntax, I installed the IAR asm from an old CD.
With this source file:

	NAME	main

	PUBLIC	main
        
        
MCUSR equ 0x34
WDRF  equ 3

main	
        in      r16, MCUSR 
        andi    r16,(0xff & (0<<WDRF)) 
 
        in      r16, MCUSR 
        andi    r16,~(1<<WDRF)
	END	main

the (0xff & (0<<WDRF)) produces the expected result of clearing all bits

      1    00000000                      NAME    main
      2    00000000              
      3    00000000                      PUBLIC  main
      4    00000000                      
      5    00000000                      
      6    00000034              MCUSR equ 0x34
      7    00000003              WDRF  equ 3
      8    00000000              
      9    00000000              main    
     10    00000000 B704                 in      r16, MCUSR 
     11    00000002 7000                 andi    r16,(0xff & (0<<WDRF)) ;Produces 0x00 as a mask
                                                      
     12    00000004               
     13    00000004 B704                 in      r16, MCUSR 
     14    00000006 7F07                 andi    r16,~(1<<WDRF) ;Produces 0xf7 as a mask-Correct

     15    00000008                      END     main

because I doubted such esteemed freaks, I shall make myself a vegemite sandwich for lunch as a punishement. It may prove fatal, so if you don't hear from me for a while you know what happened. Cards, flowers or money can be send to the address shown on my website :mrgreen:

If on the other hand you just don't want to hear from me (as some do), full stop, money can still be send at the same address, say AU$100.00 would shut me up for 24 hours :?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Quote:
If on the other hand you just don't want to hear from me (as some do), full stop, money can still be send at the same address, say AU$100.00 would shut me up for 24 hours

Bidding War! I'll shut up for 24 hours for $50! (I know of several here who will be tempted...)

whoops. my wife beat you to it. anybody interested in buying tomorrow?

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

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

Quote:
whoops. my wife beat you to it.
She probably paid using your checkbook (you better go order some more blank checks). She'll have a good thing going until you figure that one out (you'll figure it out when you go to the bank to deposit it- the bank teller will look at it, tear it up, and say 'Its all taken care of, Mr. Baird.').

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

Be aware that a few bits (interrupt flag bits, mostly, I think) are CLEARED by writing a 1 to the bit. So, it pays to read the text description for the register and its bits carefully.

Jim

 

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

 

 

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

js wrote:
It may prove fatal, so if you don't hear from me for a while you know what happened.

If it does prove fatal but we then DO hear from you after a while then that's kind of spooky! :lol:

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

FWIW I had a very nice reply from the AVR documentation team thanking me for the correction and saying that the datasheet will be changed. (didn't say when, though...)

They did point out that they really appreciate the updates as I guess it just increases the quality of the product. I also got the impression that the individual concerned is a member of this forum...

Nick

Nicko

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

.....I shall be haunting in Finchifield tonight.......woaaa haaaa oooo :evil:

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly