《电子技术应用》
您所在的位置:首页 > 嵌入式技术 > 解决方案 > PIC乘法程序

PIC乘法程序

2009-01-16
关键词: 单片机 PIC

PIC乘法程序一

;*******************************************************************
;                   8x8 Software Multiplier
;               ( Code Efficient : Looped Code )
;*******************************************************************
;
;   The 16 bit result is stored in 2 bytes
;
; Before calling the subroutine " mpy ", the multiplier should
; be loaded in location " mulplr ", and the multiplicand in
; " mulcnd " . The 16 bit result is stored in locations
; H_byte & L_byte.
;
;       Performance :
;                       Program Memory  :  15 locations
;                       # of cycles     :  71
;                       Scratch RAM     :   0 locations
;
;  This routine is optimized for code efficiency ( looped code )
;  For time efficiency code refer to "mult8x8F.asm" ( straight line code )
;*******************************************************************
;
mulcnd  equ     09      ; 8 bit multiplicand
mulplr  equ     10      ; 8 bit multiplier
H_byte  equ     12      ; High byte of the 16 bit result
L_byte  equ     13      ; Low byte of the 16 bit result
count   equ     14      ; loop counter
;
;
 include         "picreg.h"
;
; *****************************         Begin Multiplier Routine
mpy_S   clrf    H_byte
 clrf    L_byte
 movlw   8
 movwf   count
 movf    mulcnd,w
 bcf     STATUS,CARRY    ; Clear the carry bit in the status Reg.
loop    rrf     mulplr
 btfsc   STATUS,CARRY
 addwf   H_byte,Same
 rrf     H_byte,Same
 rrf     L_byte,Same
 decfsz  count
 goto    loop
;
 retlw   0
;
;*************************************************************
;               Test Program
;**************************************************************
main    movlw   0FF
 movwf   mulplr          ; multiplier (in mulplr) = 0FF
 movlw   0FF             ; multiplicand(W Reg )   = 0FF
 movwf   mulcnd
;
 call    mpy_S           ; The result 0FF*0FF = FE01 is in locations
;                               ; H_byte & L_byte
;
self    goto    self
;
 org     01FF
 goto    main
;
 END

PIC乘法程序二:

;*******************************************************************
;                   8x8 Software Multiplier
;               ( Fast Version : Straight Line Code )
;*******************************************************************
;
;   The 16 bit result is stored in 2 bytes
;
; Before calling the subroutine " mpy ", the multiplier should
; be loaded in location " mulplr ", and the multiplicand in
; " mulcnd " . The 16 bit result is stored in locations
; H_byte & L_byte.
;
;       Performance :
;                       Program Memory  :  35 locations
;                       # of cycles     :  37
;                       Scratch RAM     :   0 locations
;
;  This routine is optimized for speed efficiency ( straight line code )
;  For code efficiency, refer to "mult8x8S.asm" ( looped code )
;*******************************************************************
;
mulcnd  equ     09      ; 8 bit multiplicand
mulplr  equ     10      ; 8 bit multiplier
H_byte  equ     12      ; High byte of the 16 bit result
L_byte  equ     13      ; Low byte of the 16 bit result
;
;
 include         "picreg.h"
;
;****   Define a macro for adding & right shifting  **
;
mult    MACRO   bit             ; Begin macro
 btfsc   mulplr,bit
 addwf   H_byte,Same
 rrf     H_byte,Same
 rrf     L_byte,Same
 ENDM                    ; End of macro
;
; *****************************         Begin Multiplier Routine
mpy_F   clrf    H_byte
 clrf    L_byte
 movf    mulcnd,w        ; move the multiplicand to W reg.
 bcf     STATUS,CARRY    ; Clear the carry bit in the status Reg.
 mult    0
 mult    1
 mult    2
 mult    3
 mult    4
 mult    5
 mult    6
 mult    7
;
 retlw   0
;
;********************************************************************
;               Test Program
;*********************************************************************
main    movlw   0FF
 movwf   mulplr          ; multiplier (in mulplr)     = 0FF
 movlw   0FF
 movwf   mulcnd          ; multiplicand(in mulcnd )   = 0FF
;
 call    mpy_F           ; The result 0FF*0FF = FE01 is in locations
;                               ; H_byte & L_byte
;
self    goto    self
;
 org     01FF
 goto    main
;
 END

本站内容除特别声明的原创文章之外,转载内容只为传递更多信息,并不代表本网站赞同其观点。转载的所有的文章、图片、音/视频文件等资料的版权归版权所有权人所有。本站采用的非本站原创文章及图片等内容无法一一联系确认版权者。如涉及作品内容、版权和其它问题,请及时通过电子邮件或电话通知我们,以便迅速采取适当措施,避免给双方造成不必要的经济损失。联系电话:010-82306118;邮箱:aet@chinaaet.com。