[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] Checking carry flag and Rotate operation in C.

From: Weddington, Eric
Subject: RE: [avr-gcc-list] Checking carry flag and Rotate operation in C.
Date: Sun, 15 Jun 2008 18:26:26 -0600


> -----Original Message-----
> From: 
> address@hidden 
> [mailto:address@hidden
> org] On Behalf Of Jonathan Blanchard
> Sent: Sunday, June 15, 2008 5:43 PM
> To: address@hidden
> Subject: [avr-gcc-list] Checking carry flag and Rotate operation in C.
> Hi,
> I got two question about programming with AVR-GCC. Both are related to
> finding a way to generate a specific output in assembler.
> First, how do you create the rotate operation in C. Specifically how
> can the ROL and ROR can be generated.

AFAIK, right now you have to do the rotate in inline assembly.

> Secondly I have this piece of code where b is a 16 bit 
> unsigned integer :
>               b = b << 1;
>               if( b & 256 )
>                       b = b ^ 0b100011101;
> To optimize that I only need to check if b overflow at the left shift
> operation by checking the carry flag. I'm trying to find a way to do
> that in C. Right now I'm using the following piece of inline assembly
> to do the trick :
>               asm volatile(
>                                       "LSL %0         \n\t"
>                                       "BRCC 1f    \n\t"
>                                       "EOR %0, %1 \n\t"
>                               "1:""                   \n\t"
>                                       :"+d" (b)
>                                       :"r"  (PPoly)
>                                       );
> In this last piece of code b is a 8 bit unsigned integer and PPoly is
> a 8 bit unsigned integer with the value 0b00011101. I'm just curious
> to know if it's possible to achieve the same result only by using C.

The Status Register is already available to you in an I/O register. The
address is 0x3F, or use the SREG symbol that is defined in the I/O
header files. Test whichever flag that you need to.

The only potential problem with doing it in C is if there are extra
instructions that get generated between your LSL and the testing of the
Carry flag. Could there be extra instructions, what are they, would they
affect the outcome of the Carry flag?

If you decide to do it all in inline assembly then don't forget to look
at the SBIC instruction instead of using BRCC and a label. At the very
least you can get rid of the label.

Eric Weddington

reply via email to

[Prev in Thread] Current Thread [Next in Thread]