Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/01/06 06:07
Modified:
  09/01/06 06:09

Read: times


 
#123485 - volatile
Responding to: ???'s previous message
hi,

As well, keyword "volatile" prevents optimizer of skiping re-reading variable when seems that it has not been changed. This keyword notices compiler that variable may be changed it any time not depended on actual code processed.
Here is 2 little examples.
First code:
void main(void)
{
 unsigned char a;
 unsigned int xdata b; 
 while(1)
  {
	a = 5;
	b = a;
  }
}
Second one:
void main(void)
{
 volatile unsigned char a;
 unsigned int xdata b; 
 while(1)
  {
	a = 5;
	b = a;
  }
}
The difference is only that variable "a" is volatile in the second example.
Now try look at actual assembler code generated:
First one:
; a = 5;
MOV      R7,#0x05
; b = a;
MOV      DPTR,#0x0000
CLR      A
MOVX     @DPTR,A
INC      DPTR
MOV      A,#0x05
MOVX     @DPTR,A
And second one:
a = 5;
MOV      0x08,#0x05
; b = a; 
MOV      R7,0x08
MOV      DPTR,#0x0000
CLR      A
MOVX     @DPTR,A
INC      DPTR
MOV      A,R7
MOVX     @DPTR,A
You may see that the first code does not pay attention on actual value of variable "a" when copies it to "b". It just writes number 5. Second code tries re-read value of variable "a" before copy it to "b".

So come back. It seems that in Mark`s code there are some variables which are loaded directly and then have been changed with "not-visible" way. Let say, by work with their pointers, from other part of program in other project etc.

Regards,
Oleg

List of 24 messages in thread
TopicAuthorDate
^= , Checksum, Problem            01/01/70 00:00      
   Have you tried a simulator?            01/01/70 00:00      
      well,            01/01/70 00:00      
      OK            01/01/70 00:00      
   volatile?            01/01/70 00:00      
      volatile            01/01/70 00:00      
         using ICE ?            01/01/70 00:00      
            update code (working) and clarification            01/01/70 00:00      
               Style            01/01/70 00:00      
               Think about your variable types            01/01/70 00:00      
                  Thanks            01/01/70 00:00      
                     Magic numbers            01/01/70 00:00      
                        but don't be "oversmart"            01/01/70 00:00      
                           example?            01/01/70 00:00      
                           advantages            01/01/70 00:00      
                              the most often forgotten quality guarantee            01/01/70 00:00      
                     so, use structures!            01/01/70 00:00      
                        padding            01/01/70 00:00      
                   and             01/01/70 00:00      
                     Actual Output            01/01/70 00:00      
                     C99            01/01/70 00:00      
                        making up your own            01/01/70 00:00      
                           Names            01/01/70 00:00      
                     FYI - C99 Exact- & Minimum-width types            01/01/70 00:00      

Back to Subject List