little algorithm code implementation!

Discussion in 'Embedded Systems and Microcontrollers' started by Eric007, Aug 5, 2012.

  1. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,041
    33
    Hi All,

    I am writing a code for the following algorithm:

    b7 = g7
    b6 = b7 xor g6
    b5 = b6 xor g5
    .......
    b0 = b1 xor g0

    In the above, assuming g7 - g0 is in Wreg initially!

    I came up with the following code as attached!
    I posted it here coz I NEVER took the time to learn how to simulate codes with MPlab Sim...(I'll try reading on it so I won't post such thang nomore).

    As you can notice there is a repetition of a block of code...if the logic is ok, I will turn that block into a macro in order to reduce the number instruction cycles.

    BTW, I was tipsy when writing this code so if....yeah...

    All comments/critics are welcomed!
     
  2. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    what exactly is it for?? And what are you trying to accomplish??
     
  3. panic mode

    Senior Member

    Oct 10, 2011
    1,318
    304
    looks like code conversion grey/binary
     
  4. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I might not be understanding, but can't you do it all in one go?
    move W to temp
    clear carry
    rotate right the b byte
    xor it with temp
    job done

    <ed> I flipped bit 7, just xor the result with b'10000000' </ed>
    <ed2> Actually maybe I didn't </ed2>
     
    Last edited: Aug 5, 2012
  5. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    To convert binary to Gray, it is only necessary to XOR the original unsigned binary with a copy of itself that has been right shifted one place. I believe he is converting gray to binary here......to convert Gray to binary is to XOR the bits one at a time, starting with the two highest bits, using the newly calculated bit in the next XOR.
     
    Eric007 likes this.
  6. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,041
    33
    No! You can't do it in one go!
    Look again at the algorithm...the output of the next bit depends on the output of the previous bit and the current input bit...

    Hope it makes sense what I just said coz I'm so drunk!

    Doing it the way you suggested is the following algo:

    g7 = b7
    g6 = b7 xor b6
    g5 = b6 xor b5
    ....
    go = b1 xor bo
     
  7. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    Then go sober up and come back when sober and first answer the question in which you were asked to give some clue as to what you are trying to accomplish. Specifically, are you trying to convert from a traditional reflected Gray code to binary?
     
  8. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,041
    33
    Ok! I am sober now...:D

    What I'm trying to accomplish is to translate that algorithm I showed I post #1!
    And some members here have figured out what the algo is about...

    I just want to make sure if the code correctly translates the algo coz I simulated it with a pencil and a piece od paper and I am conviced that it is perfect!

    But I wanted an opinion from someone else like if it well coded!
    Like I said the block of code that repeats itself will be converted into a macro so that instead of having that block, I'll just have a single line of code.

    But there might be a simpler way to implement that block...

    Anyways I am sure it would work without have the data corrupted!
     
    Last edited: Aug 6, 2012
  9. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    But why did you feel that people needed to figure out what your algorithm does? Why could you start off with, perhaps, "I've developed an algorithm to convert from Gray code to binary. Could you please look it over?"

    It can be done in shorter time. Consider the following C code:

    binary = graycode;
    binary ^= binary >> 1;
    binary ^= binary >> 2;
    binary ^= binary >> 4;

    That converts an 8-bit Gray code to binary.

    For why it works, see Gray Codes
     
  10. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,041
    33
    @WBahn: I am sorry about that!

    The reason I did that is because I did not want to implement other algorithm that does the same job BUT the one I showed only! and if I had said what I need to achieve we ll be throwing other algorithms...so to avoid that I did this

    sorry errbody!

    It ok I will have a look at you algorithm as it seems less complex

    Thanks!
     
  11. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,041
    33
    Yes Sir! this is beatiful...:D

    Thanks so much...I been wasting my time for nuttin'

    Check out the code attached! Any bugs in that code? No!;)

    Thanks again!
     
  12. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    You can save a few more cycles if you use swapf instead of the last four rotates.
    Actually there are bugs,
    Code ( (Unknown Language)):
    1.     clrc                ; clear carry flag
    2.     rrf    temp, w            ;
    3.  
    4.     clrc                ; clear carry flag
    5.     rrf    temp, w    
    actually only rotates by one bit, not two.
     
  13. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,041
    33
    yep but swap alone would not work!

    will have to add andlw 0x0F otherwise the result will be wrong and again this will result in 2 instruction cycles! but if I make it a macro like I showed in the attachment....I would be able to do the 4 (2, ...) rotates in a single instruction cycle. am I correct?

    thanks Markd77
     
    Last edited: Aug 6, 2012
  14. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,041
    33
    will this macro work?

    Code ( (Unknown Language)):
    1.  
    2.  
    3. rrn    macro   freg1, freg2, literal
    4.  
    5. movlw   literal
    6. movwf  freg2
    7. clrc
    8. rrf      freg1, w
    9. decfsz   freg2, f
    10. goto      $-3
    11. endm
    12.  
     
    Last edited: Aug 7, 2012
  15. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    A macro doesn't save any instruction cycles. It takes exactly as long as the instructions as it contains.
    Also look again at my last post, you still have bugs.
     
  16. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,041
    33
    ok! I thought a macro would save the instruction cycles...

    I thought that for instance " rrn temp, count, 4" would count as a single instruction cycle! I can't find any bugs...can you show me?
     
  17. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    "rrf freg1, W" doesn't change freg1 so doing it more than once has the same effect as doing it once.
     
  18. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,041
    33
    Stupid me! it should be " rrf freg1, f", right? u are talking about the macro, right?

    too late! I am already late!
     
  19. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Partly, but that changes freg1, and you also want to keep it unaltered, so you probably need to copy it to a temporary variable.
    It applies to the macro and also "version2.txt"
     
    Eric007 likes this.
  20. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,041
    33
    Thanks! that's an easy fix!

    This is what happens when you mix Hennessy and Assembly...resulting in Chemistry!
     
Loading...