Unit III
Unit III
Third Unit-8051:
Programmer’s
Model
We will cover these skills:
Introduction to Assembly
programming
Compilers
Assemblers,
Instruction classification
Instruction set
Addressing Modes: Immediate,
register, direct, indirect and
relative
assembler directives (ORG, END),
features with examples.
Introduction to 8051
programming in C.
TEACH A COURSE 2
Introduction to Assembly programming
The assembly language is a low-level programming language used to write program code in terms of
mnemonics. Even though there are many high-level languages that are currently in demand, assembly
programming language is popularly used in many applications. It can be used for direct hardware
manipulations. It is also used to write the 8051 programming code efficiently with less number of clock
cycles by consuming less memory compared to the other high-level languages.
8051 Programming in Assembly Language: The assembly
language is a fully hardware related programming language.
The embedded designers must have sufficient knowledge on
hardware of particular processor or controllers before writing
the program. The assembly language is developed by
mnemonics; therefore, users cannot understand it easily
to modify the program.
TEACH A COURSE 3
Introduction to Assembly programming
TEACH A COURSE 4
Introduction to Assembly programming
A Register is the main part in the processors and microcontrollers which is contained in the memory
that provides a faster way of collecting and storing the data. The 8051 assembly language programming
is based on the memory registers. If we want to manipulate data to a processor or controller by
performing subtraction, addition, etc., we cannot do that directly in the memory, but it needs registers
to process and to store the data. Microcontrollers contain several types of registers that can be classified
according to their instructions or content that operate in them.
8051 Microcontroller Programs in Assembly Language: The assembly language is made up of elements
which all are used to write the program in sequential manner. Follow the given rules to write
programming in assembly language.
TEACH A COURSE 5
Introduction to Assembly programming
TEACH A COURSE 6
Introduction to Assembly programming
Op-code: The op-code is a single instruction that can be executed by the CPU. Here the op-code is a
MOV instruction.
Operands: The operands are a single piece of data that can be operated by the op-code. Example,
multiplication operation is performed by the operands that are multiplied by the operand.
Syntax: MUL a,b;
The Elements of an Assembly Language Programming:
Assembler Directives
Instruction Set
Addressing Modes
TEACH A COURSE 7
Compilers
A Compiler is primarily used for programs that translate source code from a high-level programming
language to a machine-level language to create an executable program. A compiler will consider the
entire program as a whole code and then translate it. The main job of the compiler is that it check all
kinds of limits, ranges, errors, etc. Before the compiler can successfully execute the code, the errors
must be removed from the source code. Examples of compiled languages are C, C++, Java, C#, etc.
Key Features of a Compiler:
It translates the whole source code at a time without
omitting any part of it.
Only reports on errors when the translated content is released.
Produces an efficient code for execution by a machine.
Transforms code intelligible by people into a form that can be executed on any other specific hardware
platform.
TEACH A COURSE 8
Assemblers
The Assembler takes as input the assembly code and translates it into relocatable machine code. The
assembler checks each instruction for its correctness and generates a diagnostic message, if
Key Features of an Assembler:
Controls the manipulation of operands as themed by
assembly language instructions.
With a direct correspondence of assembly instructions
with that of machine instructions it works.
Runs at a higher speed than a compiler since it works with the basic code.
Critic for software which interfacing with hardware such as embedded systems, device drivers and
embedded software.
TEACH A COURSE 9
Difference Between Compiler and Assembler
Compiler Assembler
Compiler converts the source code written by Assembler converts the assembly code into
the programmer to a machine level language. the machine code.
Compiler input source code. Assembler input assembly language code.
It converts the whole code into machine But the Assembler can’t do this at once.
language at a time.
It takes less execution time compared to an
assembler. It takes more time than a compiler.
It shows the whole program error after the It detects errors in the first phase, fixes them,
whole program is scanned. and then the second phase is start to execute.
TEACH A COURSE 10
Difference Between Compiler and Assembler
Compiler Assembler
A Compiler is more intelligent than an But, an Assembler is less intelligent than a
Assembler.
Compiler.
The compilation phases are lexical
analyzer, syntax analyzer, semantic analyzer, Assembler makes two phases over the given
intermediate code generated, a code input, first phase and the second phase.
optimizer, code generator, and error handler.
The output of compiler is a mnemonic version of
machine code. The output of assembler is binary code.
C, C++, Java, and C# are examples of compiled GAS, GNU is an example of an assembler.
languages.
TEACH A COURSE 11
Instruction classification
A microcontroller provides user with an instruction set. It is a set of instructions that can be run on that
particular microcontroller. Usually, since the microcontroller is used for some specific task., it has a
smaller and simpler instruction set as compared to the processor.
Whatever be the number of instructions in an instruction set, one can classify instructions in different
ways as follows:
Classification based on instructions function.
Classification based on instruction size.
Classification based on size of operand on which the instruction executes.
TEACH A COURSE 12
Instruction classification
Classification based on instructions function: Every instruction executes in a different way. Some
instructions perform arithmetic calculations. Some perform data transfer and so on. Depending on the
function, an instruction can be classified into the following types:
Data transfer instructions
Arithmetic instructions
Logical instructions
Bit manipulation instructions
Program control instructions
Interrupt related instructions
Machine control instructions
TEACH A COURSE 13
Instruction classification
Classification based on instruction size: Depending on how much memory space an instructions uses
for its storage, instructions can be classified into:
One byte instruction
Two byte instruction
Three byte instruction
In a one byte instruction, the instruction and the operands can be stored in a single byte of memory
space. Examples like MOV A, B, DIV AB, CPL A are one byte instructions.
In a two byte instruction, the instruction is followed by an immediate 8 bit address/data. To store this
instruction, we will require 2 bytes –one for opcode and other for operand. For Ex: MOV R0, #7.
A three byte instruction uses 3 memory locations for its storage. The opcode in such instruction is
followed by 16 bit data. One byte of memory stores the opcode while the 16 bit operand is stored as 2,8
bit numbers in two consecutive memory locations following the opcode. For Ex: MOV DPTR, #2530H
TEACH A COURSE 14
Instruction classification
Classification based on size of operand on which the instruction executes: Therefore, on the basis of
size of an operand, an instruction can be bit or a byte addressable instructions. Bit addressable
instructions act on a single bit data while a byte addressable instruction operates on a byte. For Ex: CPL
P1.3 is a bit addressable instruction and CPL. A is a byte addressable instruction.
In a bit addressable instruction, the particular bit can be addressed as Reg. X where register is the register
name we want to work and X can change from 0 to 7 indicating that specific bit. For Ex: A.3 means third
bit of Register A.
IE.7 means the 7th bit of the Interrupt Enable register IE.
TEACH A COURSE 15
Instruction set
TEACH A COURSE 16
Instruction set
INC Source: Contents of source is incremented by 1.
INC A A A+1
INC Rn R0 R0 + 1
INC address of operand operand operand + 1
INC @ Ri Ri is indirect address of operand
DEC Source: Decrements the contents of source by 1.
INC DPTR: DPTR – Data Pointer register. Contents of DPTR is increased by 1.
MUL AB: A – 1st Operand, B – 2nd operand.
The new value of A will be the product of operand 1 and operand 2, i.e. A X B.
A AXB
This will be the case if result is 8 bit. If result is 16 bit then B contains higher 8 bits of result and A contains the lower 8 bits of
result.
TEACH A COURSE 17
Instruction set
TEACH A COURSE 18
Instruction set
Logical Instructions:
ANL Destination, Source: The 2 operands are stored in destination and source. Logical ‘AND’ is carried out between them and the
result is stored in destination. Destination is always accumulator A., while source can be immediate data, register, direct or indirect
address of the data.
A A ‘AND’ source
The AND ing is carried out bit wise.
B = 52H
A = 35H
AND A, B
B= 01010010 b
A= 00110101 b
A ‘AND’ B = 0001,0000 b ∴A 10H [ ‘AND’ operation is carried out bitwise.]
TEACH A COURSE 19
Instruction set
ORL Dest, Source: Here, every bit of source is logically ORed with corresponding bits of destination
and the result is stored in destination.
A = 52H, B = 35H
ORL A, B
A= 0101 0010 b
B= 0011 0101 b
A ‘OR’ B 0111 0111 b ∴A 77H Destination is always A.
XRL Dest, Source: Logically XORs every bit of source with destination and stores the result in
destination. Destination is always A.
TEACH A COURSE 20
Instruction set
A = 52H, B = 35H
XRL A, B
A= 0101 0010 b
B= 0011 0101 b
XRL A, B 0110 0111 b A 67H
CLR A: Contents of A is cleared to 0.
CPL A: Complements every bit of A.
If A = 50H
then CPL A gives A = 0101 000 b
Ā = 1010 1111 b ∴ A 0AFH
RL A: Contents of A are rotated left by 1 bit. The MSB coming out will go back as LSB. At the same time MSB is also copied into carry.
Suppose A = 50H
A = 0101 0000H and CY =1
RL A will give
∴ A = 0A0H, CY = 0.
TEACH A COURSE 21
Instruction set
RLC A: Rotate the contents of A by 1 bit through carry. Here MSB is copied into CY and CY bit goes back as the LSB.
Suppose CY = 1, A = 50H
RLC A gives
∴ A = 0A1H, CY = 0
RR A: Rotate right contents of A. 1 bit right. Here bit coming out of LSB is copied CY as well as MSB.
CY = 1, A = 50H then RR A gives
∴ CY = 0, A = 28H
RRC A: Rotate contents of A, 1 bit right through carry. Here the bit coming out of LSB goes to carry and carry goes to back
to MSB. A = 50H CY = 1
RRC A gives
∴ A = 0A8H, CY = 0.
TEACH A COURSE 22
Instruction set
SWAP A: Here the nibbles of A are swapped. Lower 4 bits now become the upper 4 bits of A and upper 4
bits of A now become lower 4 bits.
Suppose A = 45H
A = 0100 0101 b
SWAP a gives A = 0101 0100b = 54H
Data Transfer Instructions:
MOV Dest, Source: Here destination can be accumulator A, any register R0-R7, direct address or
indirect address of the destination. The source information can be accumulator, registers R0-R7, direct
address or indirect address of the operand or an immediate data.
This instruction copies the contents of source into the destination.
TEACH A COURSE 23
Instruction set
TEACH A COURSE 24
Instruction set
MOV X A, @DPTR: This instruction copies the contents of an external memory whose 16 bit address is
present in DPTR register into accumulator A.
Suppose DPTR = 1500H and (1500H) = 25H
Then contents of location 1500H which is an external memory location is copied into A. ∴ A 25H
MOVX @ Ri, A: Here, the contents of register A is copied into an external RAM location whose address is
present in Ri.
MOV X@DPTR, A: This instruction copies contents of A into the external memory location whose address
is present in DPTR.
Suppose A = 30H, DPTR = 1500H
∴ After this instruction (1500H) 30H
TEACH A COURSE 25
Instruction set
PUSH, Direct: This contents of the direct address specified in the instruction is copied onto the stack.
Before pushing, stack pointer is incremented by 1.
PUSH 25H [25H] = 50H
It means contents of location 25H is copied onto the stack.
∴ The top of stack will have data 50H.
POP Direct: The contents of the stack (8 bit) are copied onto the address which is directly specified in the
instruction.
∴ Suppose address = 25H and Top of stack has data 10H. Then after POP 25H (25H) 10H
After the POP instruction, stack pointer is decremented by 1.
TEACH A COURSE 26
Instruction set
XCH Dest, Source: Here destination is always A. Source can be a register, memory location directly or
indirectly specified memory locations.
This instruction exchanges bytes of information between source and destination.
(Source) (Destination)
Suppose A = 30H and 30H and R5 = 20H
XCH A, R5 gives A 20H, R5 30H
MOV C A, @A + DPTR: Here effective address of operand will be sum of contents of A and DPTR register.
This address is used to fetch information from the code area (ROM) and copied into A register (MOV
from code area).
TEACH A COURSE 27
Instruction set
TEACH A COURSE 28
Instruction set
ORL C, bit: Logically ORs specified bit and CY. In the above example, if
ORL C, P2.5 is used then CY = 1(1+0)
MOV C, bit, MOV bit, C: The above instruction copies the status of a bit to CY or vice versa.
If P2.5 = 1
MOV C, P2.5 causes CY = 1
If CY = 0
MOV P3.1, C causes P3.1 = 0
JC rel address: If the carry flag is set then control is transferred to be the specified address.
JNC address: If carry flag is not set, control is transferred to the specified address.
TEACH A COURSE 29
Instruction set
TEACH A COURSE 30
Instruction set
JBC bit, address: This instruction checks for the specified bit. If it is set to 1., then control is transferred to
a specified address, and also the bit is complemented.
If P2.5 = 1 then
JBC P2.5, 200H will check status of P2.5. Since it is 1, control will be transferred to location 200H and
also P2.5 is complemented to 0.
Program Control or Branching Instructions: These instruction help the programmer to take decisions and
transfer program control to the desired section in the program.
ACALL 11 bit address, Absolute call: This instruction will cause the control to the specified address.
Because the address is 11 bits ≡ 2K location, the ranges of transferring control will be ± 2K locations from
the current location.
TEACH A COURSE 31
Instruction set
L CALL 16 bit address, Long call: This instruction also causes the control to be transferred to the
specified address. Here, the address is 16 bit, so the range of transferring control will be anywhere in
the 64KB memory area.
RET: Returns to the main program from a subroutine. It is always the last instruction of the subroutine.
The control is transferred to the next instruction after CALL in the main program.
RETI, return from interrupt: Transfers control from an interrupt service routine to the next instruction
after interrupt in the main program.
AJMP address 11 bit: Absolute jump to the specified address. Same as ACALL. However, in a ACALL the
return address of the next instruction in the main program is stored on stack before transferring control
to subroutine. IN AJMP return address is not stored.
TEACH A COURSE 32
Instruction set
TEACH A COURSE 33
Instruction set
TEACH A COURSE 34
Addressing Modes
In 8051 there are 1-byte, 2-byte instructions and very few 3-byte instructions are present. The opcodes
are 8-bit long. As the opcodes are 8-bit data, there are 256 possibilities. Among 256, 255 opcodes are
implemented.
The clock frequency is12MHz, so 64 instruction types are executed in just 1 µs, and rest are just 2 µs.
The Multiplication and Division operations take 4 µsto to execute.
Addressing modes provide a way for accessing an operand. They are the modes using which an operand
can be selected/printed. 8051 programs support different addressing modes.
TEACH A COURSE 35
Addressing Modes
TEACH A COURSE 36
Addressing Modes
Direct Addressing Mode: In this mode, the direct address of the operand in the RAM is specified along
with the instruction. It is also possible that both source and destination addresses can be specified.
The user RAM has addresses between 00H to 7FH.
The following are some of the valid instructions employing direct addressing:
a. MOV R0, 60H
b. MOV 40H, A
c. MOV 40H, 60H
In the first instruction, contents of RAM location 60H is copied into R0 if (60H) = 30H then after that
instruction is executed R0 30H.
TEACH A COURSE 37
Addressing Modes
In the second instruction, contents of A are copied into RAM location whose address is 40H. i.e. if A =
10H., after executing the instruction (40H) 10H.
In the third instruction, contents of location 60H in RAM is copied into the RAM address 40H.
MOV 05, C
This instruction copies contents of carry into bit addressable area of memory whose address is 5. i.e. 5 th
bit at address 20H (20H-2FH are bit addressable area.)
TEACH A COURSE 38
Addressing Modes
Immediate Addressing Mode: In this addressing mode, source operand will be an immediate data while
destination operand can be register memory location., direct or indirect address of the location. The
immediate data is always preceded by ‘#’ sign. The following are the valid instructions of immediate
addressing mode.
MOV R5, #60H
MOV 60H, #60H
MOV @R0, #60H
Mov DPTR, #0060H
Here, the immediate data is 60H, but the destination are R5, memory location 60H, indirect location
whose address is in R0, or 16 bit DPTR register.
TEACH A COURSE 39
Addressing Modes
Register Indirect Addressing Mode: Here, a specific register holds the address of the memory location
where the operand is present, i.e. the contents of the register a pointer to the data in memory. If the
data is in the RAM then R0 and R1 registers must only be used to hold the memory address. To access
data or information from ROM, DPTR register is used.
Suppose R0 is 50H and (50H) 10H then MOV A, @ R0 copies the contents of location 50H, i.e. 10H into
A.
The sign ‘@’ indicates that we are using indirect addressing.
Indirect addressing is useful when we want to transfer/ retrieve more data stored in continuous memory
locations. Here, the starting address can be address can be initialized in R0 or R1 and for subsequent
addresses we simply have to increment R0 to R1as shown below:
TEACH A COURSE 40
Addressing Modes
MOV R0 #40H
back: MOV B, @R0
inc R0
SJMP back
Here staring from address of RAM of 40H, the memory contents are copied on to B.
R0 and R1 are 8 bit. So they must be used to access the 128 bytes of ON chip/ internal RAM.
However, to access external RAM or ROM( internal/external) DPTR register must be use to hold the
address of the operand.
TEACH A COURSE 41
Addressing Modes
Register Addressing Mode: In this, addressing mode, both source and destination are registers of 8051. Some
examples are
MOV A, R5
Contents of R5 copied on to A
MOV B,A
Contents of A copied on to B
ADD A , R5
Contents of R5 is added to that of A
A A + R5
MOV R6, DPH
Contents of higher byte of data pointer register is copied into R6 register.
TEACH A COURSE 42
Addressing Modes
Indexed Addressing Mode: Its main application is in accessing data elements of any arrays or look up
tables located in the program ROM space of 8051.
Consider the instruction MOV C A, @ A + DPTR.
The effective address of the operand is obtained by adding contents of register A and DPTR. The contents
from this address in code area is copied to A.
Here, DPTR holds the starting address/base address of the table. ‘A’ stores the index value. To access next
elements of the table we simply have to increment A, so that DPTR + A points to the next elements of the
table. The contents of this effective location in code area is copied into A.
TEACH A COURSE 43
Addressing Modes
Consider:
MOV B, #0
MOV DPTR, #100H
MOV R5, #10
MOV R0,#40H
Next: MOV A, B
MOVC A, @A + DPTR
MOV @ R0, A
inc R0,
inc B
DJNZ R5, next
TEACH A COURSE 44
Addressing Modes
Here A holds index which is copied from B every time. This loop copies 10 bytes from code area with
effective address A + DPTR to RAM location starting from 40H. After every loop A gets incremented by 1.
Whenever we need to access data from external memory we use MOVX instruction.
Relative Addressing Mode: It is used with certain jump instruction. A relative address (or offset) is an 8
bit signed value., which is added to the program counter to from the address of the next instruction
executed. For Ex: SJMP 8 bit. The 8 bit addresses giving a range of assembler directives.
TEACH A COURSE 45
Assembler directives (ORG, END)
Assembler directives are also called pseudo instructions as there is no need to decode it. An instruction,
on the other hand, has to be decoded before execution.
The assembler directives are commands to the assembler to operate in a specified way. Since,8051
handles 8 bit data and 16bit addresses, some of the directives supported by 8051 are:
DB (define byte): This directives tells the assembler that the size of the data is 8 bits.
X dB 25H: will define the size of the variable X as 8 bit number. So 25H will be stored as 8 bit number.
DW define word: This directive tells the assembler that the size of the data is 16 bits (a word of data).
Two memory locations are used for storing the number.
ORG(origin): It indicates that the program is beginning from the starting address which is specified
with org.
TEACH A COURSE 46
Assembler directives (ORG, END)
ORG 0H: This indicates that the program is beginning from address 0H in memory (ROM). It has to be the first statement of every
program.
EQU(Equate): This is used to define a constant without using any memory location. Say, X EQU 50H means X = 50H, so in a
program wherever ‘X’ comes it is replaced by 50H. This directive associates a label with a constant value.
END (end): This indicates to the assembler the end of the program. It has to be the last statement of any assembly program. Any
statement after end is ignored by the program.
The following segment shows how the assembler directives can be used:
ORG 0H
X DB 50H
Y EQU 25H
MOV A,Y
END
TEACH A COURSE 47
Introduction to 8051 programming in C.
Assembly language is tedious to write and it is lengthier because assembly language instructions are
single functioned. One instruction results in only a small operation. Higher level language instructions
are simpler to remember as they are more English like and a single instruction can perform many small
tasks.
The choice for using ‘C’ language for 8051 programming is due to the following reasons.
Using C instructions, one can interact at the machine level just like assembly level language.
Easier to write program in C and modify and update it.
Many library functions are readily available which can be easily implemented.
‘C’ codes are easily portable to other machines with little or no modifications at all.
TEACH A COURSE 48
Introduction to 8051 programming in C.
Data Types in C:
Unsigned Char :8 bit (0 to 255) most widely used data type.
Signed Char: 8 bit data, MSB for sign so data ranges for -128 to +127. If variable is simply declared as char, it
is taken as a singed char.
Unsigned Int:16 bit data ranging from 0 to 65535. Used to specify/store memory address 2 memory locations
are used to store the value.
Signed Int: 16 bit, MSB for sign. Range from – 32768 to +32767.
Sbit(Single bit): Widely used with 8051 programming as many registers and ports are bit addressable. Allows
access to single bits of special function registers.
Bit : Allows access to single bits of bit addressable RAM area 20H to 2FH.
Sfr: One can access entire byte of the special function register.
TEACH A COURSE 49
Introduction to 8051 programming in C
TEACH A COURSE 50
Introduction to 8051 programming in C
Time Delay in C: The time delay depends on the number of check pulses which correspondence to 1
machine cycle, crystal frequency and the choice of the complier used. (For the same job, different
compilers produce different assembly code of which in turn decides how much time is required).
One can use msdelay () function to provide required time delay. If we write a delay loop for 1 ms and say
msdelay (100) we get 100 ms time delay. For Ex:
Void msdelay(unsigned int i time)
{
unsigned int i, j;
for( i=0; i< itime; i++)
for(j=0; j<1275;j++)
}
The inner delay loop j is executed ‘itime’ times.
TEACH A COURSE 51
Introduction to 8051 programming in C
TEACH A COURSE 52
Introduction to 8051 programming in C
How to use bit(bit addressable area in RAM)?: This is done by the declaration.
bit mem = bit address. Here ‘mem’ is a variable which is assigned the specified bit address.
RAM Data Space Usage by 8051 IC Compiler.
1. First 8 bytes of RAM are allocated to Bank 0 (Addressesn 0-7)
2. Individual variables (Addresses 08 and beyond)
3. Array Elements (Addresses right after the variables
4. Stack (Addresses right after the array)
A Declaration of the Type: unsigned char num[] = “ABCDEF” declares array of size 6 with name ‘num’ in RAM
area.
Unsigned int AR[10] = , , , , , , Declares an array ‘AR’ of integer type having 10 elements in it.
TEACH A COURSE 53
Introduction to 8051 programming in C
Accessing code data Space in 8051 IC: This is done by using the following declaration.
Code unsigned char name[] = “1234ABCD”
Here, ‘code’ keyword tells the complier that name is a char array of unsigned characters in the code
area, i.e. 4KB ROM instread of RAM.
The following should be the structure of a typical 8051 C program:
#include<reg51.h>
Void main(void)
{
while(1) {
local declarations
& loop}
}
TEACH A COURSE 54
Summery
The assembly language is a low-level programming language used to write program code in terms of
mnemonics. : The assembly language is a fully hardware related programming language.
The 8051 assembly language programming is based on the memory registers.
The op-code is a single instruction that can be executed by the CPU. Here the op-code is a MOV instruction.
The operands are a single piece of data that can be operated by the op-code.
A Compiler is primarily used for programs that translate source code from a high-level programming language
to a machine-level language to create an executable program.
The Assembler takes as input the assembly code and translates it into relocatable machine code.
Usually, since the microcontroller is used for some specific task., it has a smaller and simpler instruction set as
compared to the processor.
TEACH A COURSE 55
Summery
TEACH A COURSE 56
Summary
Assembler directives are also called pseudo instructions as there is no need to decode it. An instruction,
on the other hand, has to be decoded before execution.
Assembly language is tedious to write and it is lengthier because assembly language instructions are
single functioned. One instruction results in only a small operation. Higher level language instructions
are simpler to remember as they are more English like and a single instruction can perform many small
tasks.
--------------X----------------
End
TEACH A COURSE 57