WHILE/ENDW Example

MPASM Assembler

WHILE/ENDW Example

Directives highlighted in this example are:

  • while
  • endw
  • Program Functional Description

    This example shows the usefulness of directive while to perform a loop while a certain condition is true. This directive is used with the endw directive.

    Commented Code Listing

    list p=16f877 ;Select the device.

    #include <p16f877.inc> ;Include standard header file

    ;for the selected device.

    variable i ;Define the symbol 'i' as a

    ;variable.

    reg_hi equ 20 ;Assign value 20H to label

    ;reg_hi.

    reg_lo equ 21 ;Assign value 21H to label

    ;reg_lo.

    ORG 0000 ;The following code will be

    ;programmed in reset address 0.

    goto start ;Jump to an address whose label

    ;is 'start'.

    shift_right macro by_n ;Beginning of a macro, which

    ;shifts register data n times.

    ;Code length generated after

    ;assembly, varies depending upon

    ;the value of parameter 'by_n'.

    i=0 ;Initialize variable i.

    while i< by_n ;Following 3 lines of assembly

    ;code are repeated as long as

    ;i< by_n.

    bcf STATUS,C ;Clear carry bit.

    rrf reg_hi ;reg_hi and reg_lo contains

    rrf reg_lo ;16-bit data which is rotated

    ;right through carry.

    i+=1 ;Increment loop counter i.

    endw ;End while loop. The loop will

    ;break here after i=by_n.

    endm ;End of 'shift_right' macro.

    org 0010 ;My main program starts at 10H.

    start ;The label 'start' is equal to

    ;10H.

    shift_right 3 ;Shift right 3 times the 16-bit

    ;data in reg_hi and reg_lo. This

    ;is an example. A value 8 will

    ;shift data 8 times.

    goto $

    end


    Microchip Technology Inc.
    Microchip's Web Site
    Voice: (480) 792-7200
    Fax: (480) 899-9210
    Microchip's E-mail Address
    PreviousNext