Shop OBEX P1 Docs P2 Docs Learn Events
assembly code query — Parallax Forums

assembly code query

kenmackenmac Posts: 96
edited 2008-10-01 16:38 in Propeller 1
Hi folks,
I'm playing with some simple assembly code found in the Manual.
The following code operates correctly and toggles a pin at a set rate:

:loop            waitcnt       Time,     Delay         'Wait
                    xor        outa,     Pin           'Toggle Pin
                    jmp                   #:loop       'Loop endlessly



However, if the toggle action occurs before the delay, as in the following, it doesn't work - it just sets the pin high continuously.

:loop               xor         outa,     Pin            'Toggle Pin 
                    waitcnt     Time,     Delay          'Wait
                    jmp                    #:loop        'Loop endlessly



Why doesn't the second version work?
The code is part of assemblytoggle.spin in page 340 of the manual.

kenmac

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Perth, Western Australia
Time Zone = GMT + 8

Comments

  • BaggersBaggers Posts: 3,019
    edited 2008-10-01 10:03
    probably because the counter has already passed the delay, so will wait ~53 seconds before exiting the waitcnt instruction ( depending on clock speed )

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-10-01 16:22
    This is something that frequently hangs people when they make the transition from spin to assembly. In spin the addition done (ie waitcnt(delay + cnt)) is a PRE operation, IOW the addition happens before waitcnt is called. The addition done in the assembly waitcnt is a POST operation, meaning in waitcnt value,delay; delay is added after the waitcnt has been performed. Also I don't see where you incorporate the current counter value anywhere. When doing waitcnt in assembly there are two operations you should perform before calling waitcnt, 1st grab a copy of the counter value, then add the delay period. You are then ready to execute the waitcnt.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-10-01 16:38
    kenmac: The minimum delay of 9 in the example code assumes you will execute the first WAITCNT immediately afterward. You may have to increase this value by at least 4 to allow time for the XOR to execute before the first instance of WAITCNT.· Otherwise by the time XOR executes the·clock·has surpassed·the wait value and the·first delay will be 53.6 seconds at 80MHz while the clock counts all the way around to the wait value again.··Subsequent delays will be 6_000_000 clocks.·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup

    Post Edited (Ken Peterson) : 10/1/2008 4:46:47 PM GMT
Sign In or Register to comment.