Truncated match.
PICList
Thread
'CASEstatement? Reply'
1998\02\19@134420
by
GREG OWEN

Hi Andrew,
What does the ^ do in your program. I looked up the ^ symbol
and it is a bitwise XOR. Why do you have this on your examples,
except the 7 one? The 51^7 and 12^51 are even more confusing.
Thanks for your help!
Greg
>>> Andrew Warren <spam_OUTfastfwdTakeThisOuTIX.NETCOM.COM> 02/13/98 07:51am
>>>
H.P. de Vries <.....H.P.d.VriesKILLspam@spam@stud.tue.nl> wrote:
> I hope someone can help me with this, because I dont have any bright
> ideas right now ;) Is there a nifty little piece of code out there
> what does the same as the "case", e/g/ "switch" statement?
>
> I am creating a MIDIcontroller board with a 16button interface.
> Depending on which button is pressed, I want to branch to another
> pice of code. Who can help??
Hans:
There are many ways to do this... Here's how I usually manage it:
CASE:
XORLW 1
BZ W_CONTAINED_1
XORLW 2^1
BZ W_CONTAINED_2
XORLW 3^2
BZ W_CONTAINED_3
etc...
This method takes 3 words (and 3 cycles) per test. There are
faster/shorter ways, but they generally require your "cases" to be
consecutive numbers. The method shown above will work with ANY
numbers; it could just as easily have been:
CASE:
XORLW 7
BZ W_CONTAINED_7
XORLW 51^7
BZ W_CONTAINED_51
XORLW 12^51
BZ W_CONTAINED_12
Andy
=== Andrew Warren  fastfwdKILLspamix.netcom.com
=== Fast Forward Engineering  Vista, California
=== http://www.geocities.com/SiliconValley/2499
1998\02\19@150338
by
Mike Keitz

On Thu, 19 Feb 1998 13:46:58 0500 GREG OWEN <.....GOWENKILLspam.....FUSIONUV.COM> writes:
>Hi Andrew,
>What does the ^ do in your program. I looked up the ^ symbol
>and it is a bitwise XOR. Why do you have this on your examples,
>except the 7 one? The 51^7 and 12^51 are even more confusing.
What the program does is xor the unknown data byte (in W) with one of the
constants that it is to be tested against. If all bits match, the result
of the XOR is zero and the program goes to the routine for that value:
xorlw 7 ;Is W=7?
skpnz ;No, skip
goto do_7 ;Do the routine for 7.
This is simple so far. The problem is that if W was *not* 7, it's value
is different from what it was at the start. So it's not quite
straightforward to test it against another number. It is necessary to
correct for the xoring with 7. The easiest way is to xor with 7 again.
Since 7 xor 7 = 0, N xor 7 xor 7 = N. To test if the original value was
51, you could do:
xorlw 7 ;Take the 7 back out
xorlw 51 ;Is W = 51?
skpnz ;No. Skip to the next test.
goto do_51 ;Do the routine for 51.
Since 7 and 51 are both constants, there is no need for 2 xor operations
at runtime. They can be combined at assembletime and just one runtime
xor done. This leads to the routine presented by Andrew Warren and
others:
xorlw 7^51 ;Take out the 7, test with 51.
skpnz
goto do_51
The assembler combines 7 and 51 to a new constant, that will be equal to
the contents of W if the original value was 51 before it was xor'd with 7
in the process of testing if it was 7. Each subsequent compare would do
the same, xoring out the previous compare and xoring in the next value to
compare with. Hopefully I've made this explanation clear enough.
_____________________________________________________________________
You don't need to buy Internet access to use free Internet email.
Get completely free email from Juno at http://www.juno.com
Or call Juno at (800) 654JUNO [6545866]
1998\02\19@150719
by
Orin Eman
> What does the ^ do in your program. I looked up the ^ symbol
> and it is a bitwise XOR. Why do you have this on your examples,
> except the 7 one? The 51^7 and 12^51 are even more confusing.
It is XOR. He's using the the following properties of XOR:
A XOR 0 = A
A XOR A = 0
A XOR B = B XOR A
A XOR (B XOR C) = (A XOR B) XOR C
The effect of the code is
(Wo is original W)
W = Wo XOR 7 (0 if W == 7)
W = W XOR (51 XOR 7) or W = (Wo XOR 7) XOR (51 XOR 7)
which after rearranging is Wo XOR 51 (0 if Wo == 51).
This is a neat way of doing a switch BTW.
Orin.
1998\02\19@161231
by
Andrew Warren

I wrote:
> CASE:
>
> XORLW 7
> BZ W_CONTAINED_7
>
> XORLW 51^7
> BZ W_CONTAINED_51
>
> XORLW 12^51
> BZ W_CONTAINED_12
>
and GREG OWEN <EraseMEPICLISTspam_OUTTakeThisOuTMITVMA.MIT.EDU> replied:
> What does the ^ do in your program. I looked up the ^ symbol
> and it is a bitwise XOR. Why do you have this on your examples,
> except the 7 one? The 51^7 and 12^51 are even more confusing.
Greg:
If you carefully step through the code, you'll see what's happening.
First, a basic rule: If you XOR a number with itself, the result is
always zero... So the first two lines of the program,
XORLW 7
BZ W_CONTAINED_7
will branch to "W_CONTAINED_7" if W contained 7 before the "XORLW".
Following so far?
Ok...
The "XORLW" instruction stores the result of the XOR back in the
Wregister. This means that the "XORLW 7" instruction will modify
the Wregister, so if W DIDN'T contain 7 (so the branch to
"W_CONTAINED_7" WASN'T taken), the Wregister will no longer contain
its original value by the time the PIC executes the second XORLW.
Therefore, the third line of the program (which tests for W = 51),
can't simply say
XORLW 51
because even if W DID contain 51 on entry to "CASE", it won't contain
51 by the time it gets to the third line of the program. What it
WILL contain, if it was equal to 51 on entry to "CASE", is 51 XORed
with 7.
So... The third line of the program has to compare W to 51^7; if it's
equal to THAT number, it means that W was equal to 51 at "CASE", so
we branch to "W_CONTAINED_51".
Now let's deal with the third possibility, W = 12.
If W contained 12 at "CASE", the first XORLW will set W equal to
12^7 and the first branch won't be taken. The second XORLW will
further modify W, setting it to (12^7)^(51^7), and the second branch
won't be taken, either.
Two more basic rules:
A ^ 0 = A
"xor" is associative and commutative... That is,
(a^b)^c = a^(b^c), and b^c = c^b.
Where were we? Oh, yeah... If W contained 12 at "CASE", it'll hold
(12^7)^(51^7) by the time we get to the third XORLW. Using the
associative and commutative laws, we see that:
W = (12^7)^(51^7)
= ((12^51)^(7^7)
We know that 7^7 = 0, so W = (12^51)^0.
Since A^0 = A, W = 12^51.
So...
If W contained 12 at "CASE", it'll contain 12^51 at the third XORLW.
Therefore, we test for THAT number in the third XORLW, and if the
result is zero, we branch to "W_CONTAINED_12".
Does it make sense now?
Andy
=== Andrew Warren  fastfwdspam_OUTix.netcom.com
=== Fast Forward Engineering  Vista, California
=== http://www.geocities.com/SiliconValley/2499
More... (looser matching)
 Last day of these posts
 In 1998
, 1999 only
 Today
 New search...