CBLOCK/ENDC Example

MPASM Assembler

CBLOCK/ENDC Example

Directives highlighted in this example are:

  • cblock
  • endc
  • Program Functional Description

    This example shows the usage of CBLOCK and ENDC directives for defining constants or variablers in data memory space. The same directives can be used for program memory space also.

    The program calculates the perimeter of a rectangle. Length and width of the rectangle will be stored in buffers addressed by length (22H) and width (23H). The calculated perimeter will be stored in the double-precision buffer addressed by perimeter (i.e.20H and 21H).

    Commented Code Listing

    list p=16f877 ;Select the device.

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

    ;for the selected device.

    CBLOCK 0x20 ;Starting address of program or

    ;data memory space. Here the value

    ;is 20H, which is in data memory

    ;space.

    perimeter:2 ;The label perimeter is 2-byte

    ;wide. Address 20H and 21H is

    ;assigned to the label perimeter.

    length ;Address 22H is assigned to the

    ;label length.

    width ;Address 23H is assigned to the

    ;label width.

    ENDC ;This directive must be supplied

    ;at the end of CBLOCK list to

    ;terminate the list.

    clrf perimeter ;Clear the buffer addressed by

    ;'perimeter' i.e. address 20H.

    clrf perimeter+1 ;Clear address 21H.

    movf length,w ;Move the data present in the

    ;register addressed by 'length'

    ;to 'w'

    addwf width,w ;Add data in 'w' with data in the

    ;register addressed by 'width'.

    movwf perimeter ;Move 'w' to the register

    ;addressed by 20H.

    incfsz perimeter+1 ;Increment register 21H if carry

    ;is generated.

    bcf STATUS,C ;Clear carry bit in STATUS

    ;register.

    rlf perimeter+1

    rlf perimeter

    incfsz perimeter+1 ;High byte of perimeter is in

    ;21H and low byte is in 20H.

    goto $

    end


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