Connect with us

What is a good way to compare register values in a PIC assembly

Discussion in 'Microcontrollers, Programming and IoT' started by Dwnld_pwm, Apr 10, 2019.

  1. Dwnld_pwm

    Dwnld_pwm

    2
    0
    Apr 9, 2019
    Hello everyone.

    I'm using a PIC16F887 with the PICkit2.
    There doesn't seem to be a compare instruction so I'm wondering how one would go about comparing the values of two registers.

    For example let's say Register REG1 has a value b'00100010' and REG2 has b'000000000' but the value of REG2 increases by 1 every loop iteration. I want the loop to stop when REG2 = REG1.
    Is there a way of comparing them?

    It's easy in C, but how do I implement it in assembly?
    I'll leave a mixture of Assembly and C for reference.

    Cheers,

    MOVLW b'00100010'
    MOVWF REG1

    CLRF REG2

    Loop:
    INCF REG2
    ;(if REG2 == REG1){
    goto stop
    }
    else{
    goto Loop
     
  2. Dwnld_pwm

    Dwnld_pwm

    2
    0
    Apr 9, 2019
    Think I've just figured it out, here's the solution for whoever stumbles upon this in the future


    MOVLW b'00100010'
    MOVWF REG1

    CLRF REG2

    Loop:
    INCF REG2, F
    MOVFW REG2
    SUBWF REG1, W
    BTFSS STATUS, Z ;If REG1 = REG2 then REG1 - REG2 = 0 setting Z = 1
    goto Loop
    goto Stop
     
  3. Minder

    Minder

    2,774
    575
    Apr 24, 2015
    Also the logic instructions can be used in different ways to compare. notably the AND, XOR AND IOR.
    M.
     
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

-