⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter 7 pointers -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0056)http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm -->
<HTML><HEAD><TITLE>Chapter 7: Pointers -- Valvano</TITLE>
<META http-equiv=content-type content=text/html;charset=iso-8859-1>
<META content="MSHTML 5.50.3825.1300" name=GENERATOR>
<META 
content="Power HD:Applications:Microsoft Office 98:Templates:Web Pages:Blank Web Page" 
name=Template></HEAD>
<BODY vLink=#800080 link=#0000ff>
<P><!--Developing Embedded Software in C using ICC11/ICC12/Hiware by Jonathan W. Valvano--><B><FONT 
face=Helvetica,Arial size=4>Chapter 7: Pointers</FONT></B></P>
<P><B><I><FONT face=Helvetica,Arial>What's in Chapter 7?</FONT></I></B></P>
<DIR>
<P><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm#ADDRESS">Definitions 
of address and pointer</A> <FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm#DECLARATIONS">Declarations 
of pointers define the type and allocate space in memory</A> <FONT 
face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm#REFERENCING">How 
do we use pointers</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm#MEMORY">Memory 
architecture of the 6811 and 6812</A> <FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm#MATH">Pointer 
math</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm#COMPARE">Pointer 
comparisons</A><FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm#FIFO">FIFO queue 
implemented with pointers</A> <FONT face=Monaco><BR></FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm#IO">I/O port 
access</A> </P></DIR>
<P><FONT face="Times New Roman,Times">The ability to work with memory addresses 
is an important feature of the C language. This feature allows programmers the 
freedom to perform operations similar to assembly language. Unfortunately, along 
with the power comes the potential danger of hard-to-find and serious run-time 
errors. In many situations, array elements can be reached more efficiently 
through pointers than by subscripting. It also allows pointers and pointer 
chains to be used in data structures. Without pointers the run-time dynamic 
memory allocation and deallocation using the heap would not be possible. We will 
also use a format similar to pointers to develop mechanisms for </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm#IO">accessing I/O 
ports</A><FONT face="Times New Roman,Times">. These added degrees of flexibility 
are absolutely essential for embedded systems.</FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=ADDRESS></A>Addresses and 
Pointers</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Addresses that can be stored and changed 
are called <I>pointers</I>. A pointer is really just a variable that contains an 
address. Although, they can be used to reach objects in memory, their greatest 
advantage lies in their ability to enter into arithmetic (and other) operations, 
and to be changed. Just like other variables, pointers have a type. In other 
words, the compiler knows the format (8-bit 16-bit 32-bit, unsigned signed) of 
the data pointed to by the address.</FONT></P>
<P><FONT face="Times New Roman,Times">Not every address is a pointer. For 
instance, we can write <B>&amp;var</B> when we want the address of the variable 
<B>var</B>. The result will be an address that is not a pointer since it does 
not have a name or a place in memory. It cannot, therefore, have its value 
altered.</FONT></P>
<P><FONT face="Times New Roman,Times">Other examples include an array or a 
structure name. As we shall see in <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap8/chap8.htm">Chapter 8</A>, 
an unsubscripted array name yields the address of the array. In <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm">Chapter 9</A>, a 
structure name yields the address of the structure. But, since arrays and 
structures cannot be moved around in memory, their addresses are not variable. 
So, although, such addresses have a name, they do not exist as objects in memory 
(the array does, but its address does not) and cannot, therefore, be 
changed.</FONT></P>
<P><FONT face="Times New Roman,Times">A third example is a character string. <A 
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm">Chapter 3</A> 
indicated that a character string yields the address of the character array 
specified by the string. In this case the address has neither a name or a place 
in memory, so it too is not a pointer.</FONT></P>
<P>&nbsp;</P>
<P><B><I><FONT face=Helvetica,Arial><A name=DECLARATIONS></A>Pointer 
Declarations</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">The syntax for declaring pointers is like 
that for variables (<A 
href="http://www.ece.utexas.edu/~valvano/embed/chap4/chap4.htm">Chapter 4</A>) 
except that pointers are distinguished by an asterisk that prefixes their names. 
Listing 7-1 illustrates several legitimate pointer declarations. Notice, in the 
third example, that we may mix pointers and variables in a single declaration. 
I.e., the variable data and the pointer pt3 are declared in the same statement. 
Also notice that the data type of a pointer declaration specifies the type of 
object to which the pointer refers, not the type of the pointer itself. As we 
shall see, all ICC11 and ICC12 pointers contain 16-bit unsigned absolute 
addresses. This means that the ICC12 compiler does not provide for direct 
support of the extended memory available on the MC68HC812A4 microcomputer. There 
is an example from Valvano's </FONT><U>Embedded Microcomputer Systems: Real Time 
Interfacing</U><FONT face="Times New Roman,Times"> shown in </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm">Chapter 
9</A><FONT face="Times New Roman,Times">, which, while illustrating structures, 
implements an extended address mechanism for the MC68HC812A4.</FONT></P>
<DIR>
<P><CODE>short *pt1;&nbsp;&nbsp;&nbsp;&nbsp;/* define pt1, declare as a pointer 
to a 16-bit integer */<BR>char *pt2;&nbsp;&nbsp;&nbsp;/* define pt2, declare as 
a pointer to an 8-bit character */<BR>unsigned short 
data,*pt3;&nbsp;&nbsp;&nbsp;&nbsp;/* define data and pt3, 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;declare data as an unsigned 16-bit integer and 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;declare pt3 as a pointer to a 16-bit unsigned 
integer */<BR>long *pt4;&nbsp;&nbsp;&nbsp;/* define pt4, declare as a pointer to 
a 32-bit integer */<BR>extern short *pt5;&nbsp;&nbsp;&nbsp;&nbsp;/* declare pt5 
as a pointer to an integer */</CODE></P></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Listing 7-1: Example showing a 
pointer declarations</FONT></ADDRESS>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times">The best way to think of the asterisk is 
to imagine that it stands for the phrase "object at" or "object pointed to by." 
The first declaration in Listing 7-1 then reads "the object at (pointed to by) 
<B>pt1</B> is a 16-bit signed integer."</FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=REFERENCING></A>Pointer 
Referencing</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">We can use the pointer to retrieve data 
from memory or to store data into memory. Both operations are classified as 
<I>pointer references</I>. The syntax for using pointers is like that for 
variables except that pointers are distinguished by an asterisk that prefixes 
their names. Figures 7-1 through 7-4 illustrate several legitimate pointer 
references. In the first figure, the global variables contain unknown data 
(actually we know ICC11/ICC12/Hiware will zero global variables). The arrow 
identifies the execution location. Assume addresses 0x0810 through 0x081A exist 
in RAM.</FONT></P>
<ADDRESS><IMG height=302 src="Chapter 7 Pointers -- Valvano.files/pt.gif" 
width=369></ADDRESS>
<ADDRESS><FONT face="Times New Roman,Times">Figure 7-1: Pointer 
Referencing</FONT></ADDRESS>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times">The expression <B>&amp;buffer[1]</B> 
returns the address of the second 16 bit element of the <B>buffer</B> (0x0816). 
Therefore the line <B>pt=&amp;buffer[1];</B> makes <B>pt</B> point to 
<B>buffer[1]</B>.</FONT></P>
<P>When the <B>*pt</B> occurs on the left-hand-side of an assignment statement 
data is stored into memory at the address. Recall the <B>*pt</B> means "the 
16-bit signed integer at 0x0816". I like to add the parentheses () to clarify 
that <B>*</B>and <B>pt</B> are one object. In this case the parentheses are not 
needed. Later when we perform address arithmetic, the parentheses will be 
important. Therefore the line <B>(*pt)=0x1234;</B> sets <B>buffer[1]</B> to 
0x1234.</P>
<P><FONT face="Times New Roman,Times">When the <B>*pt</B> occurs on the 
right-hand-side of an assignment statement data is retrieved from memory at the 
address. Again, I like to add the parentheses () to clarify that * and pt are 
one object. Therefore the line <B>data=(*pt);</B> sets <B>data</B> to 0x1234 
(more precisely, it copies the 16-bit information from <B>buffer[1]</B> into 
<B>data</B>.)</FONT></P>
<P><FONT face="Times New Roman,Times">We can get a better understanding of 
pointers by observing the assembly generated by our compiler. The following 6811 
assembly was generated by ICC11 Version 4 when the above pointer example (figure 
7-1) was compiled.</FONT></P>
<DIR>
<P><CODE>&nbsp;&nbsp;&nbsp;&nbsp;.area&nbsp;text<BR>&nbsp;&nbsp;&nbsp;&nbsp;.globl&nbsp;_main<BR>_main:&nbsp;&nbsp;ldd&nbsp;#_buffer+2&nbsp;&nbsp;; 
pt=&amp;buffer[1];<BR>&nbsp;&nbsp;&nbsp;&nbsp;std&nbsp;&nbsp;_pt<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldy&nbsp;&nbsp;_pt&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;(*pt)=0x1234;<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldd&nbsp;&nbsp;#4660<BR>&nbsp;&nbsp;&nbsp;&nbsp;std&nbsp;&nbsp;0,y<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldy&nbsp;&nbsp;_pt&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;data=(*pt);<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldd&nbsp;&nbsp;0,y<BR>&nbsp;&nbsp;&nbsp;&nbsp;std&nbsp;&nbsp;_data<BR>&nbsp;&nbsp;&nbsp;&nbsp;rts<BR>&nbsp;&nbsp;&nbsp;&nbsp;.area 
bss<BR>&nbsp;&nbsp;&nbsp;&nbsp;.globl&nbsp;_pt<BR>_pt:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.blkb&nbsp;2<BR>&nbsp;&nbsp;&nbsp;&nbsp;.globl&nbsp;_data<BR>_data:&nbsp;&nbsp;&nbsp;.blkb&nbsp;2<BR>&nbsp;&nbsp;&nbsp;&nbsp;.globl&nbsp;_buffer<BR>_buffer:&nbsp;.blkb&nbsp;8</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The following 6812 assembly was generated 
by ICC12 Version 5.1 when the above pointer example (figure 7-1) was 
compiled.</FONT></P>
<DIR>
<P><CODE>&nbsp;&nbsp;&nbsp;&nbsp;.area&nbsp;text<BR>_main::&nbsp;movw 
#_buffer+2,_pt&nbsp;&nbsp;;&nbsp;pt=&amp;buffer[1];<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldd&nbsp;&nbsp;#4660&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;(*pt)=0x1234;<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldy&nbsp;&nbsp;_pt<BR>&nbsp;&nbsp;&nbsp;&nbsp;std&nbsp;&nbsp;0,y<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldy&nbsp;&nbsp;_pt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;&nbsp;&nbsp;data=(*pt);<BR>&nbsp;&nbsp;&nbsp;&nbsp;ldy&nbsp;&nbsp;0,y<BR>&nbsp;&nbsp;&nbsp;&nbsp;sty&nbsp;&nbsp;_data<BR>&nbsp;&nbsp;&nbsp;&nbsp;rts<BR>&nbsp;&nbsp;&nbsp;&nbsp;.area&nbsp;bss<BR>_pt::&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.blkb&nbsp;2<BR>_data::&nbsp;&nbsp;&nbsp;.blkb&nbsp;2<BR>_buffer::&nbsp;.blkb&nbsp;8</CODE></P>
<DIR>
<P>&nbsp;</P></DIR></DIR>
<P><B><I><FONT face=Helvetica,Arial><A name=MEMORY></A>Memory 
Addressing</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">The size of a pointer depends on the 
architecture of the CPU and the implementation of the C compiler. Both the 6811 
and 6812 employ an absolute memory addressing scheme in which an effective 
address is composed simply of a single 16-bit unsigned value. In particular the 
6811 and 6812 registers are shown in Figure 7-5. The MC68HC812A4 does provide 
for extended addressing. For more information on this feature see Chapter 9 of 
Valvano's </FONT><U>Embedded Microcomputer Systems: Real Time Interfacing<FONT 
face="Times New Roman,Times">.</FONT></U></P>
<DIR>
<DIR>
<P><IMG height=150 src="Chapter 7 Pointers -- Valvano.files/reg.gif" 
width=372></P></DIR></DIR>
<ADDRESS><FONT face="Times New Roman,Times">Figure 7-2: The 6811 and 6812 have 
16 bit address registers, X Y, SP PC. </FONT></ADDRESS>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times">Most embedded systems employ a segmented 
memory architecture. From a physical standpoint we might have a mixture of 
regular RAM, battery-backed-up RAM, regular EEPROM, flash EPROM, regular PROM, 
one-time-programmable PROM and ROM. RAM is the only memory structure that allows 
the program both read and write access. The other types are usually loaded with 
object code from our S19 file and our program is allowed only to read the data. 
Table 7-1 shows the various types of memory available in the 6811 and 6812 
microcomputer. The RAM contains temporary information that is lost when the 
power is shunt off. This means that all variables allocated in RAM must be 
explicitly initialized at run time by the software. If the embedded system 
includes a separate battery for the RAM, then information is not lost when the 
main power is removed. Some Motorola microcomputers have EEPROM. The number of 
erase/program cycles depends on the memory technology. EEPROM is often used as 
the main program memory during product development. In the final product we can 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -