Specifications

mikroElektronika | Free Online Book | PIC Microcontrollers | Appendix A: Programming a Microcontroller
In the event that a macro has labels, they must be defined as local ones by using directive local. The given example
contains macro which calls a subroutine (call label in this case) if the Carry bit of the STATUS register is set.
Otherwise, the first following instruction is executed.
SUBROUTINES
Asubroutine contains a sequence of instructions, begins with a label (subroutine_name) and ends with command return or
retlw. The main difference comparing to macro is that subroutine is not replaced by its code in the program, but program
jumps to subroutine to execute it. It happens every time the assembler encounters command call Subroutine_name in the
program. On the command return, it leaves a subroutine and continues execution from where it left off the main program.
Subroutine may be defined both prior to or upon the call.
As seen, concerning macros, the input and output arguments are of great importance. Concerning subroutines, it is not
possible to define arguments within the subroutine itself. However, variables predefined in the main program may be used
as subroutine arguments.
A logical sequence of events is as follows: defining variables, calling subroutine which uses them and at the end reading
variables changed upon the execution of subroutine.
The program in the following example performs addition of two 2-byte variables ARG1 and ARG2 and moves result to the
variable RES. When 2-byte variables are used, it is necessary to define higher and lower byte for each of them. The
program itself is very simple. It first adds lower bytes of variables ARG1 and ARG2 and higher afterwards. If the sum of
addition of two lower bytes is greater than 255 (maximal byte value) the remainder is added to the RESH variable.
; Program to add two 16-bit numbers
; Version: 1.0 Date: April 25, 2007 MCU:PIC16F887
PROCESSOR 16f887 ; Defining processor
#include "p16f887.inc" ; Microchip INC database
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
Cblock 0x20 ; Beginning of RAM
ARG1H ; Argument 1 higher byte
ARG1L ; Argument 1 lower byte
ARG2H ; Argument 2 higher byte
ARG2L ; Argument 2 lower byte
RESH ; Result higher byte
RESL ; Result lower byte
endc ; End of variables
ORG 0x00 ; Reset vector
goto Start
Start ; Write values to variables
movlw 0x01 ; ARG1=0x0104
movwf ARG1H
movlw 0x04
movwf ARG1L
movlw 0x07 ; ARG2=0x0705
movwf ARG2H
movlw 0x05
movwf ARG2L
http://www.mikroe.com/en/books/picmcubook/appa/ (9 of 21)5/3/2009 11:35:35 AM