I am facing a problem in using the right shift operator in C with keil. The instruction is:for(i=0;i<=8;i++) { j=sum>>i; } where sum= 32 bit no. the instruction is not executed if the syntax is as shown above. but if i write a numerical value instead of using the variable name 'sum' then the instruction is easily executed. i need to use the variable name only. how do i fix this problem?
If the optimizer is smart enough, it could eliminate the loop entirely, and just replace it with "j = sum >> 8;" as that's the net effect of the loop. Have you checked the .lst and .cod files to see what the code generator generates? Alternatively, you could temporarily declare j and sum "volatile" to force the optimizer to read/write them on every pass. Or disable the optimizer.
ACC=sum; P0=ACC; B=j; P1=B;
More simply, just
P0=sum; P1=j;
In general, it's not safe to directly manipulate the 8051 registers from C code. You don't know exactly what the code generator will be doing with those registers to implement all the other C statements. For any given release of a particular compiler, you can inspect the assembly output and decide that in this particular case you can get away with it -- but things can change on the next compiler upgrade or even if you rearrange your code. The point of using a high-level language is to leave details like register management up to the compiler. It's tricky to mix the two.
these are the warnings i am getting after compiling the program: *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL SYMBOL: ?C_STARTUP MODULE: vkd.obj (VKD) *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL SYMBOL: ?C?SLSHR MODULE: vkd.obj (VKD) *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL SYMBOL: ?C?SLDIV MODULE: vkd.obj (VKD) *** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL SYMBOL: ?C?SLSHR MODULE: vkd.obj (VKD) ADDRESS: 008AH *** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL SYMBOL: ?C?SLDIV MODULE: vkd.obj (VKD) ADDRESS: 00A0H Program Size: data=46.0 xdata=0 code=170 creating hex file from "T8-11"... "T8-11" - 0 Error(s), 5 Warning(s). and because of these warnings the code is not getting executed
These are Linker warnings.
It'll be because you've used the SRC directive.
The SRC directive causes the compiler to convert 'C' source to assember source; it is then the assembler that converts this to object files.
Therefore, all the Linker sees is a load of assembler-generated objects; Because the Linker sees only assembler-generated objects, it doesn't automatically include the C runtime libraries - which is why you get these errors.
As has already been noted, you don't need inline assembler for any of this.
Therefore, re-write your code fully in 'C', and remove the SRC directive!
http://www.keil.com/support/docs/1980.htm
thank you so much!!!!!!! i did not have to rewrite the whole code in 'c'. the link you sent in your message provided the solution. i just had to add a file called 'C51S.LIB' to the source group and now the compiler is showing 0 errors and 0 warnings and my code is running successfully. thank you for all your help and time.
"i did not have to rewrite the whole code in 'c'"
True, but it would be wise to do so!
Using inline assembler and the SRC directive brings a number of issues - you have seen just one of them.
So, unless you really need it, do not use inline assembler and the SRC directive.
In general, if you need to use assembler it's far better to write a separate assembler module and call it from 'C'.
"Let your 'C' be 'C' and your Assembler be Assembler"
In your case, as already noted, there is no reason at all to use assembler - let alone inline assembler!