3c3 < % $Header: G:\MICROP\56FTWS0\RCS\learnc.tex 1.6 1996/02/26 17:57:01 Nick Exp Nick $ --- > % $Header: C:/MICROP/56FTWS0/RCS/learnc.tex 1.8 1996/02/27 13:52:58 Nick Exp Nick $ 5a6,10 > % Revision 1.8 1996/02/27 13:52:58 Nick > % Have corrected the first errors I picked up from reading the first print > % out in the bus. I am sure that there are many other errors. > % At least I caught these before printing. > % 51c56 < % $Header: G:\MICROP\56FTWS0\RCS\learnc.tex 1.6 1996/02/26 17:57:01 Nick Exp Nick $ --- > % $Header: C:/MICROP/56FTWS0/RCS/learnc.tex 1.8 1996/02/27 13:52:58 Nick Exp Nick $ 53a59,63 > % Revision 1.8 1996/02/27 13:52:58 Nick > % Have corrected the first errors I picked up from reading the first print > % out in the bus. I am sure that there are many other errors. > % At least I caught these before printing. > % 265,266c275,276 < \RCS$Revision: 1.6 $ < \RCS$Date: 1996/02/26 17:57:01 $ --- > \RCS$Revision: 1.8 $ > \RCS$Date: 1996/02/27 13:52:58 $ 282,284c292 < \includeversion{notes} < \excludeversion{slides} < \includeversion{elecEng} --- > \newcommand*{\file}[1]{\texttt{#1}} 412c420,421 < \vspace*{0pt} --- > \vspace*{-1ex} > \enlargethispage{10cm} 414c423 < \sffamily\Huge\bfseries {\raggedright Learning and using\\[1ex]} --- > \sffamily\Huge\bfseries {\raggedright Learning and using\\[1.5ex]} 418,419c427 < \normalsize example programs by {\large Vincent Hui}\break < \vfill --- > \normalsize example programs by {\large Vincent Hui}\\[5.5ex] 481c489 < exercises and all examples that do not @#include exercises and all examples that do not @#include @ can be run 1045d1052 < \begin{elecEng} 1049d1055 < \end{elecEng} 1617c1623,1630 < Operators that act on one operand are called \emph{unary} operators. --- > Operators that act on one operand are called \emph{unary} > \index{operator!unary}% > \index{unary operator} > operators, while > those that act on two operands are called \emph{binary} > \index{operator!binary}% > \index{binary operator} > operators. 1772a1786,1791 > \index{assignment operator!multiple} > \index{operator!assignment!multiple} > Assignment operators can be used together in one statement to give > more than one variable the same value. See > program~\vref{prg:assignment} for an example of this. > 1814d1832 < \begin{notes} 1844,1847d1861 < \end{notes} < \begin{slides} < \lgrindfile{compound.lg} < \end{slides} 1953c1967 < In your laboratory work, the names @true@ and @false are used --- > In your laboratory work, the names @true@ and @false@ are used 1956c1970,1972 < \texttt{flight.h}. --- > \texttt{flight.h}. Read section~\ref{sec:include} and > section~\vref{sec:flight.h} for more information about header files > and \texttt{flight.h}. 1993c2009,2028 < \item @10 >= 0 && 30 <= 30 || ! ( 50 < 0 ) && 7 - 3 > 5@ --- > \item To solve this, you really need to look at > table~\vref{tab:operatorPrecedence} which shows operator > precedence, or in other words, which operator is evaluated first. > \label{soln:precedenceExample} > > From that table, we see that `@>=@', `@<=@', `@<@' and `@>@' have > precedence level 10, `@&&@' has precedence level 5, `@!@' has the > high priority of 14, `@||@' has a priority of 4 and binary `@-@' has a > priority of 12. The expressions are evaluated in order of this > priority, those with a higher precedence level first. Let us put > parentheses around this expression to see how it is evaluated by the C > compiler. > > @ 10 >= 0 && 30 <= 30 || ! ( 50 < 0 ) && 7 - 3 > 5@\\ > @= ( ( ( 10 >= 0 ) && ( 30 <= 30 ) ) )@\\ > @|| ( ! ( 50 < 0 ) && ( ( 7 - 3 ) > 5 ) )@\\ > @= ( 1 && 1 ) || ( ! 0 && ( 4 > 5 ) )@\\ > @= 1 || ( 1 && 0 )@\\ > @= 1 || 0@\\ > @= 1@, which has the logical value @true@. 2177,2178c2212,2213 < Here we call @0x78@ a \emph{bit mask} because we are using it to < hide the value of bits 0, 1, 2 and 7. --- > Here we call @0x78@ a \emph{bit mask}\index{bit mask} because we are > using it to hide the value of bits 0, 1, 2 and 7. 2811a2847,2878 > \label{ex:while2For} > Write a C program that is equivalent to the following program, but > which uses a @for@ loop instead of a @while@ loop. > %[ > #include > int main( void ) > { > int i = 0; > while ( i < 10 ) > { > printf( "i = %d\n", i ); > ++i; > } > return 0; > } > %] > \begin{Solution} > %[ > #include > int main( void ) > { > int i; > for ( i = 0; i < 10; ++i ) > printf( "i = %d\n", i ); > > return 0; > } > %] > \end{Solution} > \end{exercise} > > \begin{exercise} 3014c3081 < printf( "Press '2' on the key pad\n" ); --- > printf( "Press '2' on the telephone keypad\n" ); 3019c3086 < printf ( "The key '2' key is ok." ); --- > printf ( "The '2' key is ok." ); 3347,3377d3413 < \label{ex:while2For} < Write a C program that is equivalent to the following program, but < which uses a @for@ loop instead of a @while@ loop. < %[ < #include < int main( void ) < { < int i = 0; < while ( i < 10 ) < { < printf( "i = %d\n", i ); < ++i; < } < return 0; < } < %] < \begin{Solution} < %[ < #include < int main( void ) < { < int i; < for ( i = 0; i < 10; ++i ) < printf( "i = %d\n", i ); < < return 0; < } < %] < \end{Solution} < \end{exercise} < \begin{exercise} 3523c3559 < that is complete except that it is missing the body of the fucntion --- > that is complete except that it is missing the body of the function 3589c3625 < Write a function @SevenSegment()@in C that will simulate the seven --- > Write a function @SevenSegment()@ in C that will simulate the seven 3633c3669 < figure\vref{ex:sevSeg}, although I have used four columns here to save --- > figure\vref{fig:sevSegOutput}, although I have used four columns here to save 4337c4373 < the rest of the computer know that the address on the address lines is --- > the rest of the computer know that the address on the address bus is 4348,4349c4384 < \index{address decoding!partial|(} < \index{partial address decoding|(} --- > \index{address decoding!partial|(} \index{partial address decoding|(} 4357,4363c4392,4399 < means that each of the registers in the \PIT can be accessed not only at the < addresses shown in table~\vref{tab:registerAddr}, but also at $2^{15} < = 32767$ other addresses! The memory map in < figure~\vref{fig:memmap} is not strictly accurate. It shows the < \PIT registers mapped to addresses 800000\hex to 80003F\hex, only 64 < bytes. In fact, the \PIT is mapped to locations 800000\hex to < 9FFFFF\hex, occupying two megabytes of address space! --- > means that each of the registers in the \PIT can be accessed not only > at the addresses shown in table~\vref{tab:registerAddr}, but also at > $2^{15} - 1 = 32767$ other addresses! The memory map in > figure~\ref{fig:memmap} on page~\pageref{fig:memmap} is not strictly > accurate. It shows the \PIT registers mapped to addresses 800000\hex > to 80003F\hex, only 64 bytes. In fact, the \PIT is mapped to > locations 800000\hex to 9FFFFF\hex, occupying two megabytes of address > space! 4368,4369c4404,4405 < require a circuit with 19 inputs; here we need a simple circuit < circuit that only has four inputs. --- > require a circuit with 19 inputs; here we need a simple circuit that > only has four inputs. 4375c4411 < If there are three address lines, A$_{23\text{--}21}$, connected to --- > There are three address lines, A$_{23\text{--}21}$, connected to 4381,4383c4417,4419 < address lines A$_{4\text{--}1}$ connected directly to the \DUART, and < the lowest address to which the registers in the \DUART are mapped is < A00000\hex, then, as a result of partial address decoding, --- > address lines A$_{4\text{--}1}$ connected directly to the \DUART. > The lowest address to which the registers in the \DUART are mapped is > A00000\hex. Now, as a result of this partial address decoding, 4392c4428,4429 < \begin{enumerate} --- > \mbox{}\\ > \begin{enumerate}[(a)] 4395c4432 < when A$_21$ changes. So the lowest address, A00000\hex is active --- > when A$_{21}$ changes. So the lowest address, A00000\hex is active 4399,4402c4436,4439 < when A${23}$ = 1, A$_{22}$ = 1 and A$_{21}$ = 0, which (when all < other bits are zero) is C00000\hex. The address one less than < this is BFFFFF\hex. That is the highest address at which the < chip select of the \DUART will be activated. --- > when A${23}$ = 1, A$_{22}$ = 1 and A$_{21}$ = 0. The highest address > with A${23}$ = 1, A$_{22}$ = 0 and A$_{21}$ = 1 is BFFFFF\hex. > That is the highest address at which the chip select of the > \DUART will be activated. 4406c4443,4444 < decoder. Therefore there are $2^{16} = 65536$ different --- > decoder. These address lines are ``dont't cares'' to the > \DUART. Therefore there are $2^{16} = 65536$ different 4408c4446 < \end{enumerate} --- > \end{enumerate} 5227a5266,5309 > So how do we use the table? Let's see how we can evaluate a complex > expression by hand. We look up the precedence level of each operator > in the table; it helps to write it in pencil next to each operator. > The expressions with operators with the highest precedence are > evaluated first. Put parentheses around those expressions with the > highest precedence level first. If the operands are constants, you > can evaluate the expressions instead of adding parentheses. Supposing > an expression has operators next to each other with the same > precedence: what does the compiler do then? It depends on the > associativity of the operator. If the operators are left associative, > then they operators at the same precedence are evaluated starting with > the leftmost expression. If they are right associative, such as the > assignment operator, the expressions are evaluated from right to left, > starting with the one on the right. As an example, the program > example~\vref{prg:assignment} shows how assignment operators can be > used together.% > \index{assignment operator!multiple}% > \index{operator!assignment!multiple} > \begin{program} > %[ > %include > int main( void ) > { > int a, b, c; > /* The next line is equivalent to */ > /* @( a = ( b = ( c = 32000 ) ) );@ */ > a = b = c = 32000; /* Assign the value 32000 to @a@, @b@ and @c@. */ > printf( "%d %d %d\n", a, b, c ); > return 0; > } > %] > The output from this program is > \begin{verbatim} > 32000 32000 32000 > \end{verbatim} > \caption{Mutliple assignment.} > \label{prg:assignment} > \end{program} > > > > In the solution to exercise~{soln:precedenceExample} there is an > example showing how to use these rules to evaluate a complicated expression. > 5879c5961 < %@- --- > %@+ 6063c6145 < only option although the use of assembly language is declining. --- > only option, although the use of assembly language is declining. 6312c6394 < \texttt{error1.c: 1: Unknown preprocessing directive.} --- > error1.c: 1: Unknown preprocessing directive. 6434a6517,6556 > > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% > \subsection{The ``my computer looks sick!'' error message} > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% > > I wrote a nice little program, compiled it, and this is what I saw in > the \textsf{MS-DOS Prompt} window: > \begin{verbatim} > C:\MICROP>cc68k leds.C > ERROR: Invalid IEEE 695 Object Module: > object file = > C:\MICROP\leds.C > (01) 0000 23: unknown record header. > Record in hex is: > 23696E636C756465203C666C696768742E683E0D > 0A696E74206D61696E2820766F696420290D0A7B > 0D0A2020204C454453203D20307861613B0D0A20 > 202072657475726E20303B0D0A7D0D0A > > FATAL ERROR: (300) Bad IEEE Object Record Module: Part: Header Position: 0 > > WARNING: (311) Link Not Completed > > Errors: 1, Warnings: 1 > \end{verbatim} > Oh dear, this looks very serious! This error is so severe that it is > ``{\Large FATAL}''\e! What shall we do? > > In fact, this is a minor problem to solve; you need to type the > `\file{.C}' part of the filename in lower case, like this: `\file{.c}'. > The solution is simply to type > \begin{verbatim} > cc68k leds.c > \end{verbatim} > instead of > \begin{verbatim} > cc68k leds.C > \end{verbatim} > > 6463c6585 < how to assemble, download and run a program after you have designed --- > how to compile, download and run a program after you have designed 6467,6468c6589,6590 < \section{The general procedure for editing, assembling, downloading and running < an assembly language program} --- > \section{The general procedure for editing, compiling, downloading and running > a program} 6627,6628c6749,6751 < Note that the filename must be in \emph{lower case}, otherwise the < compile will not succeed. --- > Note that the `\texttt{.c}' at the end of the filename must be in > \emph{lower case}, otherwise the compile will not succeed---you will > get a very big and serious looking error message!