Maker Pro
Maker Pro

Help using multiplication with PIC microcontroller

Hi, I need some help with a task on my electronics course at college. The program underneath should multiply 3 and 2 to make 6 but instead I get 9. Could anyone help correct this please and then turn it into a linear program. Thank you!

cblock 0x20
first
second
endc

org 0
Start:
bsf STATUS,RP0
clrf TRISC
bcf STATUS,RP0
movlw 0x03
movwf first
movlw 0x02
movwf second

call addloop
movwf PORTC

addloop:
movwf first

again:
addwf first
decfsz second
goto again
return

end
 
cblock 0x20
first
second
endc

org 0
Start:
bsf STATUS,RP0
clrf TRISC
bcf STATUS,RP0
movlw 0x03
movwf first
movlw 0x02
movwf second

call addloop
movwf PORTC ;After 'addloop' function returns, you should end the program
hang_here goto hang_here


addloop:
;movwf first ; At this point WREG is already 0x02 and first=0x03, why do you want to put WREG in first again ?
movlw 0x00 ; Instead set WREG to zero

again:
addwf first ; This is ok
;decfsz second ; here you decrement 'second' and put the result in WREG ! that is a mistake
decfsz second, 1 ;by passing 1 for the second operand the result will be saved in 'second'
goto again
return ; OK now you have got the result and the function returns
 
Top