# Set function to specific address in PIC18F with the HI-TECH C compiler

I want to put a function in an specific address of memory like PIC C Compiler (#org). I'm using MPLAB X, HI-TECH compiler and PIC18F4550.

In PIC C compiler:

#org 0x1000, 0x2000
void MyFunction()
{
}

//In other part of code I'll use: asm("goto 0x1000");


How to do this in MPLAB X with the HI-TECH compiler?

• I don't understand why you'd need to. You can reference the symbol MyFunction from assembly (maybe as _MyFunction depending on compiler). – Majenko Oct 29 '14 at 14:29
• Also, you should never ever ever "goto" a function, or "goto" anywhere outside your current function. – Majenko Oct 29 '14 at 14:31

IIRC, you can set a function to an absolute address by using the "@" qualifier:

void MyFunction() @ 0x2A0
{
...
}


So the function MyFunction will be placed at address 0x2A0 in Program Memory.

• Thanks @m.Alin, I worked. I'd like to ask you how to use int address = 0x20; **asm("goto "___mktstr(addr));** but If I use asm("goto "___mktstr(0x20)); works. I want to use goto to go a specific address using addr variable, perhaps there is another option like goto_address(addr) in pic CCS compiler. Thank you. – 788498 Oct 30 '14 at 6:34
• The ___mktstr(x) macro stringifies its argument. ___mktstr(0x20) will return the string "0x20", but ___mktstr(addr) will return the string "addr". – m.Alin Oct 30 '14 at 7:22
• Thanks, but I still couldn't find a way to GO to a variable address. It looks like asm() argument must be char constant. Is there another solution to jump to an address using goto? – 788498 Oct 31 '14 at 4:56
• @jaspher I'm not experienced in assembly. You should ask a new question regarding that. – m.Alin Oct 31 '14 at 7:14

use the linker command file to set the address of .text portion of MyFunction

• This doesn't seem to answer the question. Please add further explanation. – Null Oct 30 '14 at 0:47