Shop OBEX P1 Docs P2 Docs Learn Events
How to do... If X = 1 AND Y = 1 Then ... in ASM — Parallax Forums

How to do... If X = 1 AND Y = 1 Then ... in ASM

Chris_DChris_D Posts: 305
edited 2009-03-29 12:47 in Propeller 1
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
·

Comments

  • hippyhippy Posts: 1,981
    edited 2009-03-28 13:20
    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.
  • virtuPICvirtuPIC Posts: 193
    edited 2009-03-28 13:41
    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 for tools & toys
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-28 18:47
    For this particular example (testing both for 1):
            djnz    X,#fail nr
            djnz    Y,#fail nr
    succeed ...
    
    
    


    -Phil
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-03-28 19:33
    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.
  • hippyhippy Posts: 1,981
    edited 2009-03-28 19:49
    @ MagIO2 : Not with NR as a qualifier smile.gif
  • lonesocklonesock Posts: 917
    edited 2009-03-28 20:05
    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.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-03-28 20:06
    Argh ... sure ... did I already tell you that I like this propeller? You can throw away all your experience.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-28 20:39
    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 SchwabeBeau Schwabe Posts: 6,568
    edited 2009-03-28 20:46
    Phil Pilgrim (PhiPi),

    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

    IC Layout Engineer
    Parallax, Inc.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-28 21:02
    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 SchwabeBeau Schwabe Posts: 6,568
    edited 2009-03-28 21:07
    lol @ Phil Pilgrim (PhiPi)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-28 21:15
    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_DChris_D Posts: 305
    edited 2009-03-29 12:47
    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
Sign In or Register to comment.