CNT

Register: System Counter register.

DAT
Label > < Condition > Instruction DestOperandCNT Effects >

  • Condition is an optional execution condition. See Common Syntax Elements.
  • Instruction is the desired assembly instruction. CNT is a read-only register and thus should only be used in the instruction's source operand.
  • DestOperand is a constant expression indicating the register that is operated on, and optionally written to, using the value of CNT in the instruction's source operand.

Explanation

The CNT register contains the current value in the global 32-bit System Counter. The System Counter serves as the central time reference for all cogs; it increments its 32-bit value once every System Clock cycle.

CNT is a read-only pseudo-register; when used as an instruction's source operand, it reads the current value of the System Counter. Do not use CNT as the destination operand; that only results in reading and modifying the shadow register whose address CNT occupies.

CNT is often used to determine an initial target value for a WAITCNT-based delay. The following code performs an operation in a loop every ¼ second. See Registers, and the Spin language CNT, for more information.

DAT
                org 0                         'Reset assembly pointer
AsmCode         rdlong     Delay,    #0       'Get clock frequency
                shr        Delay,    #2       'Divide by 4
                mov        Time,     cnt      'Get current time
                add        Time,     Delay    'Adjust by 1/4 second
Loop            waitcnt    Time,     Delay    'Wait for 1/4 second
                '<more code here>             'Perform operation
                jmp        #Loop              'loop back 

Delay    res    1
Time     res    1

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.