8051 Instruction Set
Conversion to machine language
Assembly level program to hex file flow
8051 Microcontroller Instruction Set
• Writing a Program for any Microcontroller consists of
giving commands to the Microcontroller in a particular
order in which they must be executed in order to
perform a specific task. The commands to the
Microcontroller are known as a Microcontroller’s
Instruction Set.
• As the 8051 family of Microcontrollers are 8-bit
processors, the 8051 Microcontroller Instruction Set is
optimized for 8-bit control applications. As a typical 8-
bit processor, the 8051 Microcontroller instructions
have 8-bit Opcodes. As a result, the 8051
Microcontroller instruction set can have up to 28 = 256
Instructions.
8051 Instruction Groups and Instructions in
each group.
Types of Instructions in 8051
Microcontroller Instruction Set
• An 8051 Instruction consists of an Opcode (short of Operation –
Code) followed by Operand(s) of size Zero Byte, One Byte or Two
Bytes.
• The Op-Code part of the instruction contains the Mnemonic, which
specifies the type of operation to be performed. All Mnemonics or
the Opcode part of the instruction are of One Byte size.
• Coming to the Operand part of the instruction, it defines the data
being processed by the instructions. The operand can be any of the
following:
– No Operand
– Data value
– I/O Port
– Memory Location
– CPU register
8051 Addressing Modes
• An Addressing Mode is a way to locate a target
Data, which is also called as Operand. The 8051
Family of Microcontrollers allows five types of
Addressing Modes for addressing the Operands.
They are:
• Immediate Addressing
• Register Addressing
• Direct Addressing
• Register – Indirect Addressing
• Indexed Addressing
Immediate Addressing
• In Immediate Addressing mode, the operand, which follows
the Opcode, is a constant data of either 8 or 16 bits. The
name Immediate Addressing came from the fact that the
constant data to be stored in the memory immediately follows
the Opcode.
• The constant value to be stored is specified in the instruction
itself rather than taking from a register. The destination
register to which the constant data must be copied should be
the same size as the operand mentioned in the instruction.
• Example: MOV A, #030H
• Here, the Accumulator is loaded with 30 (hexadecimal). The #
in the operand indicates that it is a data and not the address
of a Register.
• Immediate Addressing is very fast as the data to be loaded is
given in the instruction itself.
Register Addressing
• In the 8051 Microcontroller Memory Organization
Tutorial, we have seen the organization of RAM and
four banks of Working Registers with eight Registers in
each bank.
• In Register Addressing mode, one of the eight registers
(R0 – R7) is specified as Operand in the Instruction.
• It is important to select the appropriate Bank with the
help of PSW Register. Let us see a example of Register
Addressing assuming that Bank0 is selected.
• Example: MOV A, R5
• Here, the 8-bit content of the Register R5 of Bank0 is
moved to the Accumulator.
Direct Addressing
• In Direct Addressing Mode, the address of the
data is specified as the Operand in the
instruction. Using Direct Addressing Mode, we
can access any register or on-chip variable.
This includes general purpose RAM, SFRs, I/O
Ports, Control registers.
• Example: MOV A, 47H
• Here, the data in the RAM location 47H is
moved to the Accumulator.
Register Indirect Addressing
• In the Indirect Addressing Mode or Register Indirect
Addressing Mode, the address of the Operand is specified
as the content of a Register. This will be clearer with an
example.
• Example: MOV A, @R1
• The @ symbol indicates that the addressing mode is
indirect. If the contents of R1 is 56H, for example, then the
operand is in the internal RAM location 56H. If the contents
of the RAM location 56H is 24H, then 24H is moved into
accumulator.
• Only R0 and R1 are allowed in Indirect Addressing Mode.
These register in the indirect addressing mode are called as
Pointer registers.
Indexed Addressing Mode
• With Indexed Addressing Mode, the effective address
of the Operand is the sum of a base register and an
offset register. The Base Register can be either Data
Pointer (DPTR) or Program Counter (PC) while the
Offset register is the Accumulator (A).
• In Indexed Addressing Mode, only MOVC and JMP
instructions can be used. Indexed Addressing Mode is
useful when retrieving data from look-up tables.
• Example: MOVC A, @A+DPTR
• Here, the address for the operand is the sum of
contents of DPTR and Accumulator.
• NOTE: Some authors and textbooks add few other
Addressing Modes like Absolute Addressing Mode,
Relative Addressing Mode and Long Addressing Mode.
Data Transfer Instructions
Arithmetic Instructions
Logical Instructions
Program Branching Instructions
Boolean or Bit Manipulation Instructions
Write a program to transfer 10 bytes of data from code
memory to port 1 using assembly language coding.
Program: Specific Byte Data Search
Write programs for
• Data Transfer
– 10 bytes from 0x30-0x39 to 0x40-0x49
– 10 bytes from 0x30-0x39 to 0x35-ox3E
– Exchange 10 bytes from 0x30-0x39 to 0x40-0x49
• Convert 10 bytes of data to its 2’s Complement
form.
• Packed BCD to Unpacked BCD
• Find the largest number in 10 bytes of data
• Arrange the number in ascending order
C language
• C language was developed by Dennis Ritchie in 1969.
• It is a collection of one or more functions, and every
function is a collection of statements performing a
specific task.
• C language is a middle-level language as it supports
high-level applications and low-level applications.
Embedded C from C
• C language is a software designed with different keywords, data
types, variables, constants, etc.
• Embedded C is a generic term given to a programming language
written in C, which is associated with a particular hardware
architecture.
• Embedded C is an extension to the C language with some additional
header files. These header files may change from controller to
controller.
• The microcontroller 8051 #include<reg51.h> is used.
• The embedded system designers must know about the hardware
architecture to write programs.
• Programs/codes play prominent role in
monitoring and controlling external devices.
• They directly operate and use the internal
architecture of the microcontroller, such as
interrupt handling, timers, serial
communication and other available features.
Example: printf requires serial
configuration to see output
The basic additional features of the embedded software is
data types
• The data type refers to an extensive system for declaring
variables of different types like integer, character, float, etc.
• The embedded C software uses following data types that
are used to store data in the memory.
– The ‘char’ is used to store any single character;
– ‘int’ is used to store integer value, and
– ‘float’ is used to store any precision floating point value.
• The size and range of different data types on a 32-bit machine
is given in the following table. The size and range may vary on
machines with different word sizes.
• There are certain words that are reserved for doing specific
tasks. These words are known as keywords.
• They are standard and predefined in the Embedded C.
Keywords are always written in lowercase.
• These keywords must be defined before writing the main
program.
Keywords
Example Automatic Variable
Example of arithmetic operations
Example Boolean
Super Loop i.e. “till infinity execution”
FOR compared with While
Nested Loop
Embedded C programming
• Now we visualize the same programs we
wrote using assembly language but using
Embedded C language.
Write a program to transfer 10 bytes of data from code
memory to port 1 using assembly language coding.
– Similarly in Embedded C
• ASCII character transferred byte by byte to port 1 (code1.c).
• Requirement data (array) present as part of program
• Fetching the data from loop byte by byte
• Assigning the defined data to an output port for visualization
• Visualised data as output is in hexadeciamal but as char is
being assigned so it is an ASCII value being represented in
hexa.
#include<absacc.h>
Data transfer using - absacc.h
• DBYTE – internal data ram 1 byte array
indexed.
• CBYTE – codememory 1 byte array indexed
• XBYTE – external memory 1 byte array indexed
• DWORD – internal data ram 2 byte array
indexed.
• CWORD – codememory 2 byte array indexed
• XWORD – external memory 2 byte array
indexed
10 bytes internal Ram to internal ram – 0x30h to 0x39h
to 0x40h to 49h using “DBYTE”
Read code memory and transfer to
internal RAM
Transfer internal RAM array to External
RAM
Program: Specific Byte Data Search
Ascending Order
Delay 1 ms = requires 8 machine cycles
repeated in loop
Embedded C