little algorithm code implementation!

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
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!
 

Attachments

Markd77

Joined Sep 7, 2009
2,806
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:

BMorse

Joined Sep 26, 2009
2,675
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>
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.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
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>
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
 

WBahn

Joined Mar 31, 2012
29,978
Hope it makes sense what I just said coz I'm so drunk!
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?
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
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:

WBahn

Joined Mar 31, 2012
29,978
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...
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?"

But I wanted an opinion from someone else like if it well coded!
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
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
@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!
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
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
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!
 

Attachments

Markd77

Joined Sep 7, 2009
2,806
You can save a few more cycles if you use swapf instead of the last four rotates.
Actually there are bugs,
Rich (BB code):
    clrc                ; clear carry flag 
    rrf    temp, w            ; 
 
    clrc                ; clear carry flag 
    rrf    temp, w
actually only rotates by one bit, not two.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
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:

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
will this macro work?

Rich (BB code):
rrn    macro   freg1, freg2, literal
 
movlw   literal
movwf  freg2
clrc
rrf      freg1, w
decfsz   freg2, f
goto      $-3
endm
 
Last edited:

Markd77

Joined Sep 7, 2009
2,806
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.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
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?
 

Thread Starter

Eric007

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

too late! I am already late!
 

Markd77

Joined Sep 7, 2009
2,806
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"
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Thanks! that's an easy fix!

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