📄 chapter 4 variables and constants -- valvano.htm
字号:
replaced by a single colon,<B> :</B>, in the 6812 code. In both cases, this
static global can not be referenced outside the scope of this file.</FONT></P>
<DIR>
<P><CODE>static short TheGlobal; /* a static global
variable*/<BR>void main(void){
<BR> TheGlobal=1000; <BR>}</CODE></P></DIR>
<ADDRESS>Listing 4-3: Example showing a static global variable</ADDRESS>
<P><FONT face="Times New Roman,Times">The 6811 code generated by the ICC11
(Version 4) compiler is as follows</FONT></P>
<DIR>
<P><CODE> .area text <BR> .global
_main<BR>_main:<BR> ldd #1000
<BR> std _TheGlobal <BR> rts
<BR> .area bss <BR>_TheGlobal: .blkb 2 </CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The 6812 code generated by the ICC12
(Version 5.1) compiler is as follows</FONT></P>
<DIR>
<P><CODE> .area text <BR>_main::
<BR> movw #1000,_TheGlobal
<BR> rts <BR> .area bss
<BR>_TheGlobal: .blkb 2 </CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The 6812 code generated by the Hiware
compiler is the same as a regular global. Hiware does properly limit the access
only to the static global to functions defined in this file.</FONT></P>
<DIR>
<P><CODE>main: <BR> LDD #1000
<BR> STD TheGlobal <BR> RTS
<BR></CODE></P></DIR>
<P><FONT face="Times New Roman,Times"><A name=STATICLOCAL></A>A <B>static
local</B> is similar to the static global. Just as with the other statics, the
variable is defined in RAM permanently. The assembly language code generated by
the compiler that accesses the variable is identical. The only difference is the
scope. The static local can only be accessed within the function where it is
defined. The following example sets a static local, called <B>TheLocal</B>, to
the value 1000. The compiler limits the access to the static local, so that this
variable can not be accessed by other functions in this file or in other files.
Notice that the assembly language name of the static local is a unique
compiler-generated name (L2 in this example.) This naming method allows other
functions to also define a static local or automatic local with the same name.
</FONT></P>
<DIR>
<P><CODE>void main(void){ <BR> static stort
TheLocal; /* a static local
variable*/<BR> TheLocal=1000; <BR>}</CODE></P></DIR>
<ADDRESS>Listing 4-4: Example showing a static local variable</ADDRESS>
<P><FONT face="Times New Roman,Times">The 6811 code generated by the ICC11
(Version 4) compiler is as follows</FONT></P>
<DIR>
<P><CODE> .area text <BR> .global
_main<BR>_main:<BR> ldd #1000
<BR> std L2 <BR> rts
<BR> .area bss <BR>L2: .blkb 2 </CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The 6812 code generated by the ICC12
(Version 5.1) compiler is as follows</FONT></P>
<DIR>
<P><CODE> .area text <BR>_main::
<BR> movw #1000,L2 <BR> rts
<BR> .area bss <BR>L2: .blkb 2 </CODE></P></DIR>
<P><FONT face="Times New Roman,Times">Again the 6812 code generated by the
Hiware compiler is the same as a regular global. Hiware does properly limit the
access only to the static local to the function in which it is
defined.</FONT></P>
<DIR>
<P><CODE>main: <BR> LDD #1000
<BR> STD TheLocal <BR> RTS
<BR></CODE></P></DIR>
<P><FONT face="Times New Roman,Times">A <B>static local </B>can be used to save
information from one instance of the function call to the next. Assume each
function wished to know how many times it has been called. Remember upon reset,
the ICC11/ICC12/Hiware compilers will initialize all statics to zero (including
static locals). The following functions maintain such a count, and these counts
can not be accessed by other functions. Even though the names are the same, the
two static locals are in fact distinct.</FONT></P>
<DIR>
<P><CODE>void function1(void){ <BR> static short
TheCount;<BR> TheCount=TheCount+1;<BR>}<BR>void
function2(void){ <BR> static short
TheCount;<BR> TheCount=TheCount+1;<BR>}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 4-5: Example showing two
static local variables with the same name</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">The 6811 code generated by the ICC11
(Version 4) compiler is as follows</FONT></P>
<DIR>
<P><CODE> .area text <BR> .global
_function1<BR>_function1:<BR> ldd L2
<BR> addd #1 <BR> std L2
<BR> rts <BR> .global
_function2<BR>_function2:<BR> ldd L3
<BR> addd #1 <BR> std L3
<BR> rts <BR> .area bss <BR>L2:
.blkb 2<BR>L3: .blkb 2 </CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The 6812 code generated by the ICC12
(Version 5.1) compiler is as follows</FONT></P>
<DIR>
<P><CODE> .area text
<BR>_function1::<BR> ldd L2
<BR> addd #1 <BR> std L2
<BR> rts<BR>_function2::<BR> ldd
L3 <BR> addd #1 <BR> std L3
<BR> rts <BR> .area bss <BR>L2:
.blkb 2<BR>L3: .blkb 2 </CODE></P></DIR>
<P><B><I><FONT face=Helvetica,Arial><A
name=VOLATILE></A>Volatile</FONT></I></B><FONT face=Helvetica,Arial> </FONT></P>
<P><FONT face="Times New Roman,Times">We add the <B>volatile</B> modifier to a
variable that can change value outside the scope of the function. Usually the
value of a global variable changes only as a result of explicit statements in
the C function that is currently executing. The paradigm results when a single
program executes from start to finish, and everything that happens is an
explicit result of actions taken by the program. There are two situations that
break this simple paradigm in which the value of a memory location might change
outside the scope of a particular function currently executing: </FONT></P>
<DIR>
<DIR>
<P><FONT face="Times New Roman,Times">1) interrupts and<BR>2) input/output
ports. </FONT></P></DIR></DIR>
<P><FONT face="Times New Roman,Times">An interrupt is a hardware-requested
software action. Consider the following multithreaded interrupt example. There
is a foreground thread called <B>main()</B>, which we setup as the usual main
program that all C programs have. Then, there is a background thread called
<B>TOFhandler()</B>, which we setup to be executed on a periodic basis (e.g.,
every 16 ms). Both threads access the global variable, <B>Time</B>. The
interrupt thread increments the global variable, and the foreground thread waits
for time to reach 100. Notice that <B>Time</B> changes value outside the
influence of the <B>main() </B>program.</FONT></P>
<DIR>
<P><CODE>volatile char Time;<BR>#pragma interrupt_handler TOFhandler<BR>void
TOFhandler(void){ /* every 16ms
*/<BR> TFLG2=0x80; /* TOF
interrupt acknowledge */<BR> Time=Time+1;<BR>}<BR>void
main(void){ <BR> TSCR |=0x80; /* TEN(enable)
*/<BR> TMSK2=0xA2; /* TOI arm, TPU(pullup) timer/4
(500ns)
*/<BR> CLKCTL=0x00;<BR> Time=0;<BR> while(Time<100){}; /*
wait for 100 counts of the 16 ms timer*/<BR>}</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 4-6: ICC12 example showing
shared access to a common global variable</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">Without the <B>volatile</B> modifier the
compiler might look at the two statements:</FONT></P>
<DIR>
<P><CODE>Time=0;<BR>while(Time<100){}; </CODE></P></DIR>
<P><FONT face="Times New Roman,Times">and conclude that since the while loop
does not modify <B>Time</B>, it could never reach 100. Some compilers (not yet
in the current versions of ICC11 and ICC12) might attempt to move the read
<B>Time </B>operation, performing it once before the while loop is executed. The
<B>volatile</B> modifier disables the optimization, forcing the program to fetch
a new value from the variable each time the variable is accessed. </FONT></P>
<P><FONT face="Times New Roman,Times">In the next 6812 example, assume PORTA is
an input port containing the current status of some important external signals.
The program wishes to collect status versus time data of these external signals.
</FONT></P>
<DIR>
<P><CODE>unsigned char data[100];<BR>#define PORTA *(unsigned char volatile
*)(0x0000)<BR>#define DDRA *(unsigned char volatile *)(0x0004)<BR>void
main(void){ short i;<BR> DDRA=0x00; /* make Port A an
input */<BR> for(i=0;i<100;i++){ /* collect 100
measurements
*/<BR> data[i]=PORTA; /*
collect ith measurement */<BR> }<BR>}</CODE></P></DIR>
<ADDRESS>Listing 4-7: Example showed shared access to a common global
variable</ADDRESS>
<P><FONT face="Times New Roman,Times">Without the <B>volatile</B> modifier in
the PORTA definition, the compiler might optimize the for loop, reading PORTA
once, then storing 100 identical copies into the data array. </FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap5/chap5.htm#IO">I/O
ports</A><FONT face="Times New Roman,Times"> will be handled in more detail in
<A href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm">Chapter 7</A>
on pointers. </FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A
name=AUTOMATIC></A>Automatics</FONT></I></B><FONT face=Helvetica,Arial>
</FONT></P>
<P><FONT face="Times New Roman,Times">Automatic variables, on the other hand, do
not have fixed memory locations. They are dynamically allocated when the block
in which they are defined is entered, and they are discarded upon leaving that
block. Specifically, they are allocated on the 6811/6812 stack by subtracting a
value (one for characters, two for integers and four for long integers) from the
stack pointer register (SP). Since automatic objects exist only within blocks,
they can only be declared locally. Automatic variables can only be referenced
(read or write) by the function that created it. In this way, the information is
protected or local to the function.</FONT></P>
<P><FONT face="Times New Roman,Times">When a local variable is created it has no
dependable initial value. It must be set to an initial value by means of an
assignment operation. C provides for automatic variables to be initialized in
their declarations, like globals. It does this by generating "hidden" code that
assigns values automatically after variables are allocated space. </FONT></P>
<P><FONT face="Times New Roman,Times">It is tempting to forget that automatic
variables go away when the block in which they are defined exits. This sometimes
leads new C programmers to fall into the "dangling reference" trap in which a
function returns a pointer to a local variable, as illustrated by </FONT></P>
<DIR>
<P><CODE>int *BadFunction(void) {<BR> int
z;<BR> z=1000;<BR> return
(&z);<BR>}</CODE></P></DIR>
<ADDRESS>Listing 4-8: Example showing an illegal reference to a local
variable</ADDRESS>
<P><FONT face="Times New Roman,Times">When callers use the returned address of
<B>z </B>they will find themselves messing around with the stack space that <B>z
</B>used to occupy. This type of error is NOT flagged as a syntax error, but
rather will cause unexpected behavior during execution.</FONT></P>
<P> </P>
<P><B><I><FONT face=Helvetica,Arial><A name=IMPLEMENTATION></A>Implementation of
automatic variables</FONT></I></B><FONT face=Helvetica,Arial> </FONT></P>
<P><FONT face="Times New Roman,Times">If locals are dynamically allocated at
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -