+ Reply to Thread
Results 1 to 5 of 5

Thread: SHR SHL question

  1. #1

    Default SHR SHL question

    when shifing data in assembly, what is written to c, the last bit shifted off the 32 bit workspace or the first bit?

    say you have %0100 and shift it right 3 bits, what would be written to c?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who says you have to have knowledge to use it?

    I've killed a fly with my bare mind.
    Last edited by ForumTools; 10-02-2010 at 07:40 PM. Reason: Forum Migration

  2. #2
    SSteve's Avatar
    Location
    Sierra Foothills, California (1 hr from Parallax HQ)
    Posts
    809

    Default

    It looks to me like C doesn't get written by shr or shl. I hooked an LED up to A0 and wrote the attached code. The LED will blink slowly if C is set and quickly if C is not set. When I run this program, the LED blinks quickly, suggesting that C is not set.

    I'm not 100% confident, though. It seems like C should get shifted into. Experiment with it and see if you get the same results.

    Don't forget that you have to append WC to the line when you want C to be updated.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows
    Attached Files Attached Files
    Last edited by ForumTools; 10-02-2010 at 07:40 PM. Reason: Forum Migration

  3. #3

    Default

    For all shift instructions (ROR/ROL/SHR/SHL/RCR/RCL/SAR/REV D,S) either the initial LSB or MSB of D can be written to the C flag. You must useиa 'wc' after the operands to affect C:

    иииииии RORии A,#3ииииwcииииииииииии 'rotate A right by three bits,иold bit 0 of A into C flag
    ииииииииSHLии A,#1ииииwcииииииииииии 'shift A left by one bit,иold bitи31 of A into C flag
    иииииии SARии A,Bи ииииwcииииииииииии 'shift arithmetic A right by B bits,иold bit 0 of A into C flag

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
    Last edited by ForumTools; 10-02-2010 at 07:40 PM. Reason: Forum Migration

  4. #4
    SSteve's Avatar
    Location
    Sierra Foothills, California (1 hr from Parallax HQ)
    Posts
    809

    Default

    So it doesn't matter how many bits D is shifted by, C always gets the original bit 0 (or bit 31) of D?

    Can you take a look at the CarryInAsm.spin program I posted? I can't figure out why the "if_c" line is never executed. If I change it to "if_nc", it is always executed. I tried all kinds of combinations of shl/shr/ror/rol and different values for test_value. Edit: Never mind. Please take a look at this thread instead.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    Post Edited (SSteve) : 6/1/2006 8:21:19 AM GMT
    Last edited by ForumTools; 10-02-2010 at 07:40 PM. Reason: Forum Migration

  5. #5

    Default

    Chip,

    It's easy to understand why this carry behavior might cause confusion. I think the natural inclination among assembly programmers is that a shift of n followed by a shift of m would be equivalent to a shift of m+n in all respects:

    Code:
          shl     dest, #5
          shl     dest, #4    wc
    
    is expected to be equivalent to
    
          shl     dest, #9    wc



    This is particularly vexing/confusing as it pertains to RCL and RCR. In fact, now I'm having difficulty understanding what's going on with these two instructions. Usually, rotates such as these include the carry bit in a circular shift. In the Propeller's case this would amount to a 33-bit circular register that gets rotated by the specified number of bits, leaving the last bit shifted into the carry intact. This is important because, including the carry, no bits get lost, and the instruction can be used for packing and unpacking data. (This would really come in handy in conjuction with MOVI and MOVS for packing bytes, since one extra bit could be "protected" in the carry from the 9-bit moves.)

    So I'm wondering if maybe you could shed more light on the behavior of RCL and RCR and, perhaps, let us in on the reasoning behind choosing a "first-shift" behavior for the carry as opposed to the expected "last-shift" behavior.

    Thanks,
    Phil
    Last edited by ForumTools; 10-02-2010 at 07:40 PM. Reason: Forum Migration

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts