PIC assembly coding problem

Thread Starter

Dritech

Joined Sep 21, 2011
901
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.

Rich (BB code):
CLRF REG1

MOVLW .1                                   
MOVWF REG1

MOVLW REG1
SUBLW .3
BTFSC STATUS,Z
GOTO CALL_1

MOVLW REG1
SUBLW .2
BTFSC STATUS,Z
GOTO CALL_2

MOVLW REG1
SUBLW .1
BTFSC STATUS,Z
GOTO CALL_3
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.
 

John P

Joined Oct 14, 2008
2,026
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?
 

RiJoRI

Joined Aug 15, 2007
536
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
 

joeyd999

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

Rich (BB code):
	movlw	.1
	movwf	reg1

	movlw	high jtab
	movwf	pclath

	decf	reg1,w
	addwf	pcl,f

jtab	goto	call_3	;reg1 = 1
	goto	call_2	;reg1 = 2
	goto	call_1	;reg1 = 3
 

John P

Joined Oct 14, 2008
2,026
Or you could do it this way (assuming REG1's value can change):

Rich (BB code):
    DECFSZ REG1, F
    GOTO S1                 ; REG1 was 1, 1 decrement made it 0
    GOTO CALL_3
S1: DECFSZ REG1, F
    GOTO CALL_1             ; REG1 was 0 or >2, 2 decrements didn't make it 0
    GOTO CALL_2             ; REG1 was 2, 2 decrements made it 0
 
Top