user59113: I am trying to implement a scheduler for Arduino Mega 2560. I think I lack understanding of how to set the PC register to another instruction.
Here is my simple approach so far:
void dummy(){
    digitalWrite(LED_BUILTIN, HIGH);
 }

in a naked ISR method:
SP = (uint16_t)(&dummy)
__asm volatile(
"reti     \n\t"
)

However it is not working. How should the PC register be set to a certain function or to where it was in a previous process for a scheduler approach?

user7508: This:
SP = (uint16_t)(&dummy)

is changing the stack pointer. Not what you want. You want to instead
push the destination address to the stack. However, rather than
simulating a function return, it should be simpler to perform an
indirect jump:
asm volatile("ijmp" :: "z"(dummy));

Don't forget that your naked ISR has to save all the call-used
registers, as well as SREG, r0 and r1, in its custom prologue. And
restore them in its epilogue. Check the AVR calling
conventions.

59113: <p>I am trying to implement a scheduler for Arduino Mega 2560. I think I lack understanding of how to set the PC register to another instruction.
Here is my simple approach so far:</p>

<pre><code>void dummy(){
    digitalWrite(LED_BUILTIN, HIGH);
 }
</code></pre>

<p>in a naked ISR method:</p>

<pre><code>SP = (uint16_t)(&amp;dummy)
__asm volatile(
"reti     \n\t"
)
</code></pre>

<p>However it is not working. How should the PC register be set to a certain function or to where it was in a previous process for a scheduler approach?</p>
 7508: <p>This:</p>
<pre class="lang-c prettyprint-override"><code>SP = (uint16_t)(&amp;dummy)
</code></pre>
<p>is changing the stack pointer. Not what you want. You want to instead
<code>push</code> the destination address to the stack. However, rather than
simulating a function return, it should be simpler to perform an
<a href="https://www.microchip.com/webdoc/avrassembler/avrassembler.wb_IJMP.html" rel="nofollow noreferrer">indirect jump</a>:</p>
<pre class="lang-c prettyprint-override"><code>asm volatile(&quot;ijmp&quot; :: &quot;z&quot;(dummy));
</code></pre>
<p>Don't forget that your naked ISR has to save <em>all</em> the call-used
registers, as well as <code>SREG</code>, <code>r0</code> and <code>r1</code>, in its custom prologue. And
restore them in its epilogue. Check the <a href="https://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_reg_usage" rel="nofollow noreferrer">AVR calling
conventions</a>.</p>
