Code Examples
The following is extracted from the example multiply routines given as a sample with MPASM assembler. Most of the comments have been stripped for brevity.
Absolute Code becomes Relocatable Code, Calling File and Relocatable Code, Library Routine.
Absolute Code
LIST P=16C54
#INCLUDE "P16C5X.INC"
cblock H '020'
mulcnd ; 8 bit multiplicand
mulplr ; 8 bit multiplier
H_byte ; High byte of the 16 bit result
L_byte ; Low byte of the 16 bit result
count ; loop counter
endc
mpy clrf H_byte
clrf L_byte
movlw 8
movwf count
movf mulcnd,w
bcf STATUS,C ;Clear carry bit
Loop rrf mulplr,F
btfsc STATUS,C
addwf H_byte,F
rrf H_byte,F
rrf L_byte,F
decfsz count,F
goto loop
retlw 0
;*******************************************************
; Test Program
;*******************************************************
start clrw
option
main movf PORTB,w
movwf mulplr ; multiplier (in mulplr) = 05
movf PORTB,W
movwf mulcnd
call_m call mpy ; The result is in F12 & F13
; H_byte & L_byte
goto main
ORG 01FFh
goto start
END
Since an eight-by-eight bit multiply is a useful, generic routine, it would be handy to break this off into a separate object file that can be linked in when required. The above file can be broken into two files: a calling file representing an application and a generic routine that could be incorporated in a library.
Relocatable Code, Calling File
LIST P=16C54
#INCLUDE "P16C5x.INC"
EXTERN mulcnd, mulplr, H_byte, L_byte
EXTERN mpy
CODE
start clrw
option
main movf PORTB, W
movwf mulplr
movf PORTB, W
movwf mulcnd
call_m call mpy ; The result is in H_byte & L_byte
goto main
Reset CODE H'0lFF'
goto start
END
Relocatable Code, Library Routine
LIST P=16C54
#INCLUDE "P16C5x.INC"
UDATA
mulcnd RES l ; 8 bit multiplicand
mulplr RES 1 ; 8 bit multiplier
H_byte RES 1 ; High byte of the 16 bit result
L_byte RES 1 ; Low byte of the 16 bit result
count RES 1 ; loop counter
GLOBAL mulcnd, mulplr, H_byte, L_byte
CODE
mpy
GLOBAL mpy
clrf H_byte
clrf L_byte
movlw 8
movwf count
movf muland, W
bcf STATUS, C ; Clear carry bit
loop rrf mulplr, F
btfsc STATUS, C
addwf H_byte, F
rrf H_byte, F
rrf L_byte, F
decfsz count, F
goto loop
retlw 0
END
Microchip Technology Inc. Microchip's Web Site Voice: (480) 792-7200 Fax: (480) 899-9210 Microchip's E-mail Address |