CH55X系列指令速度求解

以下有两段代码和反汇编结果对比:

UINT8 colors[] = {0x00, 0x10, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};
// WS2812B发送1Byte数据
void WS_SendByte(UINT8 buffer)
{
UINT8 i; // 代码段一
//static UINT8 i; // 代码段二
for(i=8;i;--i)
{
if (buffer & 0x80)
{
LED = 1;
++SAFE_MOD;
++SAFE_MOD;
++SAFE_MOD;
}
else LED = 1;
++SAFE_MOD;
++SAFE_MOD;
LED = 0;
buffer <<= 1; // 低电平时序比较随意,要求不高
}
}
void main(void)
{
UINT8 i;
SAFE_MOD = 0x55; // 解锁安全模式
SAFE_MOD = 0xAA;
CLOCK_CFG = 0x86; // 系统时钟分频器4分频=24MHz
while (1)
{
mDelayuS(1000); // 延时以便示波器观察信号
for (i = 0; i != 9; ++i) WS_SendByte(colors[i]);
}
}

代码段一反汇编:

1658940673591938.png

代码段二反汇编:

1658940674837587.png


注:两段代码唯一的差别仅仅是一个 static 关键字

实测用示波器检测LED引脚,9次WS_SendByte()循环调用,代码段一比代码段二耗时长5us。

这个结果比较反直觉,毕竟代码段一的汇编指令更短,我查询指令周期表,也没发现DJNZ会比JNZ明显的消耗更多时间。

所以希望WCH官方能给予解答这个疑惑。


icon_pdf.gifCH55X指令周期.PDF


There are a few things to consider:

  1. a static for a loop var does not make any sense

  2. you are using large mode, therfore y static i will be placed in xdata (cant use a reg like in non static version)

  3. vars allways have to stored back after each loop in static version


So for optimized speed never use large mode and dont use static for a simple loop var. The Keil compiler is very smart and put your loopvar in R6 therefore can use DJNZ. By forcing it to be static it has to be stored back after every count.

By unrolling that loop it would be even faster, but uses more code


Read some C Book about the usage of the static keyword.

Here a short example;


#define WAIT ++SAFE_MOD;\
? ? ? ? ? ? ++SAFE_MOD;\
? ? ? ? ? ? ++SAFE_MOD \

void WS_SendByte(UINT8 buffer)
{
?LED = !(buffer&0x80);
?WAIT;
?LED = !(buffer&0x40);
?WAIT;
?LED = !(buffer&0x20);
?WAIT;
?LED = !(buffer&0x10);
?WAIT;
?LED = !(buffer&0x08);
?WAIT;
?LED = !(buffer&0x04);
?WAIT;
?LED = !(buffer&0x02);
?WAIT;
?LED = !(buffer &0x01);
?WAIT;
}



Sorry about the question marks. I dont know how to remove them


The question marks just a bug with this website, you can change font to MicrosoftYaHei, that can fix it.


Yes, i select the Large memory.

But i mean when i add "static", assembly code becomes more, but will run faster.


只有登录才能回复,可以选择微信账号登录