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

"StackOps" Experiment

Data stacks are not "natively" implemented as part of the low-end and mid-range PICmicro MCU architectures. They can be implemented in the PIC17CXX and PIC18CXX architectures quite easily with the post-increment and pre-decrement functions built into the INDF/FSR registers. This does not mean that Arrays cannot be easily implemented in the low-end and mid-range PICmicros.

To "push" data onto a Stack in these devices, the recommended macro for doing this is:

DataPush Macro			;  "Push" the Contents of "w" onto the FSR Stack
  incf   FSR, f
  movwf  INDF
 endm
              
while the recommended "Pop" is:
DataPop Macro			;  "Pop" the Contents of "w" onto the FSR Stack
  movf   INDF, w
  decf   FSR, f
 endm
              

The "StackOps" experiment demonstrates how pushes and pops can be implemented, but reverses the order of the instructions shown in the macros above. The reason why the macros above are recommended is if an interrupt is requested and acknowledged while the "DataPush" code is executing, then any DataPush operations within in the macro will not overwrite any data that has been stored in the mainline.

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

 title  "StackOps - Using a Stack during Arithmetic Calculations"
;
;  This Program shows how to use a stack for saving intermediate values
;   during complex mathematical operations.
;
;  99.11.13 - Updated for Second Edition
;           - Deleted Table in Original
;
;  Myke Predko
;  96.05.10
;
  LIST P=16F84, F=INHX8M, R=DEC
  INCLUDE "p16f84.inc"

;  Registers
a     equ 12                    ;  Registers to Operate on
bi    equ 13                    ;  "b" is an MPASM psuedo-Op
c     equ 14
d     equ 15
Stack equ 16                    ;  Stack for Operations (two bytes)

 __CONFIG _CP_OFF & _WDT_OFF & _RC_OSC

  PAGE
;  Mainline of StackOps

  org 0

  movlw  3                      ;  Initialized the Variables
  movwf  a
  movlw  5
  movwf  bi
  movlw  7
  movwf  c
  movlw  9
  movwf  d

  movlw  Stack                  ;  Set up the Stack Pointer
  movwf  FSR

;  d = ((( a << 1 ) + bi ) << 1 ) + (( a >> 1 ) - c )

  rlf    a, w                   ;  Get values in the 1st set of Brackets
  addwf  bi, w
  movwf  INDF
  rlf    INDF, f
  incf   FSR, f

  rrf    a, w                   ;  Get the Values in the 2nd set of Brackets
  movwf  INDF                   ;  Do the Temporary Storage
  movf   c, w                   ;  Subtract "c" from the Shifted Value
  subwf  INDF, w

  decf   FSR, f                 ;  Finally, Add the Two Values together and
  addwf  INDF, w                ;   Store
  movwf  d

;  c = (( a << 1 ) - ( bi - d )) + ( 37 + d )

  movf   a, w                   ;  Get the First Value
  movwf  INDF
  rlf    INDF, f		;  #### - See text for Comments about this Line
  incf   FSR, f

  movf   d, w                   ;  Now, do the next Value
  subwf  bi, w                  ;  Do the Subtraction
  movwf  INDF                   ;  Store on the Stack
  incf   FSR, f

  movlw  37                     ;  Get the Last Value First
  addwf  d, w
  movwf  INDF
  incf   FSR, f

  decf   FSR, f                 ;  With the values in Parenthesis, Put them together
  decf   FSR, f			;  Get Subtract Value First
  movf   INDF, w
  incf   FSR, f			;  Subtract it From the Last Value
  subwf  INDF, w
  decf   FSR, f                 ;  Add the Result to the First Value
  decf   FSR, f
  addwf  INDF, w
  movwf  c                      ;  Store the Result

Finished                        ;  Finished, Just Loop Around Forever
  goto  $


  end
              

Click Here to look at the eleventh experiment - FirstCal