Mod 2 Part 2
Mod 2 Part 2
Module 2
Addressing modes, instruction set, instruction classification.
Assembly language programming examples for 8051.
1
Module 2 ECT206 Computer Architecture and microcontrollers
Types of instructions
Depending on operation they perform, all instructions are divided in several groups:
The first part of each instruction, called MNEMONIC refers to the operation an instruction
performs (copy, addition, logic operation etc.). Mnemonics are abbreviations of the name of
operation being executed. For example: INC R1 - Means: Increment register R1 (increment
register R1).The other part of instruction, called OPERAND is separated from mnemonic by
at least one white space and defines data being processed by instructions. Some of the
instructions have no operand, while some of them have one, two or three. Here is a list of the
operands and their meanings:
2
Module 2 ECT206 Computer Architecture and microcontrollers
Instruction which are use to transfer data from one register to another register, memory to
register or register to memory, comes under this groups. Data transfer instruction is divided
into,
• Move destination byte , source byte
• Move destination bit , source bit MOV DPTR, # 16 bit
value
1. MOV A,#data
Move the 8 bit constant data immediately to the accumulator (A)
Examples:
MOV A, # 25H ; move the 25h data to the A register
MOV A, #39H ; move the 39h data to the A register
2. MOV A,Rn
Move the content of register Rn ( n is 0 to 7) to the A register
Examples:
MOV A, R3 ; move the content of reg R3 to A
MOV A ,R5 ; move the content of reg R5 to A
3. MOV A, direct
Move the content internal RAM location to the accumulator
Example:
3
Module 2 ECT206 Computer Architecture and microcontrollers
1. MOV Rn, A
Move the content of accumulator to the register Rn( n is 0 -7)
Example;
MOV R0, A : move the content of A to the register R0
2. MOV direct, A
Move the content of register A to the internal RAM location given in the
instruction Example:
MOV 40H, A : move the content of accumulator to the internal RAM location
40H
3. MOV @Ri, A
Move the content of accumulator to the internal RAM location stored in the
Ri(i=0or1) Example:
MOV @R0,A : move the content of register A to the internal RAM location given in
R0.
c) Rn is the destination
This can have the following format,
2. MOV Rn, A
Move the content of accumulator to the register Rn( n is 0 -7)
Example;
MOV R0, A : move the content of A to the register R0
Move the content of internal RAM location given in the instruction to the register
4
Module 2 ECT206 Computer Architecture and microcontrollers
Rn( n is 0-7).
Example;
MOV RO, 40H :move the content of internal RAM location to RO.
Example:
MOV 40h, @R0 ; Move the content of internal ram location stored in the RO
register to the internal ram location 40h.
3. MOV direct, A
Move the content of register A to the internal RAM location given in the
instruction Example:
MOV 40H, A : move the content of accumulator to the internal RAM location
40H
4. MOV direct, Rn
Move the content of register Rn ( n is 0 -7) to the internal ram address given in the
instruction. Example;
MOV 30H, R5 ; Move the content of reg R5 to the internal RAM location
30h.
5
Module 2 ECT206 Computer Architecture and microcontrollers
Move the content of accumulator to the internal RAM location stored in the
Ri(i=0or1) Example:
MOV @R0,A : move the content of register A to the internal RAM location
given in R0.
3. MOV @Ri, direct
Move the content of internal ram address given in the instruction to the internal
ram address stored in Ri ( i =0 or 1).
Example;
MOV @ R0, 40H ; move the content of 40h to the internal ram location stored in
the R0
1. MOVX A, @ Ri where i = 0 or 1
This moves to the accumulator a byte from external memory whose 8-bitaddress is
pointed to by Ri
Example;
MOVX A,@Rl. ;move the content of external memory stored in the R1 to
accumulator.
2. MOVX A, @ DPTR
This moves into the accumulator a byte from external memory whose address is
pointed to by DPTR.In other words, this brings data into the CPU (register A) from
the off-chip memory of the 8051.
3. MOVX @ Ri , A
This moves a byte from register A to an external memory location whose8-bit address
is held by RO(or Rl in MOVX @ Rl,A)
4. MOVX @ DPTR, A
This moves the contents of the accumulator to the external memory location whose
address is held by DPTR. In other words, this takes data from inside the CPU (register
A) to memory outside of the 8051.
g) Code memory read only data moves (from ROM)
6
Module 2 ECT206 Computer Architecture and microcontrollers
space (on-chip ROM) is formed by adding the original value of the accumulator to the
16-bit DPTR register.
2. MOVC A, @ A+PC
This MOV instruction copies the source bit to the destination bit. In this instruction one of the
operands must be the CY -flag. Look at the following examples.
This instruction loads the 16-bit DPTR (data pointer) register with a 16-bitimmediate value
.Examples:
POP direct
This copies the byte pointed to by SP (stack 'pointer) to the location whose direct address is
indicated, and decrements SP by 1. Notice that this instruction supports only direct
addressing mode. Therefore, instructions such as "POP A" or "POP R3" are illegal. Instead
we must write "POP OEOH" where EOH is the RAM address belonging to register A and
"POP 03" where 03 is the RAM address ofR3 of bank0.
PUSH direct
This copies the indicated byte onto the stack and increments SP by 1 Notice that this
instruction supports only direct addressing mode. Therefore, instructions such as "PUSH A"
or "PUSH R3" are illegal. Instead we must write "PUSH OEOH" where EOH is the RAM
address belonging to register A and "PUSH 03" where 03 is the RAM address ofR3 of bank0.
7
Module 2 ECT206 Computer Architecture and microcontrollers
XCH A, Byte
This instruction swaps the contents of register A and the source byte. The source byte can be
any register or RAM location.
Example:
XCHD A, @Ri
The XCHD instruction exchanges only the lower nibble of A with the lower nibble of the
RAM location pointed to by Ri while leaving the upper nibbles in both places intact.
Example: Assuming RAM location 40H has the value 97H, find its content after the following
instructions.
;40H= (97H)
MOV A,#l2H;A=12H (0001 0010 binary)
MOV R1,40H;R1=40H, load pointer
XCHD A,@R1;exchange the lower nibble of;A and RAM location 40H
After execution of the XCHD instruction, we have A = 17H and RAM location 40H has 92H.
8
Module 2 ECT206 Computer Architecture and microcontrollers
9
Module 2 ECT206 Computer Architecture and microcontrollers
Arithmetic Instructions
The instruction of this group performs arithmetic operation such as addition, subtraction,
increment, decrement etc. Arithmetic instruction is divided into
INC byte
This instruction adds 1 to the register or memory location specified by the operand. Note that
CY is not affected even if value FF is incremented to 00
1. INCA
This instruction adds 1 to the accumulator content.
2. INC Rn
This instruction adds 1 to the content of Rn (n= 0-7) register
Example;
INC R0
INC R3
3. INC direct
This instruction adds 1 to the byte in internal ram location.
Example;
INC 40H ; instruction adds 1 to the byte in internal ram location
4. INC @Ri
This instruction adds 1 to the byte pointed to by Ri.
Example;
INC @R0 ; This instruction adds 1 to the byte pointed to by R0
INC DPTR
This instruction increments the 16-bit register DPTR (data pointer) by 1.Notice that DPTR is
the only 16-bit register that can be incremented. Also notice that there is no decrement
version of this instruction.
Example:
10
Module 2 ECT206 Computer Architecture and microcontrollers
DEC byte
This instruction subtracts 1 from the byte operand. Note that CY (carry/borrow)is unchanged
even if a value 00 is decremented and becomes FF.
1. DECA
This instruction subtracts 1 to the accumulator content.
2. DEC Rn
This instruction subtract 1 to the content of Rn (n= 0-7) register
Example;
DEC R5
DEC R4
3. DEC direct
This instruction subs 1 to the byte in internal ram location.
Example;
DEC 40H ; instruction subs 1 to the byte in internal ram location
4. DEC @Ri
This instruction subs 1 to the byte pointed to by Ri.
Example;
DEC @R0 ; This instruction subs 1 to the byte pointed to by R0
Addition instructions
This adds the source byte to the accumulator (A), and places the result in A. Since register A
is one byte in size, the source operands must also be one byte. Flags affected are Flags: OV,
AC, and CY.
1. ADD A, # data
This instruction adds the 8 bit data to the content of A and places the result in A
Example;
ADD A, # 40H ; Adds the content of accumulator to data 40h and result
placed in the A
2. ADD A , Rn
This instruction adds the content register Rn( n= 0-7) to the content of A and places
the Result in A
Example;
ADD A, R0 ; This instruction adds the content register R0 to the content of A and
places the result in A
11
Module 2 ECT206 Computer Architecture and microcontrollers
3. ADD A, direct
This instruction adds the content of internal RAM location given in the instruction to
the
accumulator. And places the result in A
Example;
ADD A, 40H ; this instruction adds the content of internal RAM location
40H to the accumulator. And places the result in A
4. ADD A, @ Ri
This instruction adds the contents of internal RAM location stored in the Ri(i=0or 1)
to the accumulator. And places the result in A
Example;
ADD A, @ R0 ; This instruction adds the contents of internal RAM
location stored in the R0 to the accumulator. And places the result in A
This instruction is used after addition of BCD numbers to convert the result back to
BCD. The data is adjusted in the following 2 possible cases.
1. It adds 6 to the lower 4 bits of A if it is greater than 9 or if AC = 1.
2. It also adds 6 to the upper 4 bits of A if it is greater than 9 or if CY = 1.
Example:
MOV A, #47H ; A=0100 0111
ADD A, #38H ; A=47H+38H=7fH, invalid BCD
DA A ; A=1000 0101=85H, valid BCD
47H
+ 38H
7FH
+ -6H
85H
This will add the source byte to A, in addition to the CY flag (A = A + byte+ CY). If CY = I
prior to this instruction, CY is also added to A. If CY =0 prior to the instruction, source is
added to destination plus O. This is used in multi byte additions. Flags affected are OV, AC,
CY,
1. ADDC A, # data
This instruction adds the 8 bit data, CY content to the content of A and places the
result in AExample;
ADDC A, # 40H ; Adds the content of accumulator to data 40h,CY flag and result
12
Module 2 ECT206 Computer Architecture and microcontrollers
placed in the A
13
Module 2 ECT206 Computer Architecture and microcontrollers
2. ADDC A, Rn
This instruction adds the content register Rn( n= 0-7), CY flag to the content of A and
places the result in A
Example;
ADDC A, R0; This instruction adds the content register R0, cy flag to the
content of A and places the result in A
3. ADDC A, direct
This instruction adds the content of internal RAM location given in the instruction,
CY flag content to the accumulator. And places the result in A
Example;
ADD A, 40H ; this instruction adds the content of internal RAM
location 40H,cy flag content to the accumulator. And places the result in A
4. ADDC A, @Ri
This instruction adds the contents of internal RAM location stored in the
Ri(i=0or 1), cy flag content to the accumulator. And places the result in A
Example;
ADD A, @ R0 ; This instruction adds the contents of internal RAM
Location stored in the R0 , cy flag content to the accumulator. And places the result
in A
Subtraction instructions
This can have the following four formats
SUBB A, source-byte
This subtracts the source-byte and the carry flag from the accumulator and puts the result in
the accumulator. Flags affected are OV, AC, CY The steps for subtraction performed by
the internal hardware of the CPU are as follows:
1. SUBB A, # data
This instruction subtract the 8 bit data and the carry flag from the
accumulator and put result in the accumulator.
Example;
SUBB A, # 50H ; This instruction subtract the 50H data and the carry
flag from the accumulator and put result in the accumulator.
2. SUBB A, Rn
14
Module 2 ECT206 Computer Architecture and microcontrollers
This instruction subtract the content of Rn (n=0-7 ) and the carry flag from
the accumulator and put result in the accumulator
Example;
SUBB A, R0 ;This instruction subtract the content of R0 and the
carry flag from the accumulator and put result in the accumulator
3. SUBB A, direct
This instruction subtract the content of internal RAM address and the
carry flag from the accumulator and put result in the accumulator.
Example;
SUBB A, 40H ; This instruction subtract the content of internal RAM
address 40H and the carry flag from the accumulator and put
result in
the
Accumulator.
4. SUBB A, @ Ri
This instruction subtract the content of internal RAM address stored in Ri(i= 0 or 1)
and the carry flag from the accumulator and put result in the accumulator.
Example;
SUBB A, @R0 ; This instruction subtract the content of internal RAM
address Stored in R0 and the carry flag from the accumulator
and put
result in the accumulator.
MUL AB
This multiplies an unsigned byte in A by a unsigned byte in register Band the result is placed
in A and B where A has the lower byte and B has the higher byte. Flags affected are OV, CY
Example:
MOV A,#10
MOV B,#15
MUL AB ;A=150=96H, B=OO
DIV AB
This instruction divides a byte accumulator by the byte in register B. It is assumed that both
registers A and B contain an unsigned byte. After the division ,the quotient will be in register
A and the remainder in register B. If you divide by zero(that is setting register B = 0 before
the execution of"DIV AB"), the values in register A and B are undefined and the 0 V flag is
set to high to indicate an invalid result. Notice that CY is always 0 in this instruction.
15
Module 2 ECT206 Computer Architecture and microcontrollers
Example:
MOV A,#97H
16
Module 2 ECT206 Computer Architecture and microcontrollers
MOV B, #l2H
DIV AB ;A=8 and B=7
17
Module 2 ECT206 Computer Architecture and microcontrollers
Logic Instructions
The instruction of this group performs logical operation such as AND, OR, XOR Rotate and
swap instructions etc. Logical instruction is divided into,
1. AND instruction
2. OR instruction
3. XOR instruction
4. Rotate and swap instruction
According to the operation logical instruction is mainly classified in to two that are
byte level logical operation and bit level logical operations.
AND instructions
ANL dest-byte,source-byte
This performs a logical AND on the operands ,bit by bit, storing the result in
the destination. Notice that both the source and destination values are byte-size only.
1. ANL A, # data
This instruction ANDs the 8 bit immediate data to the content of accumulator
and place the result in accumulator.
Example;
ANL A,# 09H ; This instruction ANDs the 8 bit immediate data09H to the
content of accumulator and place the result in accumulator.
18
Module 2 ECT206 Computer Architecture and microcontrollers
39 h 0011 1001
09 h 0000 1001
09 h 0000 1001
2. ANL A, direct
This instruction ANDs the content of internal RAM location with
accumulator content and place the result in A.
Example;
ANL A, 40H ; This instruction ANDs the content of internal RAM location
40h with accumulator content and place the result
in A.
3. ANL A, Rn
This instruction ANDs the content of register Rn(n=0-7) with accumulator
content and place the result in A.
Example;
ANL A, R0 ; This instruction ANDs the content of register Rn(n=0-7) with
accumulator content and place the result in
4. ANL A, @Ri
This instruction ANDs the content of internal RAM location stored in Ri( i=0
or1) with accumulator content and place the result in A.
Example;
ANL A, @R0 ;This instruction ANDs the content of internal RAM location
stored in R0with accumulator content and place the result in A.
6. ANL direct ,A
This instruction ANDs the accumulator with internal RAM location content and
place the result in RAM location.
Example;
ANL 40H, A ; This instruction ANDs the accumulator with internal RAM
location40H content and place the result in RAM location OR instructions
19
Module 2 ECT206 Computer Architecture and microcontrollers
This performs a logical OR on the byte operands, bit by bit, and stores the result in the
destination.
1. ORL A, # data
This instruction ORs the 8 bit immediate data to the content of accumulator and
place the result in accumulator.
Example;
ORL A,# 09H ; This instruction ORs the 8 bit immediate data 09H to the
content of accumulator and place the result in accumulator.
39H0011 1001
09H 0000 1001
39H0011 1001
2. ORL A, direct
This instruction ORs the content of internal RAM location with accumulator
content and place the result in A.
Example;
ORL A, 40H ; This instruction ORs the content of internal RAM location 40h
with accumulator content and place the result in A.
3. ORL A, Rn
This instruction ORs the content of register Rn(n=0-7) with accumulator
content and place the result in A.
Example;
ORL A, R0 ; This instruction ORs the content of register Rn(n=0-7) with
accumulator content and place the result in
4. ORL A, @Ri
This instruction ORs the content of internal RAM location stored in Ri( i=0
or1) with accumulator content and place the result in A.
Example;
ORL A, @R0; This instruction ORs the content of internal RAM location
stored in R0with accumulator content and place the result in A.
20
Module 2 ECT206 Computer Architecture and microcontrollers
6. ORL direct ,A
This instruction ORs the accumulator with internal RAM location content and
place the result in RAM location.
Example;
ORL 40H, A ; This instruction ORs the accumulator with internal RAM
location40H content and place the result in RAM location
XOR instructions
XRL dest-byte,source-byte
This performs a logical exclusive-OR on the operands, bit by bit, storing the result in the
destination
1. XRL A, # data
This instruction XORs the 8 bit immediate data to the content of accumulator
and place the result in accumulator.
Example;
XRL A,# 40H ; This instruction XORs the 8 bit immediate data 40H to the
content of accumulator and place the result in accumulator.
2. XRL A, direct
This instruction XORs the content of internal RAM location with
accumulator content and place the result in A.
Example;
XRL A, 40H ; This instruction XORs the content of internal RAM location
40h with accumulator content and place the result
in A.
3. XRL A, Rn
This instruction XORs the content of register Rn(n=0-7) with accumulator
content and place the result in A.
Example;
21
Module 2 ECT206 Computer Architecture and microcontrollers
32H0011 0010
50H0101 0000
62H0110 0010
4. XRL A, @Ri
This instruction XORs the content of internal RAM location stored in Ri( i=0
or1) with accumulator content and place the result in A.
Example;
XRL A, @R0 ;This instruction XORs the content of internal RAM location
stored in R0with accumulator content and place the result in A.
6. XRL direct ,A
This instruction XORs the accumulator with internal RAM location content
and place the result in RAM location.
Example;
XRL 40H, A ; This instruction XORs the accumulator with internal RAM
location40H content and place the result in RAM location
This rotates the bits of A left. The bits rotated out of A are rotated back into A at the opposite
end.
22
Module 2 ECT206 Computer Architecture and microcontrollers
Example:
MOV A,#69H
RL A;Now A= 11010010
RL A ;Now A=10100101
2. RR A
This rotates the bits of register Aright. The bits rotated out of A are rotated back into
A at the opposite end.
Example;
MOV A,#66H
RRA ;Now A= 00110011
RR A;Now A=10011001
3. RLC A
This rotates the bits of the accumulator left. The bits rotated out of register A are
rotated into CY, and the CY bit is rotated into the opposite end of the accumulator.
Example;
CLR C ; CY =0
MOV A,#99H ; A=10011001
RLC A ; NOW A =00110010 and CY =1
RLC A ; NOW A= 01100101 and CY =0
4. RRC A
This rotates the bits of the accumulator right. The bits rotated out of register A are
rotated into CY and the CY bit is rotated into the opposite end of the accumulator
23
Module 2 ECT206 Computer Architecture and microcontrollers
Example;
MOV A,# 99H ; A = 10011001
RRC A ; Now A = 11001100 CY=1
5. SWAP A
The SWAP instruction interchanges the lower nibble (DO - D3) with the upper Nibble
(D4 -D7) inside register A.
Example:
MOV A,#59H;A=59H (0101 1001 in binary)
SWAP A;A=95H (1001 0101 in binary)
1. CLR A
This instruction clears register A. All bits of the accumulator are set to O.
2. CPL A
This complements the contents of register A, the accumulator. The result is the l's
complement of the accumulator. That is: Os become 1s and 1s become Os.
AND Instructions
ANL C,source-bit
In this instruction the carry flag bit is AND ed with a source bit and the result is placed in
carry. Therefore, if source bit = 0, CY is cleared; otherwise, the CY flag remains unchanged.
Example;
ANL C, P2.2 ; In this instruction the carry flag bit is AND ed with a P2.2and the
result is placed in carry
Another variation of this instruction involves the ANDing of the CY flag bit with the
complement of the source bit. Its format is "ANL C,/bit".
ANL C, / P2.2 ; In this instruction the carry flag bit is ANDed with the compliment
of P2.2and the result is placed in carry
OR instructions
ORL C,source-bit
24
Module 2 ECT206 Computer Architecture and microcontrollers
In this instruction the carry flag bit is ORed with a source bit and the result is placed in the
carry flag. Therefore, if the source bit is 1, CY is set; otherwise, the CY flag remains
Unchanged.
Example;
ORL C , P2.3 ; In this instruction the carry flag bit ORed with the
bitP2.3 and theresult is placed in carry.
Another variation of this instruction involves ORing CY with the complement of the source
bit. Its format is "ORL C,/bit".
ORL C, /P2.3 ; In this instruction the carry flag bit ORed with the
Compliment ofP2.3 and the result is placed in carry.
1. CLR bit
This instruction clears a single bit. The bit can be the carry flag, or any bit-addressable
location in the 8051
Example;
CLR P 2.3 ; Clear the bit P2.3
2. CPL bit
This instruction compliments a single bit. The bit can be the carry flag, or any bitaddressable
location in the 8051
Example;
CPL P2.3 ; Compliment the bit P2.3,
3. SETB bit
This sets high the indicated bit. The bit can be the carry or any directly
addressable bit of a port, register, or RAM location.
Examples:
SETBPl. 3 ; Pl. 3=1
SETB P2.6 ; P2. 6=1
SETB ACC.6 ;ACC.6=1
SETB C ;Set Carry Flag
25
Module 2 ECT206 Computer Architecture and microcontrollers
26
Module 2 ECT206 Computer Architecture and microcontrollers
Branch Instructions;
This group includes the instruction for conditional and unconditional jump,
subroutine call and return, restart. Branch instruction is mainly classified in to ,
1. JUMP instructions
2. CALL instructions
3. Return instructions
The jump or call instruction may have on of the three ranges, that are
(a) Relative range of +128, to -127 byte from the instruction following the jump or
call instruction.
(b) Absolute range on the same 2k byte page as he instruction following the jump or
call.
(c) A long absolute range of any address from 0000h to FFFFh.
Jump instructions
Jump instructions are mainly classified in to two that are, conditional jump
instructions and unconditional jump instructions.
According to the operation the conditional jump instruction is mainly classified in to two,
(a) byte level conditional jump instruction,
(b) bit level conditional jump instruction
27
Module 2 ECT206 Computer Architecture and microcontrollers
In this instruction a byte is decremented, and if the result is not zero it will jump to target
address in relative range,
1. DJNZ Rn, target( radd)
In this instruction decremented the content of Rn( n=0-7), and if the result is not
zero it will jump to target address in relative range, Example;
DJNZ R0, target ; In this instruction decremented the content of R0,
and
if the result is not zero it will jump to target address in relative
range
2. DJNZ direct , target (radd)
In this instruction decremented the content of internal RAM location, and if the result is
not zero it will jump to target address in relative range,
Example;
DJNZ 40h, target ; In this instruction decremented the content of
RAM
Location , and if the result is not zero it will jump to target address in relative range
The magnitudes of the source byte and destination byte are compared. If they are not equal, it
jumps to the target address.
28
Module 2 ECT206 Computer Architecture and microcontrollers
Example;
CJNE@ R0, #40H, target ;This instruction compares the 8bit data
with the with the content of internal RAM location stored in register R0 If they are
not equal, then jump to the target address in the relative range.
2. JZ target( radd)
This instruction examines the contents of the accumulator and jumps if it has value
O.
29
Module 2 ECT206 Computer Architecture and microcontrollers
Notice that all "J condition" instructions are short jumps, meaning that the target address
cannot be more than -128 bytes backward or +127 bytes forward of the PC of the instruction
following the jump.
Unconditional jump do not test any bit or byte to determine whether the jump should
taken. The jump is always taken.
1. JMP @A+DPTR
The JMP instruction is an unconditional jump to a target address. The target address
is provided by the total sum of register A and the DPTR register.
2. AJMP target(Sadd)
AJMP stands for "absolute jump." It transfers program execution to the target address
unconditionally. The target address for this instruction must be within2K bytes of
program memory.
3. LJMP target(Ladd)
This is a 3-byte instruction. The first byte is the opcode and the next two bytes are the
target address. As a result, LJMP is used to jump to any address location within the
64K byte code space of the 8051.
4. SJMP target(radd)
This is a 2-byte instruction. The first byte is the opcode and the second byte is the
signed number displacement, which is added to the PC (program counter) of the
instruction following the SJMP to get the target address. Therefore, in this jump the
target address must be within -128 to + 127bytes of the PC (program counter)
5. NOP
This performs no operation and execution continues with the next instruction. It is
sometimes used for timing delays to waste clock cycles. This instruction only updates
the PC (program counter) to point to the next instruction following NOP.
Call instructions
Another control transfer instruction is CALL instruction, which is used to call subroutine.
There are two type of call instruction in 8051 that are.
1. LCALL(long call)
2. ACALL(absolute call)
There are two types of CALLs: ACALL and LCALL. In ACALL, the target address is
with in 2K bytes of the current PC (program counter). To reach the target address in the
64K bytes maximum ROM space of the 8051, we must use LCALL. If calling a
30
Module 2 ECT206 Computer Architecture and microcontrollers
subroutine, the
31
Module 2 ECT206 Computer Architecture and microcontrollers
PC register (which has the address of the instruction after the ACALL) is pushed onto the
stack and the stack pointer (SP)is incremented by 2. Then the program counter is loaded
with the new address and control is transferred to the subroutine. At the end of the
procedure, when RET is executed, PC is popped off the stack, which returns control to
the instruction after the CALL.
Notice that LCALL is a 3-byte instruction, in which one byte is the opcode, and the other
two bytes are the 16-bit address of the target subroutine. ACALL is a 2-byte instruction,
in which 5 bits are used for the opcode and the remaining 11 bits are used for the target
subroutine address. An II-bit address limits the range to 2K bytes.
Return instructions
RET
This instruction is used to return from a subroutine previously entered by instructions LCALL
or ACALL. The top two bytes of the stack are popped into the program counter (PC) and
program execution continues at this new address. After popping the top two bytes of the stack
into the program counter, the stack pointer (SP) is decremented by 2.
RETI
This is used at the end of an interrupt service routine (interrupt handler). The top two bytes of
the stack are popped into the program counter and program execution continues at this new
address. After popping the top two bytes of the stack into the program counter (PC), the stack
pointer (SP) is decremented by 2. Notice that while the RET instruction is used at the end of a
subroutine associated with the ACALL and LCALL instructions, IRET must be used for the
interrupt service subroutines.
32
Module 2 ECT206 Computer Architecture and microcontrollers
33
Module 2 ECT206 Computer Architecture and microcontrollers
ARITHMETIC PROGRAMS
Example 1;
Example 2;
Write a program to add two 8 bit numbers. Assume that input data is in the memory
location 4500 and 4501 and store the result in 4503 and 4504 .and also check the carry.
CLR R3 ; clear R3
MOV DPTR, #4500H ; DPTR =4500
MOVX A, @DPTR A ; move the 1st data in memory location 4500 to
Example 3
34
Module 2 ECT206 Computer Architecture and microcontrollers
Write a program to add two 8 bit numbers. Assume that input data is in the internal RAM
location 45h and 46H and store the result in 60Hand 61H.and also check the carry.
CLR R3 ; clear R3
MOV A, 45 H ; move the 1st data in memory location 45H to A
MOV R0, 46H ; move the 2nd data in memory location 46H to R0
ADD A, R0 ; A+R0= A
JNC LOOP ; check the carry ,if no carry go to loop otherwise go to next step
Example 4;
Write a program to add N 8 bit numbers. Assume that input data is in the internal RAM
location starting fro 46H and store the result in 60Hand 61h.and also check the carry.
Example 5;
Write a program to add two 16 bit numbers,
Note:- for addition of two 16bit number , first add the LSB of two 16 bit numbers with ADD
instruction and then add MSB with ADDC instruction.
The numbers are 4045h and 4140h
CLR C ; clear cy
MOV A,# 45H ; move LSB of first number to A
ADD A, # 40H ; add the LSB of first no: with 2nd number.
35
Module 2 ECT206 Computer Architecture and microcontrollers
Write a program to subtract two 8 bit numbers. Assume that input data is in the internal RAM
location 45h and 46H and store the result in 60Hand 61H.and also check the carry.
Example 8;
To write an assembly language program to generate Fibonacci series.
MOV DPTR, #4500H
MOVX A,@DPTR
MOV R1, A
JNZ LOOP3
36
Module 2 ECT206 Computer Architecture and microcontrollers
SJMP HERE
37
Module 2 ECT206 Computer Architecture and microcontrollers
38
Module 2 ECT206 Computer Architecture and microcontrollers
4500 h 01 h 4505 h 04 h
4501 h 02 h
4502 h 03 h
4503 h 04 h
Example 10 ;
To write an assembly language program to sort an array in ascending order.
39
Module 2 ECT206 Computer Architecture and microcontrollers
4500 h 04 h 4501 h 22 h
4501 h 43 h 4502 h 33 h
4502 h 33 h 4503 h 42 h
4503 h 22 h 4504 h 43 h
4504 h 42 h
Example 11;
Example 12;
To write an assembly language program to generate triangular waveform
40
Module 2 ECT206 Computer Architecture and microcontrollers
For the CPU to execute an instruction takes a certain number of clock cycles. In the 8051
family, these clock cycles are referred to as machine cycles. List of 8Q51 instructions and
their machine cycles are explained in the above section. To calculate a time delay, we use this
list. In the 8051 family, the length of the machine cycle depends on the frequency of the
crystal oscillator connected to the8051 system. The crystal oscillator, along with on-chip
circuitry, provide the clock source for the 8051 CPU. The frequency of the crystal connected
to the 8051 family can vary from 4 MHz to 30 MHz, depending on the chip rating and
manufacturer. Very often the 11.0592 MHz crystal oscillator is used to make the 8051-based
system compatible with the serial port of the IBM PC . In the 8051, one machine cycle lasts
12 oscillator periods. Therefore, to calculate the machine cycle, we take 1/12 of the crystal
frequency, then take its inverse,
Example;
The following shows crystal frequency for three different 8051-based systems. Find the
period of the machine cycle in each case.
(a) 11.0592 MHz (b) t6MHz (c) 20 MHz
41
Module 2 ECT206 Computer Architecture and microcontrollers
Solution:
(a) 11.0592/12 = 921.6 kHz; machine cycle is 1/921.6 kHz = 1.085µs (microsecond)
(b) 16MHz/12= 1.333 MHz; machine cycle (MC) = 1/.1.333 MHz = 0.75µs (microsecond
(c) 20 MHz/12 = 1.66 MHz; MC = 1/1.66 MHz = 0.60µs
Example;
For an 8051 system of l 1.0592MHz find how long it takes to execute each of the following
instructions.
Solution:
The machine cycle for a system of 11.0592 MHz is 1.085µs Machine cycles for each of the
above instructions. Therefore, we have:
Example;
Find the size of the delay in the following program, if the; crystal frequency is 11.0592MHz
MOV A,#55H
AGAIN: MOV Pl,A
ACALL DELAY
CPL A
SJMP AGAIN
delay
DELAY: MOV R3,#200
HERE: DJNZ R3,HERE RET
42
Module 2 ECT206 Computer Architecture and microcontrollers
Solution:
We have the following machine cycles for each instruction of the DELAY subroutine.
Machine Cycle
DELAY:MOV R3,#200 1
HERE:DJNZ R3,HERE 2
RET 1
Therefore, we have a time delay of [(200 x 2) + 1 + 1] x 1.085µs=436.17.
Example;
For a machine cycle of 1.085µs, find the time delay in the following subroutine.
For the HERE loop, we have (4 x 250) 1.085µs = 1085 µs. The AGAIN loop repeats the
HERE loop 200 times; therefore, we have 200×1.085 µs=217000, if we do not include the
overhead. However, the instructions "MOV R3, #250" and DJNZ R2 ,AGAIN “at the
beginning and end of the AGAIN loop add (3 x 200 x1.085 µs) =651µsto the time delay. As a
result we have 217000 + 651 =217651 Milliseconds for total time delay associated with the
above DELAY subroutine.
Addressing modes
The CPU can access data in various ways. The data could be in a register, or in memory, or
be provided as an immediate value. These various ways of accessing data are called
addressing modes. The various addressing modes of a microprocessor are determined when it
is designed- and therefore cannot be changed by the programmer. The 8051 provides a total
of five distinct addressing modes. They are as follows:
(l) Immediate
(2) Register
(3) Direct
(4) register indirect
(5) Indexed
43
Module 2 ECT206 Computer Architecture and microcontrollers
In this addressing mode, the source operand is a constant. In immediate addressing mode, as
the name implies, when the instruction is assembled, the operand comes immediately after
the opcode. Notice that the immediate data must be preceded by the pound sign, "#". This
addressing mode can be used to load information into any of the registers, including the
DPTR register. Examples follow.
Although the DPTR register is 16-bit, it can also be accessed as two 8-bitregisters, DPH and
DPL, where DPH is the high byte and DPL is the low byte. Look at the following code.
MOV DPTR,#2550H is
the same as:
MOV DPL,#50H MOV
DPH,#25H
Register addressing mode involves the use of registers to hold the data to be manipulated.
Examples of register addressing mode follow.
It should be noted that the source and destination registers must match in size. In other words,
coding "MOV DPTR,A" will give an error, since the source is an 8-bit register and the
destination is a 16-bit register. See the following.
MOV DPTR,#25F5H
MOV R7, DPL
MOV R6,DPH
Notice that we can move data between the accumulator and Rn (for n = 0 to 7) but movement
of data between Rn registers is not allowed .For example, the instruction "MOV R4, R7" is
invalid.
44
Module 2 ECT206 Computer Architecture and microcontrollers
There are 128 bytes of RAM in the 8051. The RAM has been assigned
addresses 00 to 7FH. The following is summary of the allocation of these 128 bytes.
1. RAM locations 00 - IFH are assigned to the register banks and stack.
2. RAM locations 20 - 2FH are set aside as bit-addressable space .
3. RAM locations 30 - 7FH are available as a place to save byte- sized data.
Although the entire 128 bytes of RAM can be accessed using direct addressing mode, it is most
often used to access RAM locations 30 - 7FH. This is due to the fact that register bank locations
are accessed by the register names of .R0 - R7, but there is no such name for other RAM
location. in direct addressing mode, the data is in a RAM memory location whose address is
know, and this address is given as a part of the instruction. Contrast this with immediate
addressing mode in which the operand itself is provide~ with the instruction. The "#" sign
Distinguishes between the two modes. See the examples below, and note the absence of the "#"
sign.
In the register indirect addressing mode, a register is used as a pointer to the data. If the data
is inside the CPU, only registers RO and Rl are used for this purpose: In other words, R2 -R7
cannot be used to hold the address of an operand located in RAM when using this addressing
mode: when RO and Rl are used as pointers, that is, when they hold the addresses of RAM
locations, they must be preceded by the "@" sign, as shown below.
One of the advantages of register indirect addressing mode is that it make accessing data
dynamic rather than static as in the case of direct addressing mode. (b) looping of instruction
is possible .
Limitation of register indirect addressing mode in the 8051
As stated earlier, RO and Rl are the only registers that can be used for pointers in register
indirect addressing mode. Since RO and Rl are 8 bits wide,
45
Module 2 ECT206 Computer Architecture and microcontrollers
Indexed addressing mode is widely used in accessing data elements of look-up table entries
located in the program ROM space of the 8051. The instruction used for this purpose is
"MOVC A, @A+DPTR". The 16-bit register DPTR and register A are used to form ,the
address of the data element stored in on-chip ROM. Because the data elements are stored in
the program (code) space ROM of the8051, it uses the instruction MOVC instead of MOV.
The "C" means code. In this instruction the contents of A are added to the 16-bit register
DPTR to form the 16- bit address of the needed data.
46