PDA

View Full Version : How to do... If X = 1 AND Y = 1 Then ... in ASM



Chris_D
03-28-2009, 08:45 PM
Hi guys,

I sort of understand the CMP(s) function now and sort of understand the IF_C etc. functions and need to expand on them.· As the title shows...

·If X = 1 AND Y = 1 Then

is easy enough to understand in BASIC and perhaps even SPIN.· However, how would I go about this in ASM?

Chris
·

hippy
03-28-2009, 09:20 PM
Something like (untested) ...




cmp X,#1 WZ
IF_Z cmp Y,#1 WZ

IF_Z jmp #okay ' X=1 and Y=1
IF_NZ jmp #fail ' X<>1 or Y<>1




There are probably more optimal ways as well.

virtuPIC
03-28-2009, 09:41 PM
You simply have to convert the AND operator to two nested IFs:

IF X = 1 THEN
IF Y = 1 THEN

In assembler this gives




cmp X, #1 WZ
IF_Z cmp Y, #1 WZ


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Airspace V - international hangar flying!
www.airspace-v.com/ggadgets (http://www.airspace-v.com/ggadgets) for tools & toys

Phil Pilgrim (PhiPi)
03-29-2009, 02:47 AM
For this particular example (testing both for 1):



djnz X,#fail nr
djnz Y,#fail nr
succeed ...




-Phil

MagIO2
03-29-2009, 03:33 AM
Hi Phil,

again a nice solution from you. But for a newbie you should have mentioned, that your solution will change the values of X and/or Y.

hippy
03-29-2009, 03:49 AM
@ MagIO2 : Not with NR as a qualifier http://forums.parallax.com/images/smilies/smile.gif

lonesock
03-29-2009, 04:05 AM
I would probably still mention that with each DJNZ, if the jump is not taken it would require 4 extra clocks. So to get to the 'succeed' label, it would take 16 clocks. Not that this is a problem, especially if you are going for code density!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
lonesock
Piranha are people too.

MagIO2
03-29-2009, 04:06 AM
Argh ... sure ... did I already tell you that I like this propeller? You can throw away all your experience.

Phil Pilgrim (PhiPi)
03-29-2009, 04:39 AM
Good point, lonesock. However, if you compare the djnz code with




cmp X,#1 wz
if_z cmp Y,#1 wz
if_nz jmp #fail
succeed ...




you will see that the above requires 12 clocks to get to succeed or fail, while the djnz code takes 16 to get to succeed and 4 or 8 to get to fail. So the optimum choice will depend on the relative probabilities of the two cases.

-Phil

Fixed formatting.

Post Edited (Phil Pilgrim (PhiPi)) : 3/28/2009 9:06:30 PM GMT

Beau Schwabe
03-29-2009, 04:46 AM
Phil Pilgrim (PhiPi) (http://forums.parallax.com/member.php?u=45432),

I like your use of the djnz, however if you wanted something that followed more closely to ... If N=0 then ... or ... if N=1 then ... you could use the TJNZ or TJZ command.



tjnz X,#fail 'jump to address if not zero
tjnz Y,#fail 'jump to address if not zero
succeed ...


·or..



tjz X,#fail 'jump to address if zero
tjz Y,#fail 'jump to address if zero
succeed ...





▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe (mailto:bschwabe@parallax.com)

IC Layout Engineer
Parallax, Inc.

Phil Pilgrim (PhiPi)
03-29-2009, 05:02 AM
Beau,

Yup! But I think you meant, "If N<>0", rather than, "If N==1", right?

Actually, "If X==0 AND Y==0" is quicker with




or X,Y nr,wz
if_nz jmp #fail
succeed ...




-Phil

Beau Schwabe
03-29-2009, 05:07 AM
lol @ Phil Pilgrim (PhiPi) (http://forums.parallax.com/member.php?u=45432)

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe (mailto:bschwabe@parallax.com)

IC Layout Engineer
Parallax, Inc.

Phil Pilgrim (PhiPi)
03-29-2009, 05:15 AM
This is one reason I love the Propeller instruction set so much. With selective result and flag writes, along with conditional execution, a lot can be accomplished in just a few instructions. It's almost like writing microcode.

-Phil

Chris_D
03-29-2009, 08:47 PM
Wow,

That's a lot of suggestions!· Thanks very much for all of them!

I will have to do some experimenting with the various examples so I can understand them but this should be enough to help me work out the structure for more complex decision making branching and such.



Chris