Shop OBEX P1 Docs P2 Docs Learn Events
[RESOLVED] IF_NZ pickle — Parallax Forums

[RESOLVED] IF_NZ pickle

MightorMightor Posts: 338
edited 2007-09-09 21:15 in Propeller 1
Hey peeps,

I'm in a bit of a pickle but I am pretty sure this has happened to some of you before. I have the following bit of code:
[img]http://forums.parallax.com/images/smilies/tongue.gif[/img]wmloop      mov dutyold, duty1
              rdlong duty1, dc1Addr             ' get an up to date duty cycle % for motor 1
              cmp dutyold, duty1 wz             ' If Z is set then they're different, so recalculate
IF_NZ         mov multiplicand, pwmfbase
IF_NZ         mov multiplier, duty1
IF_NZ         jmpret multiply_ret, #multiply
IF_NZ         mov pulsewidth1, product   



If you look at the last line you'll see that is also preceded by an IF_NZ, however, I cannot guarantee that the multiply routine does not set the Z flag. In fact, there's a really good chance it is. So.... how do I allow the last mov command to be conditional like the rest?

Gr,
Mightor

Here's the multiply routine for clarity:
'**********************************************************
'
' Multiply two factors, product may not exceed 32 bit
'
multiply      ' Check if multiplier > multiplicand, if so, switch them around                                    
              cmp multiplier, multiplicand wc, wz 
IF_A          mov product, multiplier           
IF_A          mov multiplier, multiplicand      
IF_A          mov multiplicand, product         
              mov product, #0                   ' Always clear product in case we run this twice
                                                 
                                                 
:multiply_loop                   
              shr multiplier , #1 wc, wz        ' Shift multiplier right by one                           
if_c          add product, multiplicand         ' If C =1, add multiplicand to product                           
              shl multiplicand, #1              ' Shift multiplicand left by 1                              
if_nz         jmp #:multiply_loop               ' Loop, but only if there are digits left in multiplier
     
:multiply_ret jmp multiply_ret


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
| To know recursion, you must first know recursion.
| I'm afraid I might be phobophobic.

Post Edited (Mightor) : 9/9/2007 7:45:20 PM GMT

Comments

  • hippyhippy Posts: 1,981
    edited 2007-09-09 17:44
    Brute force ... ?
    [img]http://forums.parallax.com/images/smilies/tongue.gif[/img]wmloop      mov dutyold, duty1
                  rdlong duty1, dc1Addr             ' get an up to date duty cycle % for motor 1
                  cmp dutyold, duty1 wz             ' If Z is set then they're different, so recalculate
    IF_NZ         mov multiplicand, pwmfbase
    IF_NZ         mov multiplier, duty1
    IF_Z          jmp #:skipit
                  jmpret multiply_ret, #multiply
                  mov pulsewidth1, product 
    :skipit  
    
    


    If 'multiply' is only called here ( or Z on return doesn't matter other times it's called ), you could always have it clear the Z flag before return.
  • potatoheadpotatohead Posts: 10,260
    edited 2007-09-09 18:40
    Have the multiply routine save the flag state, then restore?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
  • MightorMightor Posts: 338
    edited 2007-09-09 19:41
    The jmp idea is great. I will probably put it right under the cmp though. Thanks for that suggestion. It's very simple and effective, just the way I like them [noparse]:)[/noparse]

    I think the save and restore is going to make my code more complicated than it has to be. I'm only just starting out with ASM, so I want to keep it as simple as I can. Thanks for the suggestion though [noparse]:)[/noparse]

    Gr,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | To know recursion, you must first know recursion.
    | I'm afraid I might be phobophobic.
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-09 21:15
    This has been a good discussion!
    It shows to me again:
    There are different coding styles with different kinds of processors.
    The answers given by Hippy and Potatohead are the "classical" answers from other processors:
    - jump over the code
    - push/pop flags

    Interestingly both techniques are in many cases not the best ones for the Prop:
    It has no push/pop but conditional instructions need no extra space (though time!)

    I think Mightor's decision is not only the most conservative, but also the clearest one in this situation smile.gif
Sign In or Register to comment.