Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22004

GCC - Volatile Variable (global vs. local)

$
0
0

if I set a global volatile variable (uint8_t) to 0 and modify it direct by memory manipulation to 1. The programm won't go to the asm("nop") statement.

If I place the variable in local scope like below, the same manipulation by memory works perfectly.Can someone tell me, why this is and/or is this the defined behaviour?

Thanks in advance

Not Working:

volatile uint8_t testVolatileVar;int main(void){    testVolatileVar = 0;    for(;;)    {        if (testVolatileVar == 1)        {            asm("nop");        }    }}

Corresponding disassembly (looks wiered to me):

          main:push    {r7}add     r7, sp, #0        testVolatileVar = 0;ldr     r3, [pc, #16]   ; (0x200003bc <main+24>)movs    r2, #0strb    r2, [r3, #0]            if (testVolatileVar == 1)ldr     r3, [pc, #12]   ; (0x200003bc <main+24>)ldrb    r3, [r3, #0]uxtb    r3, r3cmp     r3, #1bne.n   0x200003ae <main+10>                asm("nop");nop                 if (testVolatileVar == 1)b.n     0x200003ae <main+10>movs    r0, r7movs    r0, #32

Works:

int main(void){    volatile uint8_t testVolatileVar = 0;    for(;;)    {        if (testVolatileVar == 1)        {            asm("nop");        }    }}
          main:push    {r7}sub     sp, #12add     r7, sp, #0    volatile uint8_t testVolatileVar = 0;movs    r3, #0strb    r3, [r7, #7]        if (testVolatileVar == 1)ldrb    r3, [r7, #7]uxtb    r3, r3cmp     r3, #1bne.n   0x20000b86 <main+10>                asm("nop");nop             if (testVolatileVar == 1)b.n     0x20000b86 <main+10>

Condition true - but not jump to asm("nop")

Disassembly Breakpoint

Disassembly Step Trace:

ldr     r3, [pc, #12]   ; (0x200003bc <main+24>)R3 -> 0x20200038ldrb    r3, [r3, #0]R3 -> 0uxtb    r3, r3R3 -> 0 cmp     r3, #1R3 -> 0

So the assembler code do what it states - but this shouldn't be the assembler code for that c code at all. Why is a fixed zero loaded into R3?


Viewing all articles
Browse latest Browse all 22004

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>