对于代码:
uint8_t count;
ISR(TIMER1_OVF_vect, ISR_NAKED)
{
count++;
reti();
}
生成的程序集是:
--- F:\atmel-prj\compiler-test2\compiler-test2\Debug/.././compiler-test2.c -----
{
00000048 PUSH R1 Push register on stack
00000049 PUSH R0 Push register on stack
0000004A IN R0,0x3F In from I/O location
0000004B PUSH R0 Push register on stack
0000004C CLR R1 Clear Register
0000004D PUSH R24 Push register on stack
count++;
0000004E LDS R24,0x0100 Load direct from data space
00000050 SUBI R24,0xFF Subtract immediate
00000051 STS 0x0100,R24 Store direct to data space
}
00000053 POP R24 Pop register from stack
00000054 POP R0 Pop register from stack
00000055 OUT 0x3F,R0 Out to I/O location
00000056 POP R0 Pop register from stack
00000057 POP R1 Pop register from stack
00000058 RETI Interrupt return
现在,正如我所见,至少可以消除一个,最多两个 push-pop 对:
要保存一个 push-pop,请改用 r0 来消除 r24
PUSH R1 Push register on stack PUSH R0 Push register on stack IN R0,0x3F In from I/O location PUSH R0 Push register on stack CLR R1 Clear Register count++; LDS R0,0x0100 Load direct from data space SUBI R0,0xFF Subtract immediate STS 0x0100,R0 Store direct to data space ...
看到没有代码将 r1 用于 0 值,因此仅将 r1 用于所有目的。
PUSH R1 Push register on stack IN R1,0x3F In from I/O location PUSH R1 Push register on stack CLR R1 Clear Register count++; LDS R0,0x0100 Load direct from data space SUBI R0,0xFF Subtract immediate STS 0x0100,R0 Store direct to data space ...
这些中的任何一个都为我们节省了宝贵的字节和微秒。
有没有办法可以以某种方式将这些或类似的优化放入 atmel 工作室工具链/库中,以便我的编译代码生成得更好一些?
许多围绕中断和函数调用的代码,以及一些 C 到汇编的翻译都可以进行很多优化。