Answers to chapter-end questions

(Note that any questions requiring long answers or long program development are not included)

Chapter 1

1        The five languages used with the 8051 are assembly, PL/M, BASIC, C and Forth.(p2)

2        While some might argue for newer processors, the 8051 has been around for decades and continues to see wide use in many 8-bit applications. It also has been upgraded both in speed and in peripheral options while remaining very cost competitive. The main disadvantage is the unusual architecture which provides only a small amount of directly-addressable memory.(p1,2)

3        Embedded controllers are found in microwave ovens, cell phones, VCRs, stoves, washing machines, and in many sub-systems of automobiles.(p1)

Chapter 2

1        You will supply something the customer does not want.(p7) You will supply something unfriendly to the user.(p8) You will become a victim of creeping features.(p8) You will develop something that is difficult to test or maintain.(p9)

2        Working in a group on planning allows for a broader point of view and keeps you from designing based only on your own viewpoint.(p9)

3        A new controller design could be in order if the previous one is unreliable or can be reduced in cost. There might come a time where a new perspective with improved features can be justified.

4        Tasks allow the partitioning of software into discrete pieces with all related functions together. Tasks also allow you to focus on higher-level aspects before getting into details.(p6,7)

5        Ground connections, test points, and room for board extenders.(p9)

Chapter 3

1        Bit and Unsigned byte (unsigned char).(p17,18) C types in addition include signed char, signed & unsigned int, long , float, double.(p18,19)

2        Data, code and xdata.(p20,21) On-chip RAM is accessible by most instructions while off-chip RAM is reached only with tow specific instructions. On-chip has an 8-bit address while off-chip addresses are 16 bits long.

3        In C programs the nesting can be shown with different levels of indentation. In addition the curly braces can be on separate lines or on the same lines as the rest of the C code. Comment lines can also mark off pieces of code to improve readability.(p16)

4        A type cast forces a variable to be converted to a specific type—an integer when it might have been a byte, for example.(p25)

5        An assignment operator is a shorthand way to write code that performs a single operation on a variable and then replaces the previous value of the variable with the new result.(p25)

6        changed=(old^new)&new;(p21,22)

7        Floating point math increases the size of variables, requires much more complicated libraries for operations, and slows down the execution of the program.(p18,19)

8        Parentheses, square braces, and exponentiation are highest.(fig 3.24)
a=(b+c)/d; a=b+(c/d);

Chapter 4

  1. Structured languages (p29) add rules to avoid program flow that enters a block in different places. Viewing it another way, structured languages keep gotos from messing up the stack depth by entering or leaving a function without a push and corresponding pop (p35). I have personally seen use of assembly that was unstructured and as a result also very difficult to follow and understand.
  2.  A goto can add to understandability in some branching situations or when an unexpected termination of a for loop is needed.
  3. A goto can only be used to a labeled line within the same function.
  4. Omitting the typical break in a switch case expression causes the program flow from one case to flow into the code of the next case (however they happen to be arranged vertically—nothing to do with the actual sequence of index values). (p31,32)
  5. Yes. (p31)
  6. A while tests the condition before executing the loop’s code but a do…while always executes the loop code once and then tests to determine whether to go up and do it a second time. (p32)
  7. You can force the index out of range:
    for (I=0;I<20;I++){
    (loop stuff here)
    if (emergency) I=20;
  8. A forever loop (the core of all embedded software) never terminates—the conditions to “go on” never happen (and there is nothing else to go on to anyway). Commonly this is done with while (1){ or for(;;){. (p35)
  9. Software generated delays depend on the execution times of the instructions within the delay loop, which can get complicated to tailor to a given situation. In addition they tie up the processor so other activities cannot happen during the delay. If interrupts are allowed, they would stretch the current software delay. (p36)

Chapter 5

  1. The 60 line rule says that a block of code or a function should all fit on a single page of printout. When it is violated the program becomes difficult to follow—the pieces become too large to comprehend easily and should be broken into several smaller functions. (p45)
  2. A driver is usually a single function used to interface between to rest of the software and a specific piece of hardware. (p42)
  3. I tend to use “ends-in” working from the hardware details on one side and the overall problem on the other side. (p6)
  4. In-line repeated code is best when it involves only a line or two of repeated code. (p46)
  5. To get the equivalent of returning more than one parameter you can either work on the global variables directly from within the function or pass pointers to the variables that are to get the multiple results. (p39)
  6. Scope rules make it possible to have two variables with the same name (and different non-overlapping scope), but acting on the one local to the function will not alter the global one. This is a common mistake for new C programmers.

Chapter 6

  1. Takes 20 bytes (2 per integer). Stored in byte pairs (most significant first—p19). Array[5] is the 6th element so it is at 202a and 202b.
  2. Larger than 2-dimension arrays are seldom used because they rapidly exceed the small available memory space.
  3. struct coord{uchar x, uchar y);
    struct coord points[20];
  4. struct coord{uchar x, uchar y);
    struct coord square1[]={{0,0},{4,0},{4,4},{0,4},{0xff,0xff}};//ff is end of sequence
    struct coord square2[]={{2,2},{7,2},{7,7},{2,7}},{0xff,0xff}};
    void plot(struct coord *pts[]);//function prototype
    plot(square1);//function call (NOTE: I haven’t checked this on a compiler!)
  5. xdata, data, idata, pdata, code. Idata is the upper 128 bytes of on-chip RAM which must be indirect to avoid reaching the ports and registers instead. Pdata is xdata with only the lower byte involved.
  6. Different memory spaces either need specific memory pointers used only for that space or else they must have “universal” pointers which have an extra byte designating which space is used with that specific instance. The latter takes an extra byte (or two where data space is involved) for each pointer and also requires software to read the third byte and branch to a piece of machine code to retrieve the data from the designated memory space.
  7. A generic pointer is another name for a universal pointer (not mentioned in the book!)
  8. The printf() function in the 8051 must accept messages from code space and xdata space as well as data space. In addition, it can be required to handle a mix of constants and data values.
  9. Pointers to arrays are single values that can have the index value added to the pointer value to reach a specific element: uchar *PTA[];
    Arrays of pointers hold multiple pointer values at specific addresses: *uchar AOP[6];
  10. The difference between an array and a pointer is in the way the location can be offset. With an array, the address pointer is moved by the index value (times the number of bytes per element if other than single bytes). With a pointer, the value can be moved anywhere by altering the pointer value (but be careful because the ++ and — functions may move the pointer by 2 or more if the pointer is to a multi-byte type of variable).
  11. struct {uchar x, uint y}example1;
    example1.y=0x234;//straightforward, non-based
    struct {uchar x, uint y}*example2;
    example2=&example1;//have to set the pointer first
    example2->y=0x234;//use arrow instead of period for based structure
    (NOTE: I am rusty enough I should check this with a compiler when it is handy)
  12. Unions make it possible to access an area of memory in at least two different ways such as using space for integers or accessing the individual bytes of the integers. You do have to be careful you understand the high-low storage sequence used by the particular compiler and hardware.

Chapter 7

  1. A simulator uses software in the development computer (a PC?) to act like the instruction processing done in the target processor. An emulator actually runs the instructions on the actual processor but adds ways to start and stop the running and observe registers and memory (p65). The SiLabs target is a full emulator.
  2. IDE stands for Integrated Development Environment (p57)
  3. Write/edit code; compile or assemble and link; run and debug (p57-8).
  4. A breakpoint is a place in code (set during debugging) where program flow will stop so you can examine intermediate results (p65).
  5. The watch window holds the current values of variables you need to track as the program runs. The values are only updated at breakpoints or after single steps—not during uninterrupted running. (p64-5)
  6. Clicking the stop icon will cause the flow to halt at whatever is happening at that instant.
  7. Step-to-cursor halts the flow before executing the first assembly instruction of the line of C code. If the location is not reached then the flow continues endlessly (like a normal GO).
  8. The single-step instruction steps through a single machine (assembly) instruction so it may require many steps to get through all the instructions that result from a single line of C code. (p65)
  9. Multi-stepping keeps on single stepping for a large number of steps, but it updates the register and watch window values after each step. It is much slower than true running, but that makes it possible for a human to actually track the program flow (p65).

Chapter 8

  1. Modular programming allows you to write small, easy-to-understand pieces of code that can be tested independently. Problems can be isolated more easily. Modules can be debugged and then re-used or even put in a library (p67).
  2. A variable defined at the top of a C module is public to all modules. In assembly in it known only in that module (p72).
  3. In C a function is automatically public when written, but it must be prototyped in the calling module (p72).
  4. Mixing languages makes sense only where small assembly (hardware driver) modules allow tighter control of details. A few operations like stack altering can only be done in assembly. (p72).