Assembler programming



8031/51 architecture (specifically the 89C2051)


Is ideal for assembler programming!




Programs must be small and efficient.




Compilers do exist for - Pascal, C and Modula 2.


and most development work will be done in a high-level language, if at all possible.


Concentrate on assembler level programming.


An assembler programmer needs to know details of the underlying hardware, as well as be familiar with the machine instruction set.

Assembler statements are ‘English’ versions of machine instructions.


Each assembler statement generates 1 machine code instruction. Whereas a C statement may well generate several machine instructions.



An assembler programmer is in complete control of the hardware.



Details that are generated automatically by C have to be done explicitly in assembler code.

(eg passing arguments to functions)



It is important to have a good knowledge of the hardware - the 2051 includes several peripheral devices (timers, output ports etc) as well as the CPU organisation


2051 assembler programming involves:








we will develop programs using a PC as the host environment. The following programs are available from the network:


JFE the gcc editor (but any text editor will do)


ASM51 a cross-compiler for 8031/8051 code


SIM51 a simulator for the 2051 controller

The 2051 architecture:


Programs are stored in a separate memory area to where data is stored.


(This is different to many processors, where code and data are freely intermixed)


2051 code must reside in the PROM (Programmable Read Only Memory).


Constant data is also stored in the PROM.




Variable data is stored in Internal RAM.





(The 8031/8051 architecture does have facilities to allow external program and data storage to be added to the controller chip - but these are not available in the 2051 variant)


Program code space is 2KBytes


The Program Counter is a 16bit register

(only 11 bits are used!)



Variable data is stored in Internal RAM


General purpose data registers are part of RAM


Machine instructions that access registers are 1 byte shorter than an equivalent instruction that accesses RAM directly.


There are 4 banks of 8 registers R0 - R7.


Only one bank can be active at any time.


Bank 0 is located at 00h - 07h


Bank 1 is the next 8 bytes 08h - 0fh


Bank 2 is at 10h - 17h


Bank 3 is at 18h - 1fh


Register banks are used when we have more than one activity being performed at a time.


It is a simple matter to switch to the relevant set of registers, when we need to swap from one task to the other.

Other registers, eg the accumulator and stack pointer, are stored in an extension to RAM


Locations 080h - 0ffh are called SFR space.


Special Function Registers


Most of this RAM is not there! Only locations for specific registers are implemented in hardware.


SFR space varies considerably for different versions of the 8031/8051 controllers but there is a set of registers common to them all.


The 2051 implements this base set plus one other.


SFR space can be addressed either by actual locations or by standard names


The Program Status Word is either


PSW or 0d0h

Most SFRs are concerned with specific i/o functions, we will deal with them later



For the time being we need to know about the following:


081h SP the stack pointer

0d0h PSW the program status word

0e0h ACC the accumulator

0f0h B the B register


SP is used for subroutine calls and storing items on a stack


PSW contains flags to reflect the outcome of the previous instruction:


Carry flag, Auxiliary Carry flag, OVerflow flag


and also the current Register Bank pointer


ACC is used in most arithmetic and logic instructions - but is not normally accessed directly through SFR space


B is used in multiplication and division


159.233 Assembler 1 - 8