Important Points in 8051 Programming

8051 Data Memory Architecture

8051 is an microcontroller released by Intel in 1980. After its release several semiconductor vendors started offering 8051-based microcontrollers, and it is still widely used with a variety of improvements/enhancements made to it. While it’s designed to improve efficiency considerably when programming in assembler, many instances have been observed where it’s challenging to program in standard C language in terms of memory capacity, without taking special consideration. On the other hand, IAR Embedded Workbench for 8051 supports a wide range of devices for 8051 core, and can generate in the standard C language code comparable to what is written in the assembler.

In this article, we will discuss some of 8051’s complex memory architecture in order to utilize code efficiency offered by IAR Embedded Workbench, making the most out of its features.

Internal data memory space

Internal data memory space is a space whose address is 8-bit addressable using 0-0xFF, as shown in the drawing below, consisting of two parts: one is memory, the other I/O (sfr – Special Function Register.)


Internal data memory space is further divided into four different parts: dataidatabdata/bit, and sfr, some of which are overlapping.

data (0x00-0x7F) 
An area that can be accessed via direct addressing of the assembler instructions.

idata (0x00-0xFF) 
An area accessible via register indirect addressing of the assembler instructions.

bdata/bit (0x20-0x2F) 
An area that can be accessed bit by bit. The size of the area is 16 bytes and can store 128 bool type variables in C language. bdata can access bitwise data in the unit of 8 bit. This enables it to set/clear multiple bits with one instruction at once.

sfr (0x80-0xFF)
I/O area. It overlaps with the above memory areas; which one of them to be accessed depends on the assembler instruction you use. However, you do not need to be aware which one to access as long as you’re writing C source code for IAR Embedded Workbench.

External data memory space

External data memory space is a memory that is essentially connected to the outside of a microcontroller, but some of the recent microcontrollers have it inside. Areas located higher than address 0x10000 are called Extended memory. However, not all 8051 devices have Extended memory.


As with Internal data memory space, it has three areas, pdataixdata, and xdata_ROM overlapping but we’ll skip the details here. Worth mentioning is however that ixdata, if available, and pdata access external memory more efficiently. Because external data memory space can only be accessed via the assembler instruction, movx, the procedure that accesses certain variables is lengthy; placing frequently-accessed variables in this area will generate inefficient code.

Therefore, the overall efficiency improves when you place scratch memory and index for an array in the data area, and locate the array itself in the external data memory space. Since the data area is extremely small, you have no choice but to use idata for large programs. If idata is still not sufficient, you can choose the areas pdataixdataixdata, and far/huge as the program size increases.

How to write code in C language

When compiling, one of the five memory models are used. The memory model specifies what part of memory to be used for data and data pointers. If you specify one of the models in the project option according the system being developed, it’ll save you the trouble of designating the location to store the memory for the variable.


Before declaring variables in C language, specify the area to place them using a memory modifier prefixed with “__” as follows:

__idata int i, j; 
int __idata k, l;

Either of the above declarations is valid. The names of the data areas mentioned above, prefixed with two underscores, are the memory modifiers that correspond to the memory spaces described earlier, to be written either right or left of the data type that declares the memory.

data area __data 
idata area __idata
bit area __bit


Even though 8051 is an architecture that has been on the market for almost thirty years, it is still widely adopted in newly developed microcontrollers as well as SoCs. IAR Embedded Workbench is constantly evolving as well. With IAR Embedded Workbench, in addition to the data memory model, other options such as code memory model and calling conventions can be specified, allowing for full utilization of 8051’s capabilities.

© IAR Systems 1995-2016 - All rights reserved.