Return value of function disappears (not allways)

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Return value of function disappears (not allways)

跳至解决方案
2,154 次查看
flow
Contributor I
   
Hi everybody.

This is not a problem of missing function declaration or missing header or "implicit parameter-declarations",
as I enabled the -Wpd option.


problem:

I call following function:

unsigned int readSRAM_int(unsigned long add);


twice in a loop. Sadly, only the second time (better : all odd times) in the loop when the function is called, it really returns the unsigned int value into the local variable of the calling function.
In the function itself its everything going well, as explained in the compiler manual, the return value is stored into D register.
Looking in the Assembly window of the debugger (BDM) I can see, when this happens, that the "STD" instruction simply isn't present, that's why the return value is lost.


I disabled any kind of code optimization and still the return value is lost !! And still all odd times I get the return value... Seems problem of compiler optimization, doesn't ??

Here's part of my code:


    sram_add = (unsigned long) (add + 3);              

    for(i=0; i< (bitcounter*2); i+=2){

      //get length of "send" part:
      tempint2 = readSRAM_int(sram_add);
      asm STD tempint2;                       //dumb help !!!!!!!!!!!!!!!!!!!!!!

      ulong1 =  (unsigned long)  (carrier * ((unsigned long) tempint2));  // length * carrier period
      ulong1 *= 2412;
      ulong1 /= 100000; // /1000 / 10 = temp in multiples of 10 us

      if(ulong1 < (0xFFFF)){
          buffer[i+offset] = (unsigned int) (ulong1);          
          }


      //calculate "not send" part:
      sram_add++;
      tempint2 = readSRAM_int(sram_add);

      ulong1 =  (unsigned long)  (carrier * ((unsigned long) tempint2));  // length * carrier period
      ulong1 *= 2412;
      ulong1 /= 100000;                 // val in mults of 10 us

      if(ulong1 < (0xFFFF)){ //(0xFFFF+60)
          buffer[i+offset+1] = (unsigned int) (ulong1);     
          }

      sram_add++;
      }


In the very desperate hours I finally did the STD instruction manually, so it "works fine"!!!!!!!! What a dumb solution :smileysad:
And whats even more dumb : now that I do it manually, the compiler also does.. 2 times the STD....

Any suggestions ?? Please help..

Thanks


标签 (1)
标记 (1)
0 项奖励
回复
1 解答
1,017 次查看
flow
Contributor I

OK, just to terminate this thread case somebody is looking to it:

Yes, it was the case that the values of variables weren't displayed correctly,
to add the -onu option in the compiler options resolved the problem !!

Thanks

在原帖中查看解决方案

0 项奖励
回复
2 回复数
1,017 次查看
CompilerGuru
NXP Employee
NXP Employee
Is the issue that the value of the local variable tempint2 is not displayed correctly, or is the issue that the code does not run as expected?
I guess its the first one, as the tempint2 variable is only used immediately after it is assigned, and later it is always overwritten. So the compiler can just not store it in the local variable, but I see that this is not so nice for debugging. I would try to disable the -onu optimization and see if this helps. If not, (not sure), making the variable volatile  (I don't like that) or checking the value in the register (with a big comment instead of the HLI STD).


Daniel
0 项奖励
回复
1,018 次查看
flow
Contributor I

OK, just to terminate this thread case somebody is looking to it:

Yes, it was the case that the values of variables weren't displayed correctly,
to add the -onu option in the compiler options resolved the problem !!

Thanks

0 项奖励
回复