Assembler Structured Control-flow Quick Reference

The full tutorial on how it works

The source code for the IAR MSP430 version of the macros

When you #include the above header file, the following control-flow structures become available in assembly language.

 

Conditional branches

 
        <test>
        _IF cc
            ... 
        _ENDIF
 
 
        <test>
        _IF cc
            ...
        _ELSE 
            ...
        _ENDIF
 
 
Where cc is one of Z, NZ, EQ, NE, C, NC, HS, LO, N, NN, L, GE or NEVER.
 
 

Uncounted loops

 
        _REPEAT
            ...
        _FOREVER               ; Infinite
 
 
        _REPEAT
            ...
            <test>             ; Post-tested
        _UNTIL cc
 
 
        _DO
            <test>             ; Pre-tested
        _WHILE cc
            ...
        _ENDW
 
 
        _DO
            ...
            <test>             ; Mid tested
        _WHILE cc
            ...
        _ENDW

 

 

Any loop may have additional _WHILE s to exit it, but each additional one must be balanced by an _ENDIF (or _ELSE ... _ENDIF) after the end of the loop. Examples:

 

_DO     ... _WHILE cc1 ... _WHILE cc2 ...    _ENDW ... _ENDIF

_REPEAT ... _WHILE cc1 ...              _UNTIL cc2 ... _ELSE  ... _ENDIF

_REPEAT ... _WHILE cc1 ... _WHILE cc2 ... _FOREVER ... _ENDIF ... _ENDIF

 

 

Counted loops

 

Typical use:

                        ; Word counter, single decrement, 0 means 65536 times

        _FOR  #10,R8

            ...                ; Will be done 10 times.

        _NEXT_DEC  R8          ; Must use same destination as matching _FOR

 

                        ; Word counter, double decrement, 0 means 32768 times

        _FOR  #10,R9           ; Must be even

            ...                ; Will be done 5 times.

        _NEXT_DECD  R9         ; Minus 2 each time, so R9 takes on only even values.

 

                        ; Byte counter, single decrement, 0 means 256 times

        _FOR_B  #10,R8

            ...                ; Will be done 10 times.

        _NEXT_DEC_B  R8        ; Must use same destination as matching _FOR_B

 

                        ; Byte counter, double decrement, 0 means 128 times

        _FOR_B  #10,R9         ; Must be even

            ...                ; Will be done 5 times.

        _NEXT_DECD_B  R9       ; Minus 2 each time, so R9 takes on only even values.

 

 

Short-circuit-AND

 

        _COND

               <test1>

        _AND_IF cc1

               <test2>

        _AND_IF cc2

               <test3>

        _AND_IF cc3

               ...

        _ENDIFS                ; Note the plural

 

 

Short-circuit-AND with ELSE

 

        _COND

            <test1>

        _AND_IF cc1

            <test2>

        _AND_IF cc2

            <test3>

        _AND_IF cc3

            ...

        _ELSES                 ; Note the plural

            ...

        _ENDIF                 ; Note the singular. Use the plural _ENDIFS only if there is no _ELSES clause

 

 

Short-circuit OR

 

        _COND

            <test1>

        _OR_ELSE cc1

            <test2>

        _OR_ELSE cc2

            <test3>

        _OR_IFS  cc3           ; Note the plural

            ...

        _ENDIF

 

 

Short-circuit OR with ELSE

 

        _COND

            <test1>

        _OR_ELSE cc1

            <test2>

        _OR_ELSE cc2

            <test3>

        _OR_IFS  cc3           ; Note the plural

            ...

        _ELSE

            ...

        _ENDIF

 

 

Short-circuit AND/OR

 

        _COND

            <test1>

        _AND_IF cc1

        _COND                  ; Note the second _COND

            <test2>

        _OR_ELSE cc2

            <test3>

        _OR_IFS  cc3           ; Note the plural

            ...

        _ENDIFS                ; Note the plural

 

 

Short-circuit AND/OR with ELSE

 

        _COND

            <test1>

        _AND_IF   cc1

        _COND                  ; Note the second _COND

            <test2>

        _OR_ELSE  cc2

            <test3>

        _OR_IFS   cc3          ; Note the plural

            ...

        _ELSES                 ; Note the plural

            ...

        _ENDIF                 ; Note the singular. Use the plural _ENDIFS only if there is no _ELSES clause

 

 

 

Comparison of short-circuit structures (plurals shown red and green)

 

_COND <test1>  _AND_IF cc1       <test2>  _AND_IF cc2 <test3> _AND_IF  cc3 ... _ENDIFS
_COND <test1>  _AND_IF cc1       <test2>  _AND_IF cc2 <test3> _AND_IF  cc3 ...  _ELSES ... _ENDIF
_COND <test1> _OR_ELSE cc1       <test2> _OR_ELSE cc2 <test3>  _OR_IFS cc3 ... _ENDIF
_COND <test1> _OR_ELSE cc1       <test2> _OR_ELSE cc2 <test3>  _OR_IFS cc3 ...  _ELSE  ... _ENDIF

_COND <test1>  _AND_IF cc1 _COND <test2> _OR_ELSE cc2 <test3>  _OR_IFS cc3 ... _ENDIFS

_COND <test1>  _AND_IF cc1 _COND <test2> _OR_ELSE cc2 <test3>  _OR_IFS cc3 ...  _ELSES ... _ENDIF

 

 

Else-If chain

  

        _COND

            <test1>

        _IF cc1

            ...

        _ELSE

            <test2>

        _IF cc2

            ...

        _ELSE

            <test3>

        _IF cc3

            ...

        _ELSE

            ...

        _ENDIFS                ; Note the plural

 

 

Case statement

 

Typical use:

 

        _CASE

            _OF_EQ   #1,R8     ; Uses a word comparison

                ...

            _ENDOF

 

            _OF_EQ_B #’A’,R8   ; Uses a byte comparison

                ...

            _ENDOF

 

            <test1>            ; A general test

            _OF cc1

                ...

            _ENDOF

 

            ...                ; The default case

        _ENDCASE

 

 

Structure balance check (at end of program or subroutine)

 

       _CS_CHECK

 

-- Dave Keenan, 2018-Jan-10 (last updated 2018-Nov-14)

thing.gif