C4Gen logo


Making programming easier
Content

PPMacros / Examples >
Home
PPMacros
FAQ
Download
Examples
Quick Guide
Macro list
Overview
Links
Contact
C++
Forum




Examples

Here is an example. You can find more examples on the download page. This example calculates the square root of a 32-bit integer, using a simple algoritm.

; SimpleSqrt.asm

; SimpleSqrt.asm
;   An example for ppmacros
;   
                include         "p16f88.inc"
                include         "ppmacros.inc"

; SET TABSIZE to 16 (edit/properties/sizes)

                CBLOCK 0x20
                  SI:4, SO:4, V1:4, Sold:4

                  _PPMCR1_,  _PPMCR2_, _PPMCR3_
                  _PPMCR4_,  _PPMCR5_, _PPMCR6_
                  _PPMCR7_,  _PPMCR8_, _PPMCR9_
                  _PPMCR10_,  _PPMCR11_, _PPMCR12_
                  _PPMCR13_
                ENDC


;**************************************************************
;***                      Begin CODE                        ***
;**************************************************************

                org             0000h
                goto            main

;**************************************************************
;***                    Interrupt handler                   ***
;**************************************************************

                nop
                nop
                nop
                retfie

main:

;**************************************************************
;***                     Main program                       ***
;**************************************************************
                
                load_f32_l32    SI, .1963964721
                call            SQRT
                nop                             ; place breakpoint here
                                                ; check SO in watch window
                                                ; Result should be AD1C
                for_f32_l32_l32 SI,.0,.4000000000
                  call          SQRT
                  nop                           ; place breakpoint here
                                                ; check SI and SO in watch window / file register
                next
lp99            nop
                goto            lp99

;**************************************************************
;***                     Subroutines                        ***
;**************************************************************

; SQRT - the simple (but stupid) way.
; input: SI, output:SO, uses: V1 (32 bit)
;
;   The simple sqrt algorthime does work as follow:
;     Divide the number by two
;     Avarage the result with '2'
;     Use this number instead of the two above and repeat until the result is stable
;     Examples:
;       100/2=50. (50+2)/2=26.
;       100/26=3. (26+3)/2=14.
;       100/14=7. (14+7)/2=10.
;       100/10=10. (10+10)/2=10.
;
;       120/2=60. (60+2)/2/31.
;       120/31=3. (31+3)/2=17.
;       120/17=7. (17+7)/2=12.
;       120/12=10. (12+10)/2=11.
;       120/11=10. (11+10)/2=10.
;
SQRT:           if_f32_gt_l32       SI,.1
                  f32_f32_div_l32   SO,SI,.2      ; start value of guess is SI/2
                  repeat
                    load_f32_f32    Sold,SO
                    f32_f32_div_f32 V1,SI,SO      ; V1 = SI/SO
                    f32_f32_add_f32 SO,SO,V1      ; SO = (SO + V1) /2
                    f32_f32_div_l32 SO,SO,.2
                    if_f32_eq_f32   SO,Sold
                      load_f32_f32  V1,SO
                    end_if
                  until_f32_eq_f32  SO,V1         ; loop is finished if SO equal to V1
                else_if
                  load_f32_f32      SO,SI         ; if <2 then output = input
                end_if
                return

                END