PIC assembly coding problem

Discussion in 'Programmer's Corner' started by Dritech, Feb 16, 2012.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi all,

    I am doing a program where a value is moved to REG1 (in the example below the value is 1 decimal), and the program will go to different call routines according to the value moved in REG1.

    Code ( (Unknown Language)):
    1.  
    2. CLRF REG1
    3.  
    4. MOVLW .1                                  
    5. MOVWF REG1
    6.  
    7. MOVLW REG1
    8. SUBLW .3
    9. BTFSC STATUS,Z
    10. GOTO CALL_1
    11.  
    12. MOVLW REG1
    13. SUBLW .2
    14. BTFSC STATUS,Z
    15. GOTO CALL_2
    16.  
    17. MOVLW REG1
    18. SUBLW .1
    19. BTFSC STATUS,Z
    20. GOTO CALL_3
    21.  
    In the above example, the program is suppose to go to the CALL_3 routine, but for some reason its not working.

    Does anyone know what is the problem please?

    Thanks in advance.
     
  2. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    It's not a trivial thing like if the value is 1 it should go to Call1 and if it's 3 it should go to Call3, whereas what you have is 1 goes to Call3 and 3 goes to Call1, is it?
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Use MOVF REG1, W instead of MOVLW REG1 and it should work.
     
  4. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    What if you just test bits 1 & 0 in Reg1?

    If Bit1 is set, goto Test_23
    If Bit0 is set, goto CallA
    goto Ooops ; The value is zero

    Test_23:
    If Bit0 is set, goto CallC
    goto CallB


    ... replacing CallA..C with whatever you want.

    --Rich
     
    Dritech likes this.
  5. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi,

    Thanks for your replies. Its now working :)
     
  6. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,678
    2,737
    And here is an equivalent method using computed gotos (assuming PIC16 series -- PIC18 is a little different):

    Code ( (Unknown Language)):
    1.  
    2.     movlw   .1
    3.     movwf   reg1
    4.  
    5.     movlw   high jtab
    6.     movwf   pclath
    7.  
    8.     decf    reg1,w
    9.     addwf   pcl,f
    10.  
    11. jtab    goto    call_3  ;reg1 = 1
    12.     goto    call_2  ;reg1 = 2
    13.     goto    call_1  ;reg1 = 3
    14.  
     
    Dritech likes this.
  7. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Or you could do it this way (assuming REG1's value can change):

    Code ( (Unknown Language)):
    1.  
    2.     DECFSZ REG1, F
    3.     GOTO S1                 ; REG1 was 1, 1 decrement made it 0
    4.     GOTO CALL_3
    5. S1: DECFSZ REG1, F
    6.     GOTO CALL_1             ; REG1 was 0 or >2, 2 decrements didn't make it 0
    7.     GOTO CALL_2             ; REG1 was 2, 2 decrements made it 0
    8.  
     
    Dritech likes this.
Loading...