Continue to Site

# 16 bit maths routines for F84

Status
Not open for further replies.

#### mypooka

##### Member level 1
16f84 maths

:lol:

maybe these are usefull for some newer people
i set these maths routines up for 16f84

16bit maths division, subtraction, multiplication!
hope these help someone!

list P = 16F84
include "p16F84.inc"
;
;
CBLOCK 0x0C
ACCaLO
ACCaHI

ACCbLO
ACCbHI

ACCcLO
ACCcHI

ACCdLO
ACCdHI
temp
sign
ENDC
;
;
;*******************************************************************
; Double Precision Division
;*******************************************************************
; Load constant values to ACCa & ACCb for testing
;-------------------
org 0h

movlw 0h
movwf ACCaHI ; the dividing number!
movlw d'10' ; after run! loads ACCa = FF= d15
movwf ACCaLO
;
movlw 015
movwf ACCbHI ; the number to be divided!
movlw 07C ; also the result of division
movwf ACCbLO ; load eg: 157C = d5500
;---------------
call D_divS ; remainder in ACCc
; remander =63
stop goto stop
;
;-------------
D_divS
call setup
clrf ACCcHI
clrf ACCcLO
dloop bcf STATUS,C
rlf ACCdLO, F
rlf ACCdHI, F
rlf ACCcLO, F
rlf ACCcHI, F
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, F
btfss STATUS,C
decf ACCcHI, F
movf ACCaHI,W
subwf ACCcHI, F
bsf STATUS,C ;shift a 1 into b (result)
nogo rlf ACCbLO, F
rlf ACCbHI, F
decfsz temp, F ;loop untill all bits checked
goto dloop
;
retlw 0
;*******************************************************************
;
setup movlw 0x10 ; for 16 shifts
movwf temp
movf ACCbHI,W ;move ACCb to ACCd
movwf ACCdHI
movf ACCbLO,W
movwf ACCdLO
clrf ACCbHI
clrf ACCbLO
retlw 0

END
;****************************************************************

list P = 16F84
include "p16F84.inc"

CBLOCK 0x0C

temp
ACCaLO ; was called product!
ACCaHI

ACCbLO
ACCbHI ; first x number

ACCcLO ; second number!
ACCcHI

ENDC

org 0h
;*************************************************
; multiply sub rutine!
;*************************************************
goto \$ +4
clrf ACCaLO
clrf ACCaHI
nop
nop

clrf ACCaLO
clrf ACCaHI

movlw 0x0A
movwf ACCbLO ; lower 8bits of first number
movlw 0x00
movwf ACCbHI ; upper 8bits of first number

movlw 0x6F
movwf ACCcLO ; lower 8bits of second number
movlw 0x19
movwf ACCcHI ; upper 8bits of second number

movlw .16 ; Operating on 16 Bits
movwf temp

Loop ; Loop Here for Each Bit

rrf ACCbHI, f ; Shift the Multiplier down
rrf ACCbLO, f ; by one

btfss STATUS, C ; If the bit is set, add
goto Skip ; the Multiplicand to the
; "Product"
movf ACCcHI, w
movf ACCcLO, w
btfsc STATUS, C
incf ACCaHI, f

Skip ; Shift up Multiplicand and
bcf STATUS, C ; Loop Around
rlf ACCcLO, f
rlf ACCcHI, f

decfsz temp,F
goto Loop
;
; the result is in ACCaHI,ACCaLO

stop goto stop

end

list P = 16F84
include "p16F84.inc"

CBLOCK 0x0C
temp
ACCaLO ;
ACCaHI

ACCbLO
ACCbHI ; first number

ACCcLO ; second number!
ACCcHI

ACCdLO
ACCdHI

ENDC

;**************************************************
; subtract sub rutine!
;**************************************************
org 0h
;--------------------------
; test code for subtraction
;--------------------------

;--------------------

movlw 0x8A
movwf ACCaHI ; example numbers
movlw 0x06
movwf ACCaLO

movlw 0x8A
movwf ACCbHI
movlw 0x0A
movwf ACCbLO
;-------------------

;----------
movf ACCaHI,W ; Subtract the High Byte First
subwf ACCbHI,W ; b-a=c
movwf ACCcHI ; store the HI results
;----------

movf ACCaLO,W ; Subtract the Low Byte Next
subwf ACCbLO,W ; store the result in ACCbLO
movwf ACCcLO ; store the LO results

btfss STATUS, C ; Don't Dec high byte if carry Set
decf ACCcHI, F

iorwf ACCcHI, W ; Check for Equal to Zero
btfsc STATUS, Z ; If Not Zero, Jump Over
btfsc ACCcHI, 7h ; If Number is Negative, execute
goto EqualLess ; if A>B go here! Else, Jump Over

great goto great ; if A<B go here!
;
; results are in ACCcHI and ACCcLO

EqualLess

stop goto stop ;

end

;
;

Status
Not open for further replies.