Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

subtract in assembly code in pic16f628a

Status
Not open for further replies.

ceibawx

Junior Member level 2
Joined
Oct 13, 2008
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
chongqing,China
Activity points
1,606
assembly subtract

subtract in assembly code in pic16f628a

I need subtract for three times in assembly code in pic16f628a. I wrote a stupid code, it is too long. and I am wondering whether I can use a loop to do it.

1) the data is saved in 0x40~0x51.The data is from 8channels, every channel is 12bits. so two channels occupy three bytes in memory. for example channel 1 and channel 2 occupy 0x40,0x41,0x42.
2) channel 2 - channel 8;
channel 4 - channel 6;
channel 3 - channel 7;
I choose this group, because I think channel2 has the same structure to channel8. it is easier to subtract than channel2-channel3.
3) before subtract, ANDLW is used to get the four bits from a byte register.
4) first subtract the lower bits, then subtract the higher bits according judging the value of STATUS, C of lower bits.
5) lower bits are stored firstly in memory register, then high bits are stored in latter memory place. for example, one subtract result, higher bits is stored in 0x60, while lower bits are stored in 0x61.
so all subtraction result are stored in 0x60 0x61 0x62 0x63 0x64 0x65.


Anybody can help me?


Code:
Code:
     list       P=16f628a
     #include   <p16f628a.inc>


ByteCounter   EQU    0x39
     
 goto   Main
 
Main:
    BCF    STATUS,RP0    ;bank 0
    BCF    STATUS,RP1
    MOVLW  0x07
    MOVWF  CMCON

    BSF    STATUS,RP0   ;bank 1
    BCF    TRISB,2   ;output
;uart setting
    MOVLW  0x15
    MOVWF  SPBRG       

    BCF    TXSTA,TX9
    BSF    TXSTA,TXEN
    BCF    TXSTA,SYNC
    BSF    TXSTA,BRGH
   
    BCF    STATUS,RP0    ; bank 0
    BSF    RCSTA,SPEN
    BCF    RCSTA,RX9
    BSF    RCSTA,CREN
         

Save:
;f>w   
    MOVLW  0x31
    MOVWF  0x40
    MOVLW  0x32
    MOVWF  0x41  ;0x50  for diff
    MOVLW  0x33
    MOVWF  0x42

    MOVLW  0x34
    MOVWF  0x43  ;0x51  for diff
    MOVLW  0x48
    MOVWF  0x44
    MOVLW  0x47
    MOVWF  0x45  ;0x52  for diff

    MOVLW  0x46
    MOVWF  0x46
    MOVLW  0x45
    MOVWF  0x47  ;0x53  for diff 
    MOVLW  0x44
    MOVWF  0x48

    MOVLW  0x43
    MOVWF  0x49
    MOVLW  0x42
    MOVWF  0x50
    MOVLW  0x41
    MOVWF  0x51



;*******Subtraction*************
BCF      STATUS,RP0  ;bank0   
;**********the first substraction*************
Ch2_8:
;****get the 5-8bits of  0x41 0x50******
      MOVLW    0x0F
      ANDWF    0x41,0
      MOVWF    0x6A  ;  0x41' address
      MOVLW    0x0F
      ANDWF    0x50,0
      MOVWF    0x6B;    0x50' address
;*******(0x42)-(0x51) save to 0x61******
BCF   STATUS,C
MOVF  0x51,0
SUBWF 0x42,0
MOVWF 0x61;
;*******substract according to C value*****
BTFSS STATUS,C
GOTO  Ch28_value0;C=0
BCF   STATUS,C;C=1
;(0x6A)-(0x6B)  to  0x60
MOVF  0x6B,0
SUBWF 0x6A,0
MOVWF 0x60;
goto  Ch4_6

Ch28_value0:    ;C=0,(0x6A)-1-(0x6B)  to  0x60
 BCF   STATUS,C
 MOVF  0x6B,0
 SUBWF 0x6A,0
 MOVWF 0x60;
;-1       check C???
 MOVLW 0x01;
 SUBWF 0x60,1
 goto  Ch4_6
;*********the second*****************************
Ch4_6:
      MOVLW    0x0F
      ANDWF    0x44,0
      MOVWF    0x6C  ;  0x44' address
      MOVLW    0x0F
      ANDWF    0x47,0
      MOVWF    0x6D;    0x47' address
        ;(0x45)-(0x48)  to 0x63
BCF   STATUS,C
MOVF  0x48,0
SUBWF 0x45,0
MOVWF 0x63;

BTFSS STATUS,C
GOTO  Ch46_value0;C=0
BCF   STATUS,C;C=1
;(0x6C)-(0x6D)  to  0x62
MOVF  0x6D,0
SUBWF 0x6C,0
MOVWF 0x62;
goto  Ch3_7

Ch46_value0:    ;C=0,(0x6C)-1-(0x6D)  to  0x63
 BCF   STATUS,C
 MOVF  0x6D,0
 SUBWF 0x6C,0
 MOVWF 0x62;
;-1       check C???  C ??,??????
 MOVLW 0x01;
 SUBWF 0x62,1
 goto  Ch3_7
;*************the third*******************************
Ch3_7:
       movlw   0xF0;
       andwf   0x44,0
       movwf   0x6E;  44'' in 6E
       movlw   0xF0;
       andwf   0x50,0
       movwf   0x6F;   50'' IN 6F
;*****substract************
BCF   STATUS,C  ;(6E)-(6F)  to  (65)
MOVF  0x6F,0
SUBWF 0x6E,0
MOVWF 0x65;

btfss STATUS,C
GOTO  Ch37_value0;  C=0
;C=1   (43)-(49)  TO  (64)
bcf   STATUS,C
movf  0x49,0
subwf 0x43,0
movwf 0x64;
goto  Stop


Ch37_value0:  ;C=0  (43)-1-(49)  TO  (64)
bcf   STATUS,C
movf  0x49,0
subwf 0x43,0
movwf 0x64;
;-1
movlw 0x01;
subwf 0x64,1


;60 61 62 63 64 65
GOTO   Stop
Stop:
    GOTO   Stop

END
 

asm subtract

u can use this code to subtract two 16 bit numbers

;************************************************************************
; Subtraction : ACCb(16 bits) - ACCa(16 bits) -> ACCb(16 bits) *
; (a) Load the 1st operand in location ACCaLO & ACCaHI ( 16 bits ) *
; (b) Load the 2nd operand in location ACCbLO & ACCbHI ( 16 bits ) *
; (c) CALL Sub_16bit *
; (d) The result is in location ACCbLO & ACCbHI ( 16 bits ) *
;************************************************************************
Sub_16bit
call Neg_16bit
call Add_16bit
return

;************************************************************************
; Addition : ACCb(16 bits) + ACCa(16 bits) -> ACCb(16 bits) *
; (a) Load the 1st operand in location ACCaLO & ACCaHI ( 16 bits ) *
; (b) Load the 2nd operand in location ACCbLO & ACCbHI ( 16 bits ) *
; (c) CALL Add_16bit *
; (d) The result is in location ACCbLO & ACCbHI ( 16 bits ) *
;************************************************************************
Add_16bit
movf ACCaLO,W
addwf ACCbLO, F ; add lsb
btfsc STATUS,C ; add in carry
incf ACCbHI, F
movf ACCaHI,W
addwf ACCbHI, F ; add msb
return
;************************************************************************
; 2's Compliment: negate ACCa ( -ACCa -> ACCa ) *
; (a) Load the operand in location ACCaLO & ACCaHI ( 16 bits ) *
; (b) CALL Neg_16bit *
; (c) The result is in location ACCaLO & ACCaHI ( 16 bits ) *
;************************************************************************
Neg_16bit
comf ACCaLO, F ;
incf ACCaLO, F
btfsc STATUS,Z
decf ACCaHI, F
comf ACCaHI, F
return
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top