[PIC] multiplication of numbers in pic

Status
Not open for further replies.

mvrthunder

Newbie level 5
Joined
Aug 5, 2013
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
48
i want to multiply two 4 digit numbers in pic16f877a in assembly language
 

hello,

What represents these 4 digits number
Hexadecimal value 0x1234 0000 to FFFF
decimal value 1234 BCD (0000 to 9999)

or you want to mutiply 2 int (16bits) values
int signed or not ?
 

hello,

What represents these 4 digits number
Hexadecimal value 0x1234 0000 to FFFF
decimal value 1234 BCD (0000 to 9999)

or you want to mutiply 2 int (16bits) values
int signed or not ?

it is 4 digit hexa decimal numbers sir
i know how to do it with upto two x two but not 4x4
can u suggest me a idea

- - - Updated - - -

0xFFFF = 65535 = 16 bits wide.

let us assume the given numbers are 2032 x 1523
 

hello,

there are a lot of example on the web for mutiply 16x16
be carrefull, some of them have buggs !!
like this one .. http://www.piclist.com/techref/microchip/math/mul/16x16rp.htm
but this one
;http://www.piclist.com/techref/microchip/math/mul/16x16umalin.htm
is OK, tested successfully with MPLAB debug. 16F84

Code:
; 14-10-2013
; testé OK  sur un 16F84 en mode debug 
;*******************************************************************
;http://www.piclist.com/techref/microchip/math/mul/16x16umalin.htm
;malin@onspec.co.uk
;unsigned multiply of a2:a1 with b2:b1 leaving result in res4:res3:res2:res1
;    These 8 variables need to be defined
;
;    Program length    32 line
;    time 129 to 228 cycles
;    This program looks at the lsb of a1 to decide whether to add b1 to res2

        LIST      p=16F84a            ; Définition de processeur
    #include <P16F84A.inc>        ; Définitions des constantes

    __CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC

; '__CONFIG' précise les paramètres encodés dans le processeur au moment de
; la programmation du processeur. Les définitions sont dans le fichier include.
; Voici les valeurs et leurs définitions :
;    _CP_ON        Code protection ON : impossible de relire
;    _CP_OFF        Code protection OFF
;    _PWRTE_ON    Timer reset sur power on en service
;    _PWRTE_OFF    Timer reset hors-service
;    _WDT_ON        Watch-dog en service
;    _WDT_OFF    Watch-dog hors service
;    _LP_OSC        Oscillateur quartz basse vitesse
;    _XT_OSC        Oscillateur quartz moyenne vitesse
;    _HS_OSC        Oscillateur quartz grande vitesse
;    _RC_OSC        Oscillateur à réseau RC



;
    CBLOCK 0x0020                ; debut de la zone variables
a2  ; Low
a1  ; Hih
b2
b1

res4
res3
res2
res1    

w_temp           
status_temp
dummy 
   ENDC
LastBank0 EQU dummy
    IF ( LastBank0 >= 0x70 )
        ERROR   "Attention debordement zone RAM bank0"
    ENDIF

            org     0x0000
            goto Start
            
;*************************************************  
; Le gestionnaire d'interruption commence ici    *
;*************************************************

    org 4                   ;Le gestionnaire d'interruption            
; insert here
    retfie                  ;Fin de la routine d'interuption          
;*******************************************************

Multiply16x16
    clrf    res4
    clrf    res3
    clrf    res2
    movlw    0x80
    movwf    res1        

nextbit
    rrf        a2,f
    rrf        a1,f

    btfss    STATUS, C
    goto    nobit_l
    movf    b1,w
    addwf    res2,f

    movf    b2, w
    btfsc    STATUS,C
    incfsz    b2, w    
    addwf    res3, f    
    btfsc    STATUS,C
    incf    res4, f
    bcf        STATUS,C
    
nobit_l    
    btfss    a1, 7
    goto    nobit_h
    movf    b1,w
    addwf    res3,f
    movf    b2, w
    btfsc    STATUS,C
    incfsz    b2, w
    addwf    res4, f    

nobit_h
    rrf        res4,f
    rrf        res3,f
    rrf        res2,f
    rrf        res1,f

    btfss   STATUS,C
    goto    nextbit
    retlw 0
    
    
    
Start
; init 2 values to multiply
    nop
        clrf  a1
        clrf  b1
        clrf  a2
        clrf  b2
        clrf  res1
        clrf  res2
        clrf  res3
        clrf  res4   
                            
                  
loadvalue
        movlw   7
        movwf   a2  
        movlw   0xF0            ; load= 2032
        movwf   a1
        nop
        movlw   5
        movwf   b2
        movlw   0xF3          ; load 1523
        movwf   b1
        nop
        call Multiply16x16
        nop
        nop
boucle        
        goto boucle
        
        end
        
   ;     2032*1523=3094736
   ;     result :
   ;     res4 =  3094736


Be carrefull, result is (must be) in a 32 bits value (Res4)
Take care also if you use signed or not signed int16 !
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…