Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Binary addition???

Status
Not open for further replies.

Ayne

New Member
Assembly, For PICs
Is there any software for gernating the code for addition???
Thanks.
 
Last edited:
u can create it..using XOR,OR and AND operations of the PIC..check any digital logic book..
 
You should really try to be a little more clear with your questions. What do you mean by "generating the code for addition"?

The PIC has addition instructions, like ADDLW and ADDWF. What more do you need?
 
You should really try to be a little more clear with your questions. What do you mean by "generating the code for addition"?

The PIC has addition instructions, like ADDLW and ADDWF. What more do you need?

I mean,
there is any program that produce assembly codes for addition and for subtrection.
For example we gave it two variable A, B.
A and B are both 16 bit wide. it adds two varibles and the result will store in third varible C.
C = A + B

in shot we can say that there is any program that produces the assembly codes for C = A + B

Infact it is always difficult to write program every time with different variable names. And with different data width.
Hope u can understand.
 
Ayne said:
I mean,
there is any program that produce assembly codes for addition and for subtrection.
For example we gave it two variable A, B.
A and B are both 16 bit wide. it adds two varibles and the result will store in third varible C.
C = A + B

in shot we can say that there is any program that produces the assembly codes for C = A + B

Infact it is always difficult to write program every time with different variable names. And with different data width.
Hope u can understand.

I posted 16 bit maths routines just a few days ago, have a look for that!.

To make it easy, I'll post it agin!.

Code:
;16 bit Maths subroutines
Sub16:
       		COMF   Acc2H, f       		; Take two's complement
		COMF   Acc2L, f       		; of Acc2. If zero, Acc2L
		INCF   Acc2L, f
		BTFSC  STATUS, Z   		; overflowed, so carry
		INCF   Acc2H, f       		; into Acc2H.
Add16:
       		BCF    Flags, OverFlow 		; clear overflow flag
		MOVF   Acc2L, w    		; Add the LSBs. If carry,
		ADDWF  Acc1L, f
		BTFSC  STATUS, C   		; increment MSB of sum.
		INCF   Acc1H, f
		MOVF   Acc2H, w   		; Add the MSBs
		ADDWF  Acc1H, f
		BTFSC  STATUS, C   		; check for sum overflow
		BSF    Flags, OverFlow 		; set overflow flag
		MOVF   Acc1L, w    		; return lobyte in W
		RETURN
Mult16:
		CLRF   Acc3H          		; Clear product to make
		CLRF   Acc3L         		; way for new calculation.
		MOVLW  d'16'        		; Number of bits to calc.
		MOVWF  tmp1
Multiply_loop
		BCF    STATUS, C
		RLF    Acc3L, f        		; Shift product left.
		RLF    Acc3H, f
		BCF    STATUS, C
		RLF    Acc1L, f        		; Shift multiplicand left.
		RLF    Acc1H, f
		BTFSS  STATUS, C    		; If carry, add multiplier
		GOTO   Multiply_skip
		MOVF   Acc2L, w      		; to the product. Else skip.
		ADDWF  Acc3L, f
		BTFSC  STATUS, C    		; 16-bit addition: prod+mulp
		INCF   Acc3H, f
		MOVF   Acc2H, w
		ADDWF  Acc3H, f
Multiply_skip
		DECFSZ tmp1
		GOTO   Multiply_loop
		MOVF   Acc3H, w
		MOVWF  Acc1H        		; return answer in Acc1
		MOVF   Acc3L, w
		MOVWF  Acc1L        		; with lobyte in W
		RETURN

DIV16:          MOVF    Acc2H, w                ; Check for division by 0.
		IORWF   Acc2L, w
		BTFSC   STATUS, Z
		RETLW   d'255'        		; Error code= 255: return.
		MOVLW   d'1'          		; Otherwise, initialize variables
		MOVWF   tmp1
		CLRF    tmp2         		; index for the division.
		CLRF    Acc3H
		CLRF    Acc3L
Divide_sh_loop  BTFSC   Acc2H, d'7'   		; Shift divisor left
		GOTO    Divide_d1
		BCF     STATUS, C     		; until msb is in
		RLF     Acc2L, f         	; Acc2H.7.
		RLF     Acc2H, f         	; tmp1 = no. of shifts+1.
		INCF    tmp1
		GOTO    Divide_sh_loop
Divide_d1       BCF     STATUS, C
		RLF     Acc3L, f          	; Shift quotient left.
		RLF     Acc3H, f
		MOVF    Acc2L,w       		; top = top - btm.
		SUBWF   Acc1L, f
		BTFSC   STATUS, C     		; If top - btm < 0 then
		GOTO    Divide_d2
		MOVLW   d'1'          		; top = top + btm
		SUBWF   Acc1H, f
		BTFSC   STATUS, C     		; The idea is to do the
		GOTO    Divide_d2
		INCF    Acc1H, f         	; the subtraction and comparison
		MOVF    Acc2L, w      		; (top > btm?) in one step.
		ADDWF   Acc1L, f
		goto    Divide_reentr 		; Then, if btm > top, undo <Microchip instruction>
Divide_d2       MOVF    Acc2H, w      		; the subtraction by adding
		SUBWF   Acc1H, f
		BTFSS   STATUS, C     		; top and btm back together
		goto    Divide_less   		; <Microchip instruction>
		BSF     Acc3L,  d'0'
Divide_reentr
		BCF     STATUS, C
		RRF     Acc2H, f
		RRF     Acc2L, f
		DECFSZ  tmp1
		GOTO    Divide_d1
		MOVF    Acc3H, w
		MOVWF   Acc1H         		; return answer in Acc1
		MOVF    Acc3L, w
		MOVWF   Acc1L        		; with lobyte in W
		RETURN
Divide_less     MOVF    Acc2L, w      		; btm > top, so
		ADDWF   Acc1L, f
		BTFSC   STATUS, C     		; undo the subtraction by
		INCF    Acc1H, f         	; adding them back together.
		MOVF    Acc2H, w
		ADDWF   Acc1H, f
		goto    Divide_reentr 		; <Microchip instruction>

SHIFTR:         MOVWF    Acc2L
_LoopR          BCF      STATUS, C
		RRF      Acc1H, F
		RRF      Acc1L, F
		DECFSZ   Acc2L
		GOTO     _LoopR
		MOVF     Acc1L, W
		RETURN

SHIFTL:         MOVWF    Acc2L
_LoopL          BCF      STATUS, C
		RLF      Acc1L, F
		RLF      Acc1H, F
		DECFSZ   Acc2L
		GOTO     _LoopL
		MOVF     Acc1L, W
		RETURN
 
Last edited:
What you're asking for is a program that converts a simple mathematical expression into assembly language using 8-bit math... This is pretty much what a higher-level language compiler would do for you (C, BASIC, etc)...
The math routines Nigel posted seem simple enough; if you think it's too much work to simply copy-paste those routines into your program and use them, then why are you even using assembly language?
 
evandude said:
What you're asking for is a program that converts a simple mathematical expression into assembly language using 8-bit math... This is pretty much what a higher-level language compiler would do for you (C, BASIC, etc)...
The math routines Nigel posted seem simple enough; if you think it's too much work to simply copy-paste those routines into your program and use them, then why are you even using assembly language?

Incidently, they are the maths routines from my BASIC compiler, and the A=B+C type of thing just transfers the variables to the accumulators, calls the correct routine, then transfers the result to the other variable. It's really how any hi-level language works, and it's easy to do in assembler.

This is that code in my compiler:

Code:
Device 16F628
Define PortB=%00000000
Dim A, C, B
B=2000
C=2500
A=B+C

And is the actual assembler code the compiler creates, notice the original BASIC lines are listed in the assembler as comments:

Code:
; WinPicProg BASIC Compiler Version1.91
;
; Device 16F628
    LIST P=16F628, W=2, X=ON, R=DEC
    #INCLUDE P16F628.INC
    __CONFIG    0x3D18
;Flags register bit definitions
LEADZ         EQU   0x00   ;set to show leading zeros
OverFlow      EQU   0x01   ;set for add/sub overflow
Lo            EQU   0x05   ;set for I2C normal addressing
Hi            EQU   0x06   ;set for I2C extended addressing
Err           EQU   0x07   ;set for I2C error
;Pre-defined variables
TEMP1         EQU   32
TEMP2         EQU   33
RESTORE       EQU   34
TEMP1_DU      EQU   35
TEMP2_DU      EQU   36
RESTORE_DU    EQU   37
W_DU          EQU   38
STATUS_DU     EQU   39
Bit_Cntr      EQU   40
Xmit_Byte     EQU   41
Rcv_Byte      EQU   42
LCD_DAT1      EQU   43
Delay_Count   EQU   44
_N            EQU   44
LCD_DAT2      EQU   45
LCD_DAT3      EQU   46
TEMP_LCD      EQU   47
TEMP_LCD2     EQU   48
DELAY1        EQU   49
state         EQU   50
pin           EQU   51
hiRnd         EQU   52
lowRnd        EQU   53
Acc1L         EQU   54
Acc1H         EQU   55
Acc2L         EQU   56
Acc2H         EQU   57
Acc3L         EQU   58
Acc3H         EQU   59
Flags         EQU   60
TenK          EQU   61
Thou          EQU   62
Hund          EQU   63
Tens          EQU   64
Ones          EQU   65
Adr_Lo        EQU   66
Adr_Hi        EQU   67
Chip_Adr      EQU   68
Data_Page     EQU   69
    ORG      0x0000
    GOTO     _START1
    ORG      0x0010
    #INCLUDE MATHS.INC
_START1
    CLRF     RESTORE
    CLRF     Flags
    BCF      Flags, LEADZ
    MOVLW    0x07
    MOVWF    CMCON ;turn comparators off (make it like a 16F84)
; Define PortB=%00000000
    BANKSEL  TRISB
    MOVLW    0
    MOVWF    TRISB
    BANKSEL  PORTB
; Dim A, C, B
VAR2    EQU      70;A
VAR4    EQU      72;C
VAR6    EQU      74;B
; B=2000
    MOVLW    7
    MOVWF    VAR6+1
    MOVLW    208
    MOVWF    VAR6
; C=2500
    MOVLW    9
    MOVWF    VAR4+1
    MOVLW    196
    MOVWF    VAR4
; A=B+C
    MOVF     VAR6,W
    MOVWF    Acc1L
    MOVF     VAR6+1,W
    MOVWF    Acc1H
    MOVF     VAR4,W
    MOVWF    Acc2L
    MOVF     VAR4+1,W
    MOVWF    Acc2H
    CALL     _Add16
    MOVF     Acc1L, W
    MOVWF    VAR2
    MOVF     Acc1H, W
    MOVWF    VAR2+1
    #INCLUDE HANDLERS.INC
    END
;
;Number of variables used:74 out of 127
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top