Shop OBEX P1 Docs P2 Docs Learn Events
[resolved][puzzle] jumping counter — Parallax Forums

[resolved][puzzle] jumping counter

kuronekokuroneko Posts: 3,623
edited 2009-12-12 23:49 in Propeller 1
Apologies if this is common knowledge but I just found out about it and share it anyway [noparse]:)[/noparse]

What's the result returned to the caller (through par) in the following code fragment and what label does it jump to (:_5, :_6 or :_7)?

For example, (%1000_0111 and :_7) would be a valid - but wrong - answer. Solution: label :_5, result $85. Who would have guessed [noparse]:)[/noparse]

Also, don't be scared about anything before jmp cnt. The reason for having this code in there is to make sure that the lower 9 bits of cnt are 0 or slightly higher but in a controlled range. A waitcnt vscl, #0 would have done the job just as well but depending on clock speed might take a while (therefore the imposed limit of 257..512).

Note: jmp cnt/phsx is confined to one of the jump targets :_5, :_6 or :_7, i.e. it doesn't go off into nowhere.

DAT             org     0

start           movd    cnt, #2                 ' get some room
                add     cnt, cnt                ' min 257 cycles
                andn    cnt, #%111111111        ' max 512 cycles
                     
                waitcnt cnt, #0                 ' wait for $XXXXX(%xxx0)00
                jmp     cnt

:_5             add     outb, #%001
:_6             add     outb, #%010
:_7             add     outb, #%100

                or      outb, #%10000000        ' indicate result ready
                wrlong  outb, par
                
                cogid   cnt
                cogstop cnt

                fit


For people uncomfortable with waitcnt & Co here a version with ordinary counters. Functionality is the same.

DAT             org     0

start           movi    ctra, #%0_11111_000
                mov     frqa, #1
                mov     phsa, #3
                jmp     phsa

                nop
:_5             add     outb, #%0001
:_6             add     outb, #%0010
:_7             add     outb, #%0100

                or      outb, #%10000000
                wrlong  outb, par
                
                cogid   cnt
                cogstop cnt

                fit

Post Edited (kuroneko) : 12/9/2009 6:03:13 AM GMT
«1

Comments

Sign In or Register to comment.