ACCESSING CODE ROM SPACE IN 8051 C

SECTION 7.5: ACCESSING CODE ROM SPACE IN 8051 C
Using the code (program) space for predefined data is the widely used option in the 8051, as we saw in Chapter 5. In that chapter we saw how to use the Assembly language instruction MOVC to access the data stored in the 8051 code space. In this chapter, we explore the same concept for 8051 C.
RAM data space v. code data space
In the 8051 we have three spaces in which to store data. They are as follows:
  1. The 128 bytes of RAM space with address range 00 – 7FH. (In the 8052, it is
    256 bytes.) We can read (from) or write (into) this RAM space directly or indi
    rectly using the RO and Rl registers as we saw in Chapter 5.

The 64K bytes of code (program) space with addresses of 0000 – FFFFH. This
64K bytes of on-chip ROM space is used for storing programs (opcodes) and
therefore is directly under the control of the program counter (PC). We can use
the “MOVC A, @A+DPTR” Assembly language instruction to access it for
data (see Chapter 5). There are two problems with using this code space for
data. First, since it is ROM memory, we can burn our predefined data and
tables into it. But we cannot write into it during the execution of the program.
The second problem is that the more of this code space we use for data, the less
is left for our program code. For example, if we have an 8051 chip such as
DS89C420 with only 16K bytes of on-chip ROM, and we use 4K bytes of it to
store some look-up table, only 12K bytes is left for the code program. For
some applications this can be a problem. For this reason Intel created another
memory space called external memory especially for data. This is discussed
next very briefly and we postpone the full discussion to Chapter 14.



3. The 64K bytes of external memory, which can be used for both RAM and ROM. This 64K bytes is called external since we must use the MOVX Assembly language instruction to access it. At the time the 8051 was designed, the cost of on-chip ROM was very high; therefore, Intel used all the on-chip ROM for code but allowed connection to external RAM and ROM. In other words, we have a total of 128K bytes of memory space since the off-chip or external memory space of 64K bytes plus the 64K bytes of on-chip space provides you a total of 128K bytes of memory space. We will discuss the external memory expansion and how to access it for both Assembly and C in Chapter 14.
Next, we discuss on-chip RAM and ROM space usage by the 8051 C compiler. We have used the Proview32 C compiler to verify the concepts discussed next. Use the compiler of your choice to verify these concepts.
RAM data space usage by the 8051 C compiler
In Assembly language programming, as shown in Chapters 2 and 5, the 128 bytes of RAM space is used mainly by register banks and the stack. Whatever remains is used for scratch pad RAM. The 8051 C compiler first allocates the first 8 bytes of the RAM to bank 0 and then some RAM to the stack. Then it starts to allocate the rest to the variables declared by the C program. While in Assembly the default starting address for the stack is 08, the C compiler moves the stack’s starting address to somewhere in the range of 50 – 7FH. This allows us to allocate contiguous RAM locations to array elements.
In cases where the program has individual variables in addition to array elements, the 8051 C compiler allocates RAM locations in the following order:
  1. Bank 0 addresses 0-7
  2. Individual variables addresses 08 and beyond
  3. Array elements addresses right after variables
  4. Stack addresses right after array elements
  5. You can verify the above order by running Example 7-30 on your 8051 C simulator and examining the contents of the data RAM space. Remember that array elements need contiguous RAM locations and that limits the size of the array due to the fact that we have only 128 bytes of RAM for everything. In the case of Example 7-31 the array elements are limited to around 100. Run Example 7-31 on your 8051 C simulator and examine the RAM space allocation. Keep changing the size of the array and monitor the RAM space to see what happens.


    Figure 7-1. RAM Allocation in the 8051




    Example 7-30
    Compile and single-step the following program on your 8051 simulator. Examine the contents of the 128-byte RAM space to locate the ASCII values.

    Run the above program on your 8051 simulator and examine the RAM space to locate values 41H, 42H, 43H, 44H, etc., the hex values for ASCII letters ‘A’, ‘B’, ‘C’, and so on.
    Example 7-31
    Write, compile, and single-step the following program on your 8051 simulator. Examine the contents of the code space to locate the values.

    Run the above program on your 8051 simulator and examine the data RAM space to locate values FFH, FEH, FDH, and so on in RAM.
    The 8052 RAM data space
    Intel added some new features to the 8051 microcontroller and called it the 8052. One of the new features was an extra 128 bytes of RAM space. That means that the 8052 has 256 bytes of RAM space instead of 128 bytes. Remember that the 8052 is code-compatible with the 8051. This means that any program written for the 8051 will run on the 8052, but not the other way around since some fea-


    tures of the 8052 do not exist in the 8051. The extra 128 bytes of RAM helps the 8051/52 C compiler to manage its registers and resources much more effectively. Since the vast majority of the new versions of the 8051 such as DS89C4xO are really based on 8052 architecture, you should compile your C programs for the 8052 microcontroller. We do that by (1) using the reg52.h header file, and (2) choosing the 8052 option when compiling the program.
    Accessing code data space in 8051 C
    In all our 8051 C examples so far, byte-size variables were stored in the 128 bytes of RAM. To make the C compiler use the code space instead of the RAM space, we need to put the keyword code in front of the variable declaration. The following are some examples:

    Example 7-32 shows how to use code space for data in 8051 C.
    Example 7-32
    Compile and single-step the following program on your 8051 simulator. Examine the contents of the code space to locate the ASCII values.
    Solution:

    Run the above program on your 8051 simulator and examine the code space to locate values 41H, 42H, 43H, 44H, etc., the hex values for ASCII characters of’A', ‘B’, ‘C’, and so on.
    Compiler variations
    Look at Example 7-33. It shows three different versions of a program that sends me string “HELLO” to the PI port. Compile each program with the 8051 C compiler of your choice and compare the hex file size. Then compile each program on a different 8051 C compiler, and examine the hex file size to see the effectiveness of your C compiler. See www.MicroDigitalEd.com for 8051 C compilers.


    Example 7-33
    Compare and contrast the following programs and discuss the advantages and disadvantages of each one.

    Solution:
    All the programs send out “HELLO” to PI, one character at a time, but they do it in different ways. The first one is short and simple, but the individual characters are embedded into the program. If we change the characters, the whole program changes. It also mixes the code and data together. The second one uses the RAM data space to store array elements, therefore the size of the array is limited. The third one uses a separate area of the code space for data. This allows the size of the array to be as long as you want if you have the on-chip ROM. However, the more code space you use for data, the less space is left for your program code. Both programs (b) and (c) are easily upgradable if we want to change the string itself or make it longer. That is not the case for program (a).


    See the following Web sites for 8051 C compilers:
    www.MicroDigitalEd.com
    www.8052.com

Next post:

Previous post: