PIC Microcontoller Subtraction Math Method

32 bit unsigned substraction from Eddy Gora



;------------------------------------------------------------------------
; This is simple routine for full unsigned 32 bit substract
; a - b and result back in a ( initially a is lost )
; on exit success, CARRY = 0, on fail ( substraction negative ) CARRY = 1
; can be easly reduced / increased for 8 / 16 / 32 / 64 bits
; no optimisations ( tricks ) are made
; enjoy, eddy@hcv.ro from HCV Grup Ltd.,

      __Config   3FF1h

       include    <p16f877a.inc>



a1    equ       0x20
a2    equ       0x21
a3    equ       0x22
a4    equ       0x23

b1    equ       0x24
b2    equ       0x25
b3    equ       0x26
b4    equ       0x27

        org    0
  
START 
        clrf     a1  
        clrf     a2
        clrf     a3
        clrf     a4
        clrf     b1
        clrf     b2
        clrf     b3
        clrf     b4

        movlw   0x9c                 
        movwf   a1 
        movlw   0x00                 
        movwf   a2 
        movlw   0x00                  
        movwf   a3 
        movlw   0xE2
        movwf   a4 			; MSB

        movlw   0x9d
        movwf   b1 
        movlw   0xff
        movwf   b2 
        movlw   0x4f
        movwf   b3 
        movlw   0xc0
        movwf   b4 			; MSB

        nop
		call	SUBB_32
		nop


SELF
	goto	SELF

;------------------------------------------------------------------------
; This is simple routine for full unsigned 32 bit substract
; a - b and result back in a ( initially a is lost )
; on exit success, CARRY = 0, on fail ( substraction negative ) CARRY = 1
; can be easly reduced / increased for 8 / 16 / 32 / 64 bits
; no optimisations ( tricks ) are made
; enjoy, eddy@hcv.ro from HCV Grup Ltd.,

SUBB_32
	bcf		STATUS,C	; clear our routine return flag	

	movf    b1,W    	; substract first LSB bytes
    subwf   a1,F    	; a1 - b1 --&gt; a1
						; a > b --&gt; c=1
						; a = b --&gt; c=1
						; a < b --&gt; c=0 we have BORROW
	btfsc	STATUS,C	; have BORROW ?
	goto	NO_BORROW1	; 1 NO
						; 0 YES, so substract 1 from the next LSB
	movlw	.1
	subwf	a2,F
	btfsc	STATUS,C	; have also BORROW here ?
	goto	NO_BORROW1	; 1
						; 0 YES, so substract more, 1 from next
	movlw	.1
	subwf	a3,F
	btfsc	STATUS,C	; still BORROW ?
	goto	NO_BORROW1	; 1
						; 0 YES, substract 1 from the last
	movlw	.1
	subwf	a4,F		

	btfsc	STATUS,C	
	goto	NO_BORROW1
	bsf		STATUS,C	; if another BORROW, the substraction result is negative
						; so return quick with CARRY = 1
	return
NO_BORROW1

	movf    b2,w     	; the same manner ...	
    subwf   a2,f     	
	btfsc	STATUS,C	
	goto	NO_BORROW2	
	movlw	.1
	subwf	a3,F
	btfsc	STATUS,C	
	goto	NO_BORROW2	
	movlw	.1
	subwf	a4,F		
	btfsc	STATUS,C	
	goto	NO_BORROW2
	bsf		STATUS,C	
	return
NO_BORROW2
	movf    b3,w     	
    subwf   a3,f     	
	btfsc	STATUS,C	
	goto	NO_BORROW3	
	movlw	.1
	subwf	a4,F
	btfsc	STATUS,C
	goto	NO_BORROW3
	bsf		STATUS,C
	return
NO_BORROW3
	movf    b4,w    
    subwf   a4,f    
	btfsc	STATUS,C
	goto	NO_BORROW4
	bsf		STATUS,C
	return
NO_BORROW4
	bcf		STATUS,C
	return
;-----------------------------------------------------------------------	

	end

Questions:

Comments: