Shop OBEX P1 Docs P2 Docs Learn Events
How to compare two pin inputs in PASM ??? — Parallax Forums

How to compare two pin inputs in PASM ???

BeanBean Posts: 8,129
edited 2010-03-30 21:55 in Propeller 1
I'm experiencing brain fade...

I'm working on the PropBASIC compiler and here is what I have for "DO...LOOP WHILE P0 = P1".

__DO_1                                                       '  DO
 
                  or            outa,P16                     '    P16 = 1
 
                  mov           __temp1,#0                   '  LOOP WHILE P0 = P1
                  and           P0,ina WZ, NR               
    IF_NZ         adds          __temp1,#1                  
                  and           P1,ina WZ, NR               
    IF_NZ         adds          __temp1,#1                  
                  subs          __temp1,#1                  
    IF_NZ         jmp           #__DO_1                     


P0 and P1 are LONGs that contain the bitmask of the pins (so P0 = 1, and P1 = 2).

It seems like a bunch of code to do something seemingly so simple.

Can anyone suggest some more effiecient code ?

Thanks,
Bean


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.
PropBASIC thread http://forums.parallax.com/showthread.php?p=867134

March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
There are two rules in life:
· 1) Never divulge all information

Post Edited (Bean) : 3/30/2010 8:55:30 PM GMT

Comments

  • pullmollpullmoll Posts: 817
    edited 2010-03-30 15:02
    Bean said...
    I'm experiencing brain fade...
    I'm working on the PropBASIC compiler and here is what I have for "DO...LOOP WHILE P0 = P1".

    I know this feeling...
        mov   temp1, ina
        mov   temp2, temp1
        shr     temp2, #1
        xor     temp1, temp2
        and     temp1, #1
    
    


    Then you have 1 in temp1 if P0 <> P1 and 0 if P0 == P1.

    Or another way, also 5 instructions:
        test   ina, #1   WZ
        test   ina, #2   WC
        mov  result, #0
    if_nc_and_z mov result, #1
    if_c_and_nz mov result, #1
    
    



    Hmm... there has to be a shorter way:
        mov result, ina
        shr   result, #1     WC
    if_c  xor  result, #1
        and  result, #1
    
    



    Aha! 4 instructions. I think this is the shortest.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    He died at the console of hunger and thirst.
    Next day he was buried. Face down, nine edge first.

    Post Edited (pullmoll) : 3/30/2010 3:10:13 PM GMT
  • AribaAriba Posts: 2,690
    edited 2010-03-30 15:22
    __DO_1                                                       '  DO
     
                      or            outa,P16                     '    P16 = 1
     
                      and           P0,ina WZ, NR              
                      and           P1,ina WC, NR              
        IF_C_EQ_Z     jmp           #__DO_1 
    
    
  • BeanBean Posts: 8,129
    edited 2010-03-30 16:27
    Ariba,
    Nice... That's what I was looking for.
    Thank you very much for posting it.

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Use BASIC on the Propeller with the speed of assembly language.

    PropBASIC thread http://forums.parallax.com/showthread.php?p=867134

    March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
    ·
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-03-30 16:44
    Bean,

    Are P0 - P31 built in as predefinitions in PropBASIC? I know this has become a "standard" of sorts for naming the port pins, but Parallax has made a grave and regrettable mistake in promoting it at the expense of the original (and much better) A0 - A31. (It is port A, after all.) Could the latter be included, as well, as an alternate (or even the preferred) nomenclature?

    -Phil
  • BeanBean Posts: 8,129
    edited 2010-03-30 18:06
    Phil,
    Pins are NOT predefined. I didn't include all the code (I know bad form).

    They are define like:

    P0 PIN 0 INPUT 
    P1 PIN 1 INPUT
    


    So I guess you can use P0 or A0 whichever you like, but you DO need to explicitly define them.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Use BASIC on the Propeller with the speed of assembly language.
    PropBASIC thread http://forums.parallax.com/showthread.php?p=867134

    March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There are two rules in life:
    · 1) Never divulge all information

    Post Edited (Bean) : 3/30/2010 8:55:06 PM GMT
  • lonesocklonesock Posts: 917
    edited 2010-03-30 18:16
    Maybe I'm missing something, but the TEST command can store the parity of the result in C. So if you have a mask of both ports, can't you simply do:
    test both_port_mask, ina wc
    if_nc jmp #wherever
    


    ?

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lonesock
    Piranha are people too.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-03-30 18:21
    @Jonathan,

    Your method makes sense for hand-coding, but the PropBASIC compiler only generates masked for declared pins. To create a combined mask with what the compiler generates would eliminate the code savings of your method.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • lonesocklonesock Posts: 917
    edited 2010-03-30 18:23
    JonnyMac said...
    @Jonathan,

    Your method makes sense for hand-coding, but the PropBASIC compiler only generates masked for declared pins. To create a combined mask with what the compiler generates would eliminate the code savings of your method.
    I would think the compiler could generate the combined mask once, and use it inside the loop, so the speed savings could be considerable over the long haul..unless the pins are changing dynamically?

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lonesock
    Piranha are people too.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-03-30 18:26
    Ariba said...
    __DO_1                                                       '  DO
     
                      or            outa,P16                     '    P16 = 1
     
                      and           P0,ina WZ, NR              
                      and           P1,ina WC, NR              
        IF_C_EQ_Z     jmp           #__DO_1 
    
    


    Am I wrong, or shouldn't the last instruction be IF_C_NE_Z? When P0 is high (1) the Z flag will get zero; when P1 is high the C flag will be set. Likewise, when P0 is low the Z flag will be set; when P1 is low the C flag will be clear.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • BeanBean Posts: 8,129
    edited 2010-03-30 18:29
    Jonathan,
    · That is a good idea, but I don't have both pin masks available as a single value.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Use BASIC on the Propeller with the speed of assembly language.
    PropBASIC thread http://forums.parallax.com/showthread.php?p=867134

    March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There are two rules in life:
    · 1) Never divulge all information

    Post Edited (Bean) : 3/30/2010 8:51:50 PM GMT
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-03-30 18:32
    lonesock said...
    JonnyMac said...
    @Jonathan,

    Your method makes sense for hand-coding, but the PropBASIC compiler only generates masked for declared pins. To create a combined mask with what the compiler generates would eliminate the code savings of your method.
    I would think the compiler could generate the combined mask once, and use it inside the loop, so the speed savings could be considerable over the long haul..unless the pins are changing dynamically?

    Jonathan

    It's a single-pass compiler so it wouldn't know until it reached the code that it needed a compbined mask. Of course, Bean could adopt your strategy using internal variables:

    __DO_1          or      outa, P16
                    mov     __temp1, P0
                    or      __temp1, P1
                    test    __temp1, ina    wc
            if_nc   jmp     #__DO_1
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • lonesocklonesock Posts: 917
    edited 2010-03-30 18:34
    Bean said...
    ...I don't have both pin masks available as a single value...
    Got it, thanks [noparse][[/noparse]8^)

    Jonathan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lonesock
    Piranha are people too.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-03-30 19:58
    Bean,

    Thanks. That makes sense: users can name them however they want. Looking forward, though, since there could be a port B or higher someday (esp. Prop II), I wonder if a single digit provides enough specificity to define which pin you're using. Might something like the following encompass future options better, or are you not concerned with that possibility right now?

    P0 PIN A[noparse][[/noparse]0] INPUT 
    P1 PIN A[noparse][[/noparse]1] INPUT
    
    or
    
    P0 APIN 0 INPUT 
    P1 APIN 1 INPUT
    
    
    



    -Phil
  • BeanBean Posts: 8,129
    edited 2010-03-30 20:08
    Phil,
    I figured I would handle the port "B" pins as 32 thru 63.

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Use BASIC on the Propeller with the speed of assembly language.
    PropBASIC thread http://forums.parallax.com/showthread.php?p=867134

    March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There are two rules in life:
    · 1) Never divulge all information

    Post Edited (Bean) : 3/30/2010 8:51:34 PM GMT
  • AribaAriba Posts: 2,690
    edited 2010-03-30 21:55
    JonnyMac said...
    Ariba said...
    __DO_1                                                       '  DO
     
                      or            outa,P16                     '    P16 = 1
     
                      and           P0,ina WZ, NR              
                      and           P1,ina WC, NR              
        IF_C_EQ_Z     jmp           #__DO_1 
    
    


    Am I wrong, or shouldn't the last instruction be IF_C_NE_Z? When P0 is high (1) the Z flag will get zero; when P1 is high the C flag will be set. Likewise, when P0 is low the Z flag will be set; when P1 is low the C flag will be clear.

    You're right, the Z flag is inverse to the bit. But my version can be used for LOOP UNTIL [noparse];)[/noparse]

    Andy
Sign In or Register to comment.