Connect with us

Help using multiplication with PIC microcontroller

Discussion in 'Electronics Homework Help' started by REDHOTHOBO, Jun 24, 2014.

Scroll to continue with content
  1. REDHOTHOBO

    REDHOTHOBO

    1
    0
    Jun 24, 2014
    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
     
  2. Ehsan

    Ehsan

    100
    1
    Jun 12, 2014
    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
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-