cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with code slowness

sim20
Associate II

Hi all,

I am using the Delay​ function as follows.

static void Delay(volatile int d) { 

 d = d*20; 

 while (d--) asm volatile("NOP"); 

}

​However, sometimes when adding printf code, Delay function becomes slow.

So, when I analyzed the cause, I was able to see that the assembly code changes a lot if volatile is used as the argument of the Delay function.

If volatile is not added, the slowdown seems to disappear.

I used STM32H743XI and set the optimization option to O2.

  • When volatile is not included in the argument assembly code

=> static void Delay(int d)

0806a27c <Delay>:

 806a27c: eb00 0080 add.w r0, r0, r0, lsl #2

 806a280: 0080   lsls r0, r0, #2

 806a282: 1e43   subs r3, r0, #1

 806a284: b118   cbz r0, 806a28e <Delay+0x12>

 806a286: bf00   nop

 806a288: f113 33ff adds.w r3, r3, #4294967295

 806a28c: d2fb   bcs.n 806a286 <Delay+0xa>

 806a28e: 4770   bx lr

  • Assembly code when volatile is added to the argument

=> static void Delay(volatile int d)

0806a27c <Delay>:

 806a27c: b082   sub sp, #8

 806a27e: 9001   str r0, [sp, #4]

 806a280: 9b01   ldr r3, [sp, #4]

 806a282: eb03 0383 add.w r3, r3, r3, lsl #2

 806a286: 009b   lsls r3, r3, #2

 806a288: 9301   str r3, [sp, #4]

 806a28a: 9b01   ldr r3, [sp, #4]

 806a28c: 1e5a   subs r2, r3, #1

 806a28e: 9201   str r2, [sp, #4]

 806a290: b12b   cbz r3, 806a29e <Delay+0x22>

 806a292: bf00   nop

 806a294: 9b01   ldr r3, [sp, #4]

 806a296: 1e5a   subs r2, r3, #1

 806a298: 9201   str r2, [sp, #4]

 806a29a: 2b00   cmp r3, #0

 806a29c: d1f9   bne.n 806a292 <Delay+0x16>

 806a29e: b002   add sp, #8

 806a2a0: 4770   bx lr

 806a2a2: bf00   nop

I do not know why the code changes a lot by adding volatile, so I ask this question.​

I'd like to know the definitive answer to whether this can slow down the code a lot.​​

Thank you.

Best Regards,

Gyosun.​

1 REPLY 1

This is the purpose of volatile variables, they cannot be optimized out and each access must be performed be the compiler.

JW