PDA

View Full Version : [RESOLVED] IF_NZ pickle

Mightor
09-10-2007, 12:35 AM
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:

http://forums.parallax.com/images/smilies/tongue.gifwmloop 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

hippy
09-10-2007, 12:44 AM
Brute force ... ?

http://forums.parallax.com/images/smilies/tongue.gifwmloop 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.

09-10-2007, 01:40 AM
Have the multiply routine save the flag state, then restore?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Wiki: Share the coolness! (http://propeller.wikispaces.com/)

Mightor
09-10-2007, 02:41 AM
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 :)

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 :)

Gr,
Mightor

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

deSilva
09-10-2007, 04:15 AM
This has been a good discussion!
It shows to me again:
There are different coding styles with different kinds of processors.