Myke's Home Page

Book CD-ROM Home

File Copying/Harddrive Setup

Development Tools

Experiments

Projects

Useful Code Snippets and Macros

Introduction to Electronics

Introduction to Programming

Datasheets

PCBs

Links

McGraw-Hill Professional Publishing

"FirstCal" Experiment

The simplest method of passing data to a subroutine is through a register. In the PICmicro, this can be done by either passing a byte parameter between the calling code and the subroutine using the "w" register or using a temporary file register common to both the caller and the routine. Passing data to a subroutine using “w” is always safe, but care has to be taken for returning parameters if “w” is to be used.

The source code listed below can be accessed from the CD-ROM by clicking Here.

 title  "FirstCal - Passing Subroutine Parms via Registers"
;
;  This Program shows how to Pass Parameters back and forth between the
;   Mainline and Routines using Registers.
;
;  99.11.13 - Updated for Second Edition
;
;  Myke Predko
;  96.05.10
;
  LIST P=16F84, F=INHX8M, R=DEC
  INCLUDE "p16f84.inc"

;  Registers
i     equ 12                    ;  Registers to Operate on
j     equ 13
Temp  equ 14

 __CONFIG _CP_OFF & _WDT_OFF & _RC_OSC

  PAGE
;  Mainline of FirstCal

  org 0

  movlw  3                      ;  Initialized the Variables
  movwf  i
  movlw  5
  movwf  j

;  j = increment( i )

;  Pass Parameters using the "w" Register

  movf   i, w                   ;  Get value to Increment
  call   increment_w            ;  Increment using the 'w' Register
  movwf  j

;  Pass Parameters using Temporary Registers

  movf   i, w                   ;  Get the Value to Increment
  movwf  Temp
  call   increment_Temp         ;  Increment the Temp Value
  movf   Temp, w                ;  Store the Incremented Value
  movwf  j

Finished                        ;  Finished, Just Loop Around Forever
  goto  $

;  Increment Subroutines

increment_w                     ;  Increment the value in "w"
  addlw  1                      ;  Add one to Value in "w"
  return

increment_Temp                  ;  Increment the Temporary Value
  movlw  1
  addwf  Temp, f
  return

  end
              

FirstCal was modified into FirstC54.asm to demonstrate that the "return" statement can be used with the MPLAB IDE assembler and not get a failure. Repeat the simulation experiment with FirstC54 to see the "retlw 0" instruction replacing the "retun" statement. Depending on the version of the MPLAB IDE you are working with the assembly may return a warning - but an error is never returned. This is one of the reasons why I always make sure my applications assemble "cleanly".

 title  "FirstC54 - Passing Subroutine Parms via Registers"
;
;  This Program is the same as "FirstCal" except the device 
;   has been changed from a mid-range to a low-end.  
;
;  99.11.13 - Created
;
;  Myke Predko
;
  LIST P=16C54, F=INHX8M, R=DEC
  INCLUDE "p16c5x.inc"

;  Registers
i     equ 12                    ;  Registers to Operate on
j     equ 13
Temp  equ 14

 __CONFIG _CP_OFF & _WDT_OFF & _RC_OSC

  PAGE
;  Mainline of FirstCal

  org 0

  movlw  3                      ;  Initialized the Variables
  movwf  i
  movlw  5
  movwf  j

;  j = increment( i )

;  Pass Parameters using the "w" Register

  movf   i, w                   ;  Get value to Increment
  call   increment_w            ;  Increment using the 'w' Register
  movwf  j

;  Pass Parameters using Temporary Registers

  movf   i, w                   ;  Get the Value to Increment
  movwf  Temp
  call   increment_Temp         ;  Increment the Temp Value
  movf   Temp, w                ;  Store the Incremented Value
  movwf  j

Finished                        ;  Finished, Just Loop Around Forever
  goto  $

;  Increment Subroutines

increment_w                     ;  Increment the value in "w"
  movwf  Temp			;  - Save in "Temp" for Update
  movlw  1                      ;  Add one to Value in "w"
  addwf  Temp, w
  return

increment_Temp                  ;  Increment the Temporary Value
  movlw  1
  addwf  Temp, f
  return

  end
              

Click Here to look at the twelfth experiment - StakCall