Electronic – GOTO a specific address in HI-TECH C Compiler PIC18F

hi-tech-compilerpic

I'm trying to jump to a specific address in HI-TECH compiler and the address is given dynamically.

I tried using asm() function but it looks like the argument must be a constant char[]. Is there another solution to jump to an address using goto or another function like goto_address(address) in CCS Compiler?

I tried this:

long address = 0xA20;
char addr[15];
sprintf(addr, "goto %s", address)
asm(addr); // Here argument error.

Thanks.

Best Answer

This is the kind of thing most easily done in assembler.

If you need to do this regularly, you can create a assembler subroutine that follows the C subroutine linkage conventions. This would take a 24 bit argument that is the address to jump to. Remember to have the subroutine pop its own return address from the stack before doing the jump. The subroutine would write bits 23:16 of the address to PCLATU, bits 15:8 to PCLATH, and then bits 7:0 to PCL. That last instruction will actually do the jump, so everything else needs to be set up properly before the write to PCL.