PDA

View Full Version : Code Optimization



Kye
01-20-2009, 02:59 AM
Hello, all I've been busting my head on how to do something in asm much more simply, but I just can't seem to get it done.

Here's the code..



muxnz keyBuffer, #1 ' Backup Z flag.

cmp keyArrayCounter, #2 wz ' Wait a little while to synchronize.
if_nz cmp keyArrayCounter, #6 wz '
if_nz cmp keyArrayCounter, #10 wz '
if_nz cmp keyArrayCounter, #14 wz '
if_nz cmp keyArrayCounter, #18 wz '
if_nz cmp keyArrayCounter, #22 wz '
if_z waitcnt keyTimeBuffer, clockWait '

test keyBuffer, #1 wz ' Restore Z flag.

Basically, I need to use waitcnt for a state machine at specific times·in my code. The piece above is run inside a loop that loops 26 times. Every four loops I need to execute the waitcnt which is what this code does.

Now, I want to make it more optimized by using the c flag only so I don't need to backup the z flag since I'm using it within the loop. The problem is however, that there's a sweet spot. The code above executes that sweet spot. To say, waitcnt must only be executed on the 22, 18, 14, 10, 6, 2 parts in the loop. Note that the loop counts down from 26 so the execution of the coditional for 2 is last and 26 is first.

I can't seem to figure out how to do this with the c flag...

Any ideas?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,

lonesock
01-20-2009, 03:30 AM
Untested, off the top of my head:




mov tmp, keyArrayCounter
sub tmp, #2
and tmp, #3
tjnz tmp, #skip_pause
waitcnt keyTimeBuffer, clockWait
skip_pause


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

Kye
01-20-2009, 03:43 AM
Wow, I think that will work, thanks!

Any other ideas anyone?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,

lonesock
01-20-2009, 03:50 AM
YW, and maybe this will work:




mov tmp, keyArrayCounter
rev tmp, #30
djnz tmp, #:skip_pause
waitcnt keyTimeBuffer, clockWait
:skip_pause




Reversing the lower 2 bits of tmp will zero out all upper 30 bits, and will leave a 1 in tmp if the original value would have been a 2. Then the DJNZ jump to :skip_pause if the value of tmp was not 1.

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

mpark
01-20-2009, 05:03 AM
Wow Lonesock! Nice one!

lonesock
01-20-2009, 05:35 AM
Thanks.

* bows *


[8^)

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