; ; strcnt subroutine example ; INCLUDE "p16f873.inc" ; Register Usage CBLOCK 0x020 ; s1:16,s2:16 ; reserve 32 bytes in data memory for strings s1,s2 ENDC CBLOCK 0x050 ; parameter, local storage block for strcnt subroutine i,ptr; ENDC org 0 call init_s1 ;; copy string in program mem to s1 data mem call init_s2 ;; copy string in program mem to s2 data mem ;; set up call for strcnt movlw s1 movwf ptr ;; set ptr = s1 call strcnt ;; do strcnt ;; cnt value is stored in 'i', returned in 'w' here goto here ;;strcnt subroutine ;; example that counts number of characters in string s1 ;; count does not include 0 byte at end of string strcnt clrf i ;; clear i movlw s1 movwf FSR ;; point FSR as s1 strcnt_loop: movf INDF,w ;; get current character btfsc STATUS,Z ;; check if zero return ;; finished incf i,f ;; increment char count incf FSR,f ;; increment pointer to next character goto strcnt_loop ;;; utility subroutines ;;; complicated mess to copy strings from program memory ;;; to data memory ;; temporary storage for init string routine CBLOCK 0x070 ; registers start after special registers tablo; tabhi; ENDC ;; these strings are in program memory which is ROM ;; to change them, must copy them to data memory first. s1const dt "Upper/LOWER.",0 s2const dt "mIXeD CaSe..",0 init_s1 movlw s1 movwf FSR ;; point FSR as s1 movlw high s1const movwf tabhi movlw low s1const ;;; initialize table low,high to s1const movwf tablo call init_str return init_s2 movlw s2 movwf FSR ;; point FSR as s2 movlw high s2const movwf tabhi movlw low s2const ;;; initialize table low,high to s1const movwf tablo call init_str return ;; FSR must be pointing to where string is to be stored ;; destination assumed to be in bank0 init_str call getbyte ;; returns in W movwf INDF ;; store byte to string using indirect movf INDF,F ;; test byte just moved btfsc STATUS,Z ;; check if zero return incf FSR, F ;; increment to next location goto init_str getbyte movf tabhi,w movwf PCLATH movf tablo,w incf tablo,f ;; go ahead and increment btfsc STATUS,Z incf tabhi,f ;; increment high address movwf PCL ;; get the byte, note this returns to calling routine end