• 回复
  • 收藏
  • 点赞
  • 分享
  • 发新帖

PIC单片机的除法运算

以下是16位除16位的程序.大家来谈谈它是怎么样实现的.
org     0000h
goto main
;*******************************************************************;
;   Division : ACCb(16 bits) / ACCa(16 bits) -> ACCb(16 bits) with
;                                               Remainder in ACCc (16 bits)
;      (a) Load the Denominator in location ACCaHI & ACCaLO ( 16 bits )
;      (b) Load the Numerator in location ACCbHI & ACCbLO ( 16 bits )
;      (c) CALL D_div
;      (d) The 16 bit result is in location ACCbHI & ACCbLO
;      (e) The 16 bit Remainder is in locations ACCcHI & ACCcLO
;
;       NOTE :
;               The performance specs are for Unsigned arithmetic ;
;*******************************************************************
D_divF
setup
movlw   D'16'             ; for 16 shifts
movwf   temp
movf    ACCbHI,w          ; ACCb = ACCd
movwf   ACCdHI
movf    ACCbLO,w
movwf   ACCdLO
clrf    ACCbHI
clrf    ACCbLO

clrf    ACCcHI
clrf    ACCcLO
divv bcf     STATUS,C
rlf     ACCdLO
rlf     ACCdHI
rlf     ACCcLO
rlf     ACCcHI
movf    ACCaHI,w
subwf   ACCcHI,w          ;check if a>c
btfss   STATUS,Z
goto    NOCHK
movf    ACCaLO,w
subwf   ACCcLO,w        ;if msb equal then check lsb
NOCHK
btfss   STATUS,C ;carry set if c>a
goto    NOGO
movf    ACCaLO,w        ;c-a into c
subwf   ACCcLO
btfss   STATUS,C
decf    ACCcHI
movf    ACCaHI,w
subwf   ACCcHI
bsf     STATUS,C ;shift a 1 into b (result)
NOGO    
rlf     ACCbLO
rlf     ACCbHI

decfsz temp,1
goto divv
return
;
;*******************************************************************
;   测试程序(注意用法,ACCb/ACCa,结果放在ACCb里,余数放在ACCc里)
;*******************************************************************
;    Load constant values to ACCa & ACCb for testing
;
main    
movlw   04
movwf   ACCaHI
movlw   0e2             ; loads ACCa = 01FF
movwf   ACCaLO
;
movlw   0f4
movwf   ACCbHI
movlw   024             ; loads ACCb = 7FFF
movwf   ACCbLO

call    D_divF          ; remainder in ACCc. Here ACCb =0040 & ACCc=003F
;
self goto    self        ;如不是测试程序,本句无用
END
;****************************************************************
全部回复(0)
正序查看
倒序查看
现在还没有回复呢,说说你的想法