Module 2
Module 2
outline
• Addressing Modes, Data Transfer instructions,
Arithmetic instructions, Logical instructions,
Branch instructions, Bit manipulation
instructions. Simple Assembly language
program examples (without loops) to use
these instructions.
• Introduction 8051 Instruction Set
• CIP-51 architecture and memory organization review
• Addressing Modes
– Register addressing
– Direct addressing
– Indirect addressing
– Immediate constant addressing
– Relative addressing
– Absolute addressing
– Long addressing
– Indexed addressing
• Instruction Types
– Arithmetic operations
– Logical operations
– Data transfer instructions
– Boolean variable instructions
– Program branching instructions
Introduction
• A computer instruction is made up of an operation code (op-code)
followed by either zero, one or two bytes of operands
• The op-code identifies the type of operation to be performed while
the operands identify the source and destination of the data
• The operand can be:
– The data value itself
– A CPU register
– A memory location
– An I/O port
• If the instruction is associated with more than one operand, the
format is always:
Instruction Destination, Source
CIP-51 Architecture Review
Memory Organization
The memory
organization of
C8051F020 is
similar to that of
a standard 8051
PCA0CPM PCA0CPM
D8 PCA0CN PCA0MD PCA0M0 PCA0CPM1 PCA0CPM2
3 4
0(8)
Bit 1(9) 2(A) 3(B) 4(C) 5(D) 6(E) 7(F)
addressable
Addressing Modes
• Eight modes of addressing are available with the C8051F020
• The different addressing modes determine how the operand
byte is selected
• Example:
MO R0, A
V
• Example:
• Examples:
ADD A,#030H ;Add 8-bit value of 30H to
;the accumulator register
;(which is an 8-bit register).
GoBack:DEC A ;Decrement A
JNZ GoBack ;If A is not zero, loopback
Absolute Addressing
• Two instructions associated with this mode of addressing
are ACALL and AJMP instructions
• These are 2-byte instructions where the 11-bit absolute
address is specified as the operand
• The upper 5 bits of the 16-bit PC address are not modified.
The lower 11 bits are loaded from this instruction. So, the
branch address must be within the current 2K byte page of
program memory (211 = 2048)
• Example:
ACALL PORT_INIT ;PORT_INIT should be
;located within 2k bytes.
• Example:
MO A,#08H ;Offset from table start
V DPTR,#01F00H ;Table start address
MO A,@A+DPTR ;Gets target value from thetable
V ;start address + offset and putsit
MO ;in A.
VC
• After the execution of the above instructions, the program will branch to
address 1F08H (1F00H+08H) and transfer into the accumulator the data byte
retrieved from that location (from the look-up table)
Instruction Types
• The C8051F020 instructions are divided into
five functional groups:
– Arithmetic operations
– Logical operations
– Data transfer operations
– Boolean variable operations
– Program branching operations
Arithmetic Operations
• With arithmetic instructions, the C8051F020 CPU has no special knowledge of
the data format (e.g. signed binary, unsigned binary, binary coded decimal,
ASCII, etc.)
• The appropriate status bits in the PSW are set when specific conditions are
met, which allows the user software to manage the different data formats
RAM location and an SFR location MOV @Ri, #data [@Ri] = immediate data
without going through the accumulator MOV DPTR, #data 16 [DPTR] = immediate data
• The operations include set, clear, and, CLR bit Clear direct bit
• Also included are bit–level moves or SETB bit Set direct bit
• All bit accesses use direct addressing CPL bit Complement direct bit
ANL C,bit AND bit with C
ANL C,/bit AND NOT bit with C
• Examples:
ORL C,bit OR bit with C
ORL C,/bit OR NOT bit with C
• Rn refers to registers
R0-R7 of the
currently selected
register bank
ADD A,<source-byte> ADDC A,<source
byte>
• ADD adds the data byte specified by the source operand to
the accumulator, leaving the result in the accumulator
• ADDC adds the data byte specified by the source operand,
the carry flag and the accumulator contents, leaving the
result in the accumulator
• Operation of both the instructions, ADD and ADDC, can
affect the carry flag (CY), auxiliary carry flag (AC) and the
overflow flag (OV)
– CY=1 If there is a carryout from bit 7; cleared otherwise
– AC =1 If there is a carryout from the lower 4-bit of A i.e. from
bit 3; cleared otherwise
– OV=1 If the signed result cannot be expressed within the
number of bits in the destination operand; cleared
otherwise
SUBB A,<source-byte>
• SUBB subtracts the specified data byte and the carry flag together from
the accumulator, leaving the result in the accumulator
CY=1 If a borrow is needed for bit 7; cleared otherwise
AC =1 If a borrow is needed for bit 3, cleared otherwise
OV=1 If a borrow is needed into bit 6, but not into bit 7, or into bit 7,
but not into bit 6.
• Example:
The accumulator holds 0C1H (11000001B), Register1 holds 40H
(01000000B) and the CY=1.The instruction,
SUBB A, R1
gives the value 70H (01110000B) in the accumulator, with the CY=0 and
AC=0 but OV=1
INC <byte>
• Increments the data variable by 1. The instruction is used in register, direct
or register direct addressing modes
• Example:
INC 6FH
If the internal RAM location 6FH contains 30H, then the instruction
increments this value, leaving 31H in location 6FH
• Example:
MO R1, #5E
V R1
INC @R1
• If R1=5E
INC(01011110) and internal RAM location 5FH contains 20H, the
instructions will result in R1=5FH and internal RAM location 5FH to
increment by one to 21H
DEC <byte>
• The data variable is decremented by 1
• The Low order byte of the 16-bit product will go to the accumulator and
the High order byte will go to the B register
• If the product is greater than 255 (FFH), the overflow flag is set; otherwise
it is cleared. The carry flag is always cleared.
• If ACC=85 (55H) and B=23 (17H), the instruction gives the product 1955
(07A3H), so B is now 07H and the accumulator is A3H. The overflow flag is
set and the carry flag is cleared.
DIV AB
• Divides A by B
• Example:
ANL A,R2
If ACC=D3H (11010011) and R2=75H (01110101), the result of the
instruction is ACC=51H (01010001)
• Example:
AN P1,#10111001B
L
ORL<dest-byte>,<source-byte>
• This instruction performs the logical OR operation on the
source and destination operands and stores the result in
the destination variable
• Example:
ORL A,R2
If ACC=D3H (11010011) and R2=75H (01110101), the result
of the instruction is ACC=F7H (11110111)
• Example:
ORL P1,#11000010B
This instruction sets bits 7, 6, and 1 of output Port 1
XRL <dest-byte>,<source-byte>
• This instruction performs the logical XOR (Exclusive OR) operation
on the source and destination operands and stores the result in the
destination variable
• Example:
XRL A,R0
If ACC=C3H (11000011) and R0=AAH (10101010), then the
instruction results in ACC=69H (01101001)
• Example:
XRL P1,#00110001
This instruction complements bits 5, 4, and 0 of
output Port 1
CLR A CLR A and CPL A
• This instruction clears the accumulator (all bits set to 0)
• No flags are affected
• If ACC=C3H, then the instruction results in ACC=00H
CPL A
This instruction logically complements each bit of the accumulator (one’s complement)
No flags are affected
If ACC=C3H (11000011), then the instruction results in ACC=3CH (00111100)
RL A
• The 8 bits in the accumulator are rotated one
bit to the left. Bit 7 is rotated into the bit 0
position.
• Bit 7 of the accumulator will move into carry flag and the
original value of the carry flag will move into the Bit 0
position
MO R1,#60 ;R1=60H
V A,@R1 ;A=[60H]
MO R2,#61 ;R2=61H
V A,@R2 ;A=A+[61H]
AD R7,A ;R7=A
D
• If internal
MO RAM locations 60H=10H, and 61H=20H, then after the
operations
V of the above instructions R7=A=30H. The data contents of
memory locations 60H and 61H remain intact.
MOV DPTR, #data 16
• This instruction loads the data pointer with
the 16-bit constant and no flags are affected
• Example:
MO DPTR,#1032
V
• There are two forms of this instruction, the only difference between them is
whether to use an 8-bit or 16-bit indirect addressing mode to access the
external data RAM
• The 8-bit form of the MOVX instruction uses the EMI0CN SFR to determine the
upper 8 bits of the effective address to be accessed and the contents of R0 or
R1 to determine the lower 8 bits of the effective address to be accessed
• Example:
MOV EMI0CN,#10 ;Load high byte of
H ;address into EMI0CN.
MO R0,#34H ;Load low byte of
V ;address into R0(or R1).
MO A,@R ;Load contents of 1034H
VX 0 ;into ACC.
MOVX <dest-byte>,<source-byte>
• The 16-bit form of the MOVX instruction accesses the memory location
pointed to by the contents of the DPTR register
• Example:
MOV DPTR,#1034H ;Load DPTR with 16 bit
;address to read (1034H).
MO A,@DPT ;Load contents of 1034H
VX R ;into ACC.
• The above example uses the 16-bit immediate MOV DPTR instruction to set
the contents of DPTR
• Alternately, the DPTR can be accessed through the SFR registers DPH, which
contains the upper 8 bits of DPTR, and DPL, which contains the lower 8 bits of
DPTR
PUSH Direct
• This instruction increments the stack pointer (SP) by 1
• Example:
PUSH 22H
PUSH 23H
• Initially the SP points to memory location 4FH and the contents of memory
locations 22H and 23H are 11H and 12H respectively. After the above
instructions, SP=51H, and the internal RAM locations 50H and 51H will store
11H and 12H respectively.
POP Direct
• This instruction reads the contents of the internal RAM location addressed
by the stack pointer (SP) and decrements the stack pointer by 1. The data
read is then transferred to the Direct address which is an internal memory
or a SFR. No flags are affected.
• Example:
PO DP
P H
PO DP
• If SP=51H
P originally
L and internal RAM locations 4FH, 50H and 51H contain
the values 30H, 11H and 12H respectively, the instructions above leave
SP=4FH and DPTR=1211H
POP SP
• If the above line of instruction follows, then SP=30H. In this case, SP is
decremented to 4EH before being loaded with the value popped (30H)
XCH A,<byte>
• This instruction swaps the contents of ACC with
the contents of the indicated data byte
• Example:
XC A,@R
H 0
• The high order nibbles (bits 7-4) of both the registers remain the
same
• Example:
XCHD A,@R0
If R0=2EH, ACC=76H (01110110) and internal RAM location
2EH=F3H (11110011), the result of the instruction leaves RAM
location 2EH=F6H (11110110) and
ACC=73H (01110011)
Boolean Variable Instructions
Mnemonic Description
• The C8051F020 processor can
CLR C Clear C
perform single bit operations
CLR bit Clear direct bit
SETB C Set C
• The operations include set, clear, SETB bit Set direct bit
• Example:
CLR P2.7
If Port 2 has been previously written with DCH (11011100),
then the operation leaves the port set to 5CH (01011100)
SETB <bit>
• This operation sets the specified bit to 1
• Example:
SETB C
SETB P2.0
• If the carry flag is cleared and the output Port 2 has the
value of 24H (00100100), then the result of the instructions
sets the carry flag to 1 and changes the Port 2 value to 25H
(00100101)
CPL <bit>
• This operation complements the bit indicated by the operand
• Example:
CPL P2.1
CPL P2.2
• If Port 2 has the value of 53H (01010011) before the start of the
instructions, then after the execution of the instructions it leaves
the port set to 55H (01010101)
ANL C, <source-bit>
• This instruction ANDs the bit addressed with the Carry bit and stores the result in the
Carry bit itself
• If the source bit is a logical 0, then the instruction clears the carry flag; else the carry
flag is left in its original value
• If a slash (/) is used in the source operand bit, it means that the logical complement of
the addressed source bit is used, but the source bit itself is not affected
• Example:
MOV C,P2.0 ;Load C with input pin
;state of P2.0.
ANL C,P2.7 ;AND carry flag with
;bit 7 of P2.
MO P2.1,C ;Move C to bit 1 of Port 2.
V C,/OV ;AND with inverse of OV flag.
AN
• If P2.0=1,
L P2.7=0 and OV=0 initially, then after the above instructions,
P2.1=0, CY=0 and the OV remains unchanged, i.e. OV=0
ORLC, <source-bit>
• Thisinstruction ORs the bit addressed with the Carry bit and stores the result in the
Carry bit itself
• It sets the carry flag if the source bit is a logical 1; else the carry is left in its original
value
• If a slash (/) is used in the source operand bit, it means that the logical complementof
the addressed source bit is used, but the source bit itself is not affected
• Example:
MOV C,P2.0 ;Load C with input pin
;state of P2.0.
OR C,P2.7 ;OR carry flag with
L ;bit 7 of P2.
MO P2.1,C ;Move C to bit 1 of
;port 2.
V ;OR with inverse of OV
OR C,/OV
;flag.
L
MOV <dest-bit>,<source-bit>
• The instruction loads the value of source operand bit into the destination
operand bit
• One of the operands must be the carry flag; the other may be any directly-
addressable bit
• Example:
MOV P2.3,C
MOV C,P3.3
MOV P2.0,C
• If P2=C5H (11000101), P3.3=0 and CY=1 initially, then after the above
instructions, P2=CCH (11001100) and CY=0.
JC rel
• This instruction branches to the address, indicated by the label, if the carry
flag is set, otherwise the program continues to the next instruction
• Example:
CLR C
SUBB A,R0
C
JC ARRAY1
MOV A,#20H
• The carry flag is cleared initially. After the SUBB instruction, if the value of A is
smaller than R0, then the instruction sets the carry flag and causes program
execution to branch to ARRAY1 address, otherwise it continues to the MOV
instruction.
JNC rel
• This instruction branches to the address, indicated by the label, if the carry
flag is not set, otherwise the program continues to the next instruction
• Example:
CLR SUBB C
JNC MOV A,R0
ARRAY
2
A,#20H
• The above sequence of instructions will cause the jump to be taken if the
value of A is greater than or equal to R0. Otherwise the program will continue
to the MOV instruction.
JB <bit>,rel
• This instruction jumps to the address indicated if the
destination bit is 1, otherwise the program continues to the
next instruction
• Example:
JB ACC.7,ARRAY1
JB P1.2,ARRAY2
• Example:
JNB ACC.6,ARRAY1
JNB P1.3,ARRAY2
• Example:
JBC P1.3,ARRAY1
JBC P1.2,ARRAY2
CJNE Rn,#data,rel
Compare and Jump if Not Equal
NOP No Operation
ACALL addr11
• This instruction unconditionally calls a subroutine indicated by the address
• The operation will cause the PC to increase by 2, then it pushes the 16-bit PC
value onto the stack (low order byte first) and increments the stack pointer
twice
• The PC is now loaded with the value addr11 and the program execution
continues from this new location
• The subroutine called must therefore start within the same 2 kB block of the
program memory
• Example:
ACALL LOC_SUB
• If SP=07H initially and the label “LOC_SUB” is at program memory location
0567H, then executing the instruction at location 0230H, SP=09H, internal
RAM locations 08H and 09H will contain 32H
and 02H respectively and PC=0567H
•
LCALL addr16
This instruction calls a subroutine located at the indicated address
• The operation will cause the PC to increase by 3, then it pushes the 16-bit PC
value onto the stack (low order byte first) and increments the stack pointer
twice
• The PC is then loaded with the value addr16 and the program execution
continues from this new location
• Since it is a Long call, the subroutine may therefore begin anywhere in the full
64 kB program memory address space
• Example:
LCALLLOC_SUB
• Initially, SP=07H and the label “LOC_SUB” is at program memory location
2034H. Executing the instruction at location 0230H,
SP=09H, internal RAM locations 08H and 09H contain 33H
and 02H respectively and PC=2034H
RET
• This instruction returns the program from a subroutine
• RET pops the high byte and low byte address of PC from the
stack and decrements the SP by 2
• Example:
AJMP NEAR
• Example:
LJMP
FAR_AD
R
• If the label FAR_ADR is at program memory location 3456H, the
LJMP instruction at location 0120H loads the PC
with 3456H
SJMP rel
• This is a short jump instruction, which increments the PC by
2 and then adds the relative value ‘rel’ (signed 8-bit) to the
PC
• Example:
SJMP RELSRT
• Example:
MO DPTR, #LOOK_TBL
V @A + DPTR
LOOK_TBL: JMP LOC
AJM 0
P LOC
If the ACC=02H, execution jumps
AJM to LOC1
1
P LOC
• AJMP is a two byte instruction
AJM 2
P
JZ rel
• This instruction branches to the destination address if
ACC=0; else the program continues to the next
instruction
• The ACC is not modified and no flags are affected
• Example:
SUBB A,#20H
JZ LABEL1
DE A
C
• If ACC originally holds 20H and CY=0, then the SUBB
instruction changes ACC to 00H and causes the
program execution to continue at the instruction
identified by LABEL1; otherwise the program continues
to the DEC instruction
JNZ rel
• This instruction branches to the destination address if any
bit of ACC is a 1; else the program continues to the next
instruction
• Example:
DE A
C LABEL2
JNZ RO, A
MO
V
• If ACC originally holds 00H, then the instructions change
ACC to FFH and cause the program execution to continue at
the instruction identified by LABEL2; otherwise the
program continues to MOV instruction
CJNE <dest-byte>,<source-byte>,rel
• This instruction compares the magnitude of the dest-byte and the source-byte
and branches if their values are not equal
• The carry flag is set if the unsigned dest-byte is less than the unsigned integer
source-byte; otherwise, the carry flag is cleared
• Example:
CJNE R3,#50H,NEQ
U …
… ;R3 = 50H
NEQU: JC LOC1 ;If R3 < 50H
… … ;R7 > 50H
LOC1: … … ;R3 < 50H
DJNZ <byte>,<rel-addr>
• This instruction is ”decrement jump not zero”
• It decrements the contents of the destination location and if the resulting
value is not 0, branches to the address indicated by the source operand
• An original value of 00H underflows to FFH
• No flags are affected
• Example:
DJNZ 20H,LOC1
DJNZ 30H,LOC2
DJNZ40H,LOC3
• If internal RAM locations 20H, 30H and 40H contain the values 01H, 5FH and
16H respectively, the above instruction sequence will cause a jump to the
instruction at LOC2, with the values 00H, 5EH, and 15H in the 3 RAM locations.
– Note, the first instruction will not branch to LOC1 because the [20H] = 00H, hence
the program continues to the second instruction
– Only after the execution of the second instruction (where the
location [30H] = 5FH), then the branching takes place
• This is the no operation instructionN OP
• The instruction takes one machine cycle operation time
• Hence it is useful to time the ON/OFF bit of an output port
• Example:
CLR P1.2
NO
P
NO
P
NO
P
• The above
NO sequence of instructions outputs a low-going output pulse on bit 2
of PortP 1 lasting exactly 5 cycles.
– Note a simple SETB/CLR generates a 1 cycle pulse, so four additional cycles must be
SETB P1.2
inserted in order to have a 5-clock
pulse width
References
1. “The 8051 Microcontroller and Embedded Systems – using
assembly and C”, Muhammad Ali Mazidi and Janice
Gillespie Mazidi and Rollin D. McKinlay; PHI, 2006 /
Pearson, 2006.