0

I am trying to write some code in inline assembly for ATmega328P I found that my output variable changes not in way i expected. So i reduced original code to this:

uint8_t a=0;
__asm__ (
        "nop " "\n\t"
        :"=&r" (a)
        : 
        );

I am expecting that value of a will remain same, but depending of where in my programm i put this code value of a sometimes changes to something else. I am don't understanding why this happens. Will be glad if someone will explain to me what i am missing.

Alex
  • 2,009
  • 6
  • 24
  • 27
  • Maybe looking at the generated assembly code by gcc will provide some hints – dim Apr 25 '16 at 12:48
  • @dim I am new to this. Don't know how :) I am using Microsoft Visual studio with vMicro plagin (Arduino) – Alex Apr 25 '16 at 12:50
  • Ok, I never used this plugin, but I'm using Visual Studio regularily for PC development, so maybe it's the same: you can have the disassembly while you're debugging (I suggest putting a breakpoint just before your piece of code) from the menu Debug -> Windows -> Disassembly (or something like that, mine is in French) – dim Apr 25 '16 at 13:18
  • @dim I will try. THank you – Alex Apr 25 '16 at 13:27
  • Since you only list `a` as an output, the existing value (a=0) is meaningless, since it is never used. If you are enabling optimizations, the assignment can be discarded by gcc. Try using `+&r`. Other alternatives might be appropriate depending on what the original code looks like. – David Wohlferd Apr 26 '16 at 07:59

0 Answers0