Strange jumps of PC

Status
Not open for further replies.

sandeepa

New Member
Code:
org	0x10fe
menu0_table
	addwf	PCL,1
	dt	"CHANGE SETTINGS",0

with W =1, before execution
Code:
W = 1
PCLATH = 0x10
PCL = 0x83(call menu0_table)

and after execution
Code:
W = 0x9e
PCALTH = 0x10
PCL =  0x63

Where does this value get into W ?And why does PCL jump to 0x63 ?
What am i missing ?

Thanks.
 
You don't mention which microcontroller you are using! But it looks like you are crossing an address boundry or jumping into lala land via your data string.
 
After the addwf instruction is executed the value that is placed into PCL is $fe + 1 (for addwf instruction) + 1 (W) = 0. The zero value gets combined with PCLATH and execution continues at 0x1000.

HTH

Mike.
 
Suppose i change the addwf to movwf PCL, what do you reckon will happen ?
Code:
        movfw  display_digit
	addlw	LOW menu0_table

	call	menu0_table
org	0x10fe
menu0_table
	movwf	PCL
	dt	"CHANGE SETTINGS",0

in this case, will it function correctly if display_digit = 1
and enter an endless loop if display_digit = 0 ?
 
Why do you want to put it at 0x10fe? If you put it at 0x10ff it will work correctly.

Alternatively, you can do 16 bit addition and have your tables anywhere.
Code:
		org	0x10f0;	can be any address
menu0_table
		movwf	Temp;			Temporarily store W
		movlw	high(message);		Setup PCLATH
		movwf	PCLATH
		movfw	Temp;			Retrieve W
		addlw	low(message);		calculate low address
		btfsc	STATUS,C;		crossed page boundary?
		incf	PCLATH,F;		yes, increment high byte
		movwf	PCL;			do the jump
message		dt	"CHANGE SETTINGS",0

HTH

Mike.
 
I didnt put this piece at 0x10fe, thats where it came in my program, and I had problems because of boundary cross.I had done the 16 bit PCLATH update, but used the addwf PCL,1 in the called table.So the problem.
Now I need some clarification regarding the movwf instruction.
Say in the code you have posted, if Temp = 0, then the program will enter an endless loop, right ?
Thanks.
 
sandeepa said:
Now I need some clarification regarding the movwf instruction.
Say in the code you have posted, if Temp = 0, then the program will enter an endless loop, right ?
Thanks.

No, when any instruction is executed PCL is automatically incremented and so calling the above code with W=0 (which gets stored in Temp) will result in the first character of the message ("C") being returned. Even calling the code with W=255 (-1) will result in a jump to address message+255.

HTH

Mike.
 
Code:
        org	0x107c
menu0_display
	movlw	HIGH menu0_table
	movwf	PCLATH			;high addr in PCLATH
	movlw	LOW	menu0_table
	addwf	display_digit,0	;check offset for table read
	btfsc	STATUS,C
	incf	PCLATH,1		;increment PCH if boundary crossed
;menu0_display
	call	menu0_table
	pagesel	$
	xorlw	0x00
	btfsc	STATUS,Z
	goto	menu_return		;return if 0 is returned from called table
	gcall	LCD_Char		;display char
	incf	display_digit,1	;increment offset
	goto	menu0_display

	org	0x10fc
menu_return
	goto	menu_return
	
	org	0x10fe
menu0_table
	movwf	PCL
	dt	"CHANGE SETTINGS",0

Thats the code I am using and
for display_digit = 0, the PC stays stuck at 0x10fe
Code:
Address      Symbol Name      Value   

             WREG                    0xFE
     000A    PCLATH                  0x10
     0002    PCL                     0xFE
     0065    display_digit           0x00

Thats the value in the relevant registers.
 
You can't do the call after you modify PCLATH. The call to menu0_table after PCLATH has been incremented will call 0x11fe. I'm not sure why it would fail for a zero value. If your still having trouble tomorrow, I'll try and figure it out. It's midnight here now, so time for bed.

BTW, if you are running this in the simulator, it seems to mess up with unexpected PCLATH values.

Mike.
 
You can't do the call after you modify PCLATH. The call to menu0_table after PCLATH has been incremented will call 0x11fe.
No, the CALL works fine because only bits <4:3> of PCLATH are used to calculate the address.
And as for the zero value with movwf PCL instruction, the PCL does get automatically incremented, however gets over-written with the value in W once the instruction is executed.This was the explaination given to me, when I asked similar question on Microchip forum.
This is the link to teh thread

http://forum.microchip.com/tm.aspx?m=194594

Thanks for all the help.
 
You are right, the call will work fine. However, the PCL value does not get overwritten after it is incremented. The error is as explained in the other thread, the label you use to calculate the address is on the line movwf PCL instead of the line starting DT. The code I posted above would work correctly because I added the label "message" to the DT line.

Anyway, glad you got it working.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…