Why Inserting Several NOP Instructions Changes the Power Consumption of an MCU?

Why Inserting Several NOP Instructions Changes the Power Consumption of an MCU?

I recently tested the operating power consumption of an M0+ MCU. The test code used the simplest method, which is to run an empty loop while(1) in the main function. The measured current was 1.11mA, using the KEIL MDK IDE with optimization level 0.

Why Inserting Several NOP Instructions Changes the Power Consumption of an MCU?

When I inserted 3 NOP instructions before the while(1), the measured current changed to 0.89mA.

Why Inserting Several NOP Instructions Changes the Power Consumption of an MCU?

What is going on here? Is it a measurement error, or is it really the case? This is a difference of over 200uA! To investigate further, I conducted several comparative experiments.

Test Conditions

Power Consumption

Optimization Level 0, no NOP before while(1)

1.11mA

Optimization Level 0, insert 1 NOP before while(1)

0.90mA

Optimization Level 0, insert 2 NOPs before while(1)

1.11mA

Optimization Level 0, insert 3 NOPs before while(1)

0.89mA

Optimization Level 0, insert 4 NOPs before while(1)

1.12mA

Optimization Level 0, insert 5 NOPs before while(1)

0.91mA

Optimization Level 0, insert 6 NOPs before while(1)

1.11mA

Optimization Level 0, insert 7 NOPs before while(1)

0.88mA

Optimization Level 0, insert 8 NOPs before while(1)

1.11mA

From the above experiments, a clear pattern can be observed: whenever an odd number of NOPs are inserted before while(1), the power consumption is relatively lower (around 0.9mA), while an even number of NOPs results in higher power consumption (around 1.11mA).

At this point, we need to understand the NOP instruction. My previous understanding of the NOP instruction was limited to its use for software delays, but it also has an important role in achieving instruction alignment.

Why Inserting Several NOP Instructions Changes the Power Consumption of an MCU?

In the debugging window, let’s take a look at the assembly code.

Why Inserting Several NOP Instructions Changes the Power Consumption of an MCU?

The C code’s while(1) is compiled into 2 instructions, namely NOP and B, with the jump instruction B automatically inserting a NOP before it. The while(1) actually executes a NOP instruction first, followed by the B instruction, which jumps to its own address, achieving an infinite loop effect. At this point, the address of the NOP instruction in while(1) is 0x00000152 (decimal 338), and the address of the B instruction is 0x00000154 (decimal 340).

When an odd number of NOP instructions are inserted before while(1), the corresponding instruction address of while(1) changes.

Why Inserting Several NOP Instructions Changes the Power Consumption of an MCU?

Why does the change in instruction address affect power consumption? This brings us to the CPU’s instruction execution process.

Why Inserting Several NOP Instructions Changes the Power Consumption of an MCU?

The CPU continuously executes the process of Fetch (fetching instructions) –> Decode (decoding instructions) –> Execute (executing instructions).

When the CPU fetches instructions during program execution, it reads 32-bit instructions from Flash in a 4-byte aligned manner. If an even (including 0) number of NOP instructions are inserted before while(1), the CPU needs to read 32-bit content from Flash twice to decode and execute while(1). If an odd number of NOP instructions are inserted before while(1), the CPU only needs to read 32-bit content from Flash once. This difference in operation leads to a difference in power consumption, as the former requires more operations, resulting in slightly higher power consumption.

Additionally, if no NOP is added before while(1) but the optimization level is set to the highest, the B instruction will not have a NOP inserted before it, and the address of the B instruction will be 0x00000152. This effect is similar to not having optimization and inserting an odd number of NOPs before while(1), resulting in lower power consumption. The reasoning is the same, as the execution of while(1) only requires fetching from address 0x150 once.

Why Inserting Several NOP Instructions Changes the Power Consumption of an MCU?

Finally, I conducted another experiment where I placed the program in RAM. Regardless of how many NOPs were added before while(1), the power consumption remained the same at 0.58mA. When the program is in RAM, there is no need to read from Flash, resulting in lower power consumption.Using differences in power consumption for cracking behavior follows a similar principle.The above analysis is merely speculation, as I do not fully understand the internal workings of the MCU. If there are any inaccuracies, I welcome corrections from everyone.

Scan to Join

Microcontroller Technology Exchange Group

Why Inserting Several NOP Instructions Changes the Power Consumption of an MCU?

END

Popular Recommendations:

Which comes first, the fuse or the TVS? Two groups of engineers are arguing!

Profitable products are easily cracked this way.

Disassembling a telecom set-top box, a textbook case of hardware design to reduce costs!

Why is it often required for MOSFETs to turn off quickly, but not to turn on quickly?

Why do MOSFETs need driver circuits?

Is there an anomaly in the battery sampling circuit? Almost made me run away…

Guess what! Is it possible to replace the inductor when the power ripple is large?

I am the best lighting master in the electronics circle, the brightest guy!

If hardware engineers do not understand pricing, do not take private jobs! Otherwise…

Why Inserting Several NOP Instructions Changes the Power Consumption of an MCU?

Leave a Comment