# non-ASM statement in naked function is not supported (port.c module for FreeRTOS)

**URL:** <https://forums.freertos.org/t/non-asm-statement-in-naked-function-is-not-supported-port-c-module-for-freertos/13964>\
**Category:** Microchip\
**Created:** [November 28, 2021, 1:33am UTC](https://forums.freertos.org/t/non-asm-statement-in-naked-function-is-not-supported-port-c-module-for-freertos/13964 "2021-11-28T01:33:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmulchin](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@jmulchin](https://forums.freertos.org/u/jmulchin)\
**Post date:** [November 28, 2021, 1:33am UTC](https://forums.freertos.org/t/non-asm-statement-in-naked-function-is-not-supported-port-c-module-for-freertos/13964/1 "2021-11-28T01:33:04Z")

</div>

I have a port.c file for FreeRTOS that throws an error message under AS7 indicating that a naked function can not support any statement types except ASM statements when a “naked” attribute is declared. Now lets make things a little more confusing … The same two functions will compile just fine under AS7 on a ATmega128A project I have, but not under a ATXmega128A project. Same AS7 program, same exact functions, but different processors. I don’t think it’s processor related myself. The code is below:

```auto
/*
 * Manual context switch. The first thing we do is save the registers so we
 * can use a naked attribute.
 */
void vPortYield( void ) __attribute__ ( ( naked ) );
void vPortYield( void )
{
	portSAVE_CONTEXT();
	vTaskSwitchContext();
	portRESTORE_CONTEXT();

	asm volatile ( "ret" );
}
/*-----------------------------------------------------------*/

/*
 * Context switch function used by the tick. This must be identical to 
 * vPortYield() from the call to vTaskSwitchContext() onwards. The only
 * difference from vPortYield() is the tick count is incremented as the
 * call comes from the tick ISR.
 */
void vPortYieldFromTick( void ) __attribute__ ( ( naked ) );
void vPortYieldFromTick( void )
{
	portSAVE_CONTEXT();
	if( xTaskIncrementTick() != pdFALSE )
	{
		vTaskSwitchContext();
	}
	portRESTORE_CONTEXT();

	asm volatile ( "reti" );
}
/*-----------------------------------------------------------*/

```

What is the solution to this problem?

Thanks  
Jerry

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 28, 2021, 2:11am UTC](https://forums.freertos.org/t/non-asm-statement-in-naked-function-is-not-supported-port-c-module-for-freertos/13964/2 "2021-11-28T02:11:02Z")

</div>

What is AS7?

Please post the compiler command line and output - at least the output that shows the error.

---

<div class="post-metadata">

**Author:** ![jmulchin](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@jmulchin](https://forums.freertos.org/u/jmulchin)\
**Post date:** [November 28, 2021, 5:34am UTC](https://forums.freertos.org/t/non-asm-statement-in-naked-function-is-not-supported-port-c-module-for-freertos/13964/3 "2021-11-28T05:34:41Z")

</div>

AS7 is Atmel Studio 7 (Microchip Studio since the buyout). It uses GCC for the compiler, but I’m not sure what version. But it is pretty recent. I do not know the command to generate the error as it is part of the Studio 7 build process. The exact error message (two of them) is:  
“(N) 2465:non-ASM statement in naked function is not supported” That’s all I have.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [November 28, 2021, 7:01am UTC](https://forums.freertos.org/t/non-asm-statement-in-naked-function-is-not-supported-port-c-module-for-freertos/13964/4 "2021-11-28T07:01:43Z")

</div>

I had to look it up and found that several compilers do not allow to put C-statements in a functions which has the naked attribute. The reason is that it is considered [not safe](https://stackoverflow.com/questions/47316611/clang-error-non-asm-statement-in-naked-function-is-not-supported?rq=1).

Now I wonder if it is allowed to call the C-function from within an inline statement:

```auto
extern "C" void vPortYield( void ) __attribute__ ( ( naked ) );
void vPortYield( void )
{
	portSAVE_CONTEXT();
	/* Call vTaskSwitchContext() */
	asm volatile ( "call vTaskSwitchContext" );
	portRESTORE_CONTEXT();

	asm volatile ( "ret" );
}

```

Note that portSAVE/portRESTORE only contain ASM statements. Can you try this?

**NOTE** : I used the prefix `extern "C"` because I testing the compilation with a C++ compiler.

---

<div class="post-metadata">

**Author:** ![jmulchin](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@jmulchin](https://forums.freertos.org/u/jmulchin)\
**Post date:** [November 28, 2021, 7:26pm UTC](https://forums.freertos.org/t/non-asm-statement-in-naked-function-is-not-supported-port-c-module-for-freertos/13964/5 "2021-11-28T19:26:29Z")

</div>

Hein,  
Thanks for your help. Your solution solves the compiler problem and now GCC is happy.

Thanks again  
Jerry

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [November 30, 2021, 12:55pm UTC](https://forums.freertos.org/t/non-asm-statement-in-naked-function-is-not-supported-port-c-module-for-freertos/13964/6 "2021-11-30T12:55:42Z")

</div>

Good to hear. Did you get FreeRTOS running now?

---

<div class="post-metadata">

**Author:** ![jmulchin](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@jmulchin](https://forums.freertos.org/u/jmulchin)\
**Post date:** [November 30, 2021, 7:07pm UTC](https://forums.freertos.org/t/non-asm-statement-in-naked-function-is-not-supported-port-c-module-for-freertos/13964/7 "2021-11-30T19:07:29Z")

</div>

Hein,  
Yes I have 8 task toggling LEDs on an XMega128A1 part. Your suggestion was actually a very good one. Especially with what must be changes in GCC that caused this problem. I always include a Heartbeat LED that’s toggled from inside the ISR handler to let me know that the CPU is at least processing interrupts. But in order to do that, I placed an asm volatile (“hb\_togl”); to a function called void hb\_togl(void) that bumps a counter, checks if the counter has reached 1000, if yes, set the counter to zero and toggle the LED. That function is in C code, and the heartbeat LED toggles as well. I also looked at the .LSS listing to see if the asm statements were actually calling the required function, and they are. Another warm fuzzy feeling that it had a possibility of working. So it was a great learning experience.

Thanks Again  
Jerry
