Lecture-2: Jump, Loop and Call Instructions
Lecture-2: Jump, Loop and Call Instructions
Write a program to (a) load the accumulator with the value 55H, and
(b) complement the ACC 700 times
MOV A,#55H ;A=55H
MOV R3,#10 ;R3=10, outer loop count
Solution:
11.0592/12 = 921.6 kHz;
machine cycle is 1/921.6 kHz = 1.085μs
For 8051 system of 11.0592 MHz, find how long it takes to execute
TIME DELAY each instruction.
(a) MOV R3,#55 (b) DEC R3 (c) DJNZ R2 target
FOR VARIOUS (d) LJMP (e) SJMP (f) NOP (g) MUL AB
8051 CHIPS
(cont’) Solution:
Machine cycles Time to execute
(a) 1 1x1.085μs = 1.085μs
(b) 1 1x1.085μs = 1.085μs
(c) 2 2x1.085μs = 2.17μs
(d) 2 2x1.085μs = 2.17μs
(e) 2 2x1.085μs = 2.17μs
(f) 1 1x1.085μs = 1.085μs
(g) 4 4x1.085μs = 4.34μs
Find the size of the delay in following program, if the crystal
TIME DELAY frequency is 11.0592MHz.
FOR VARIOUS
MOV A,#55H
8051 CHIPS AGAIN: MOV P1,A
ACALL DELAY
Delay CPL A
SJMP AGAIN
Calculation ;---time delay-------
A simple way to short jump
to itself in order to keep the
DELAY: MOV R3,#200 microcontroller busy
HERE: DJNZ R3,HERE HERE: SJMP HERE
RET We can use the following:
SJMP $
Solution:
Machine cycle
DELAY: MOV R3,#200 1
HERE: DJNZ R3,HERE 2
RET 2
Therefore, [(200x2)+1+2]x1.085μs = 436.255μs.
Find the size of the delay in following program, if the crystal
TIME DELAY frequency is 11.0592MHz.
FOR VARIOUS
Machine Cycle
8051 CHIPS
DELAY: MOV R3,#250 1
HERE: NOP 1
Increasing NOP 1
NOP 1
Delay Using
NOP 1
NOP DJNZ R3,HERE 2
RET 2
Solution:
The time delay inside HERE loop is
[250(1+1+1+1+2)]x1.085μs = 1627.5μs.
Adding the two instructions outside loop we
have 1627.5μs + 3 x 1.085μs = 1630.755μs
Find the size of the delay in following program, if the crystal
TIME DELAY frequency is 11.0592MHz.
FOR VARIOUS Machine Cycle
8051 CHIPS DELAY: MOV R2,#200 1
Notice in nested loop,
AGAIN: MOV R3,#250 1
HERE: NOP 1 as in all other time
Large Delay NOP 1 delay loops, the time
Using Nested DJNZ R3,HERE 2 is approximate since
we have ignored the
Loop DJNZ R2,AGAIN 2
first and last
RET 2
instructions in the
Solution: subroutine.
For HERE loop, we have (4x250)x1.085μs=1085μs.
For AGAIN loop repeats HERE loop 200 times, so
we have 200x1085μs=217000μs. But “MOV
R3,#250” and “DJNZ R2,AGAIN” at the start and
end of the AGAIN loop add (3x200x1.805)=651μs.
As a result we have 217000+651=217651μs.
Two factors can affect the accuracy
TIME DELAY of the delay
FOR VARIOUS
Crystal frequency
8051 CHIPS The duration of the clock period of the
machine cycle is a function of this crystal
Delay frequency
Calculation for 8051 design
Other 8051 The original machine cycle duration was set
at 12 clocks
Advances in both IC technology and CPU
design in recent years have made the 1-
clock machine cycle a common feature
Clocks per machine cycle for various 8051 versions
Chip/Maker Clocks per Machine Cycle
AT89C51 Atmel 12
P89C54X2 Philips 6
DS5000 Dallas Semi 4
Port 0
33
32
P0.6(AD6)
8051 P0.2
8051 P0.7(AD7)
P0.3
(8031) P0.4
P0.5
P0.6
P0.7
In order to make port 0 an input, the
I/O
port must be programmed by writing
PROGRAMMING
1 to all the bits
Port 0 as Input Port 0 is configured first as an input port by writing 1s to it, and then
data is received from that port and sent to P1
MOV A,#0FFH ;A=FF hex
MOV P0,A ;make P0 an i/p port
;by writing it all 1s
39
38
P0.0(AD0) BACK: MOV A,P0 ;get data from P0
P0.1(AD1)
37
36
P0.2(AD2)
P0.3(AD3) MOV P1,A ;send it to port 1
35 P0.4(AD4)
34 P0.5(AD5)
P0.6(AD6)
SJMP BACK ;keep doing it
33
32 P0.7(AD7)
8051
(8031)
To make port 1 an input port, it
I/O must be programmed as such by
PROGRAMMING writing 1 to all its bits
Port 1 is configured first as an input port by writing 1s to it, then data
Port 1 as Input is received from that port and saved in R7 and R5
MOV A,#0FFH ;A=FF hex
MOV P1,A ;make P1 an input port
P1.0
Instructions that are used for single-
I/O BIT
MANIPULATION
bit operations are as following
PROGRAMMING Single-Bit Instructions
Instruction Function
I/O Ports SETB bit Set the bit (bit = 1)
and Bit CLR bit Clear the bit (bit = 0)
Addressability CPL bit Complement the bit (bit = NOT bit)
(cont’) JB bit, target Jump to target if bit = 1 (jump if bit)
JNB bit, target Jump to target if bit = 0 (jump if no bit)
JBC bit, target Jump to target if bit = 1, clear
bit (jump if bit, then clear)
The JNB and JB instructions are
I/O BIT widely used single-bit operations
MANIPULATION
They allow you to monitor a bit and make
PROGRAMMING a decision depending on whether it’s 0 or 1
These two instructions can be used for any
Checking an bits of I/O ports 0, 1, 2, and 3
Input Bit Port 3 is typically not used for any I/O,
either single-bit or byte-wise
Instructions for Reading an Input Port
Checking an Solution:
SETB P1.7 ;make P1.7 an input
Input Bit AGAIN: JB P1.2,OVER ;jump if P1.7=1
(cont’) MOV P2,#‟N‟ ;SW=0, issue „N‟ to P2
ORG 200H
MYDATA: DB “America”
… … …
Special Function Register (SFR) Addresses
Symbol Name Address
ACCESSING TMOD Timer/counter mode control 89H
MEMORY TCON* Timer/counter control 88H
* Bit addressable
Example 5-1
ACCESSING
MEMORY Write code to send 55H to ports P1 and P2, using
(a) their names (b) their addresses
Solution:
PUSH 05 ;push R5 onto stack
PUSH 0E0H ;push register A onto stack
BIT 2F
2E
7F
77
7E
76
7D
75
7C
74
7B
73
7A
72
79
71
78
70
ADDRESSES 2D 6F 6E 6D 6C 6B 6A 69 68
2C 67 66 65 64 63 62 61 60
Bit-addressable 2B 5F 5E 5D 5C 5B 5A 59 58
Bit- locations 2A 57 56 55 54 53 52 51 50
Addressable 29
28
4F
47
4E
46
4D
45
4C
44
4B
43
4A
42
49
41
48
40
RAM 27 3F 3E 3D 3C 3B 3A 39 38
I/O Port
Bit Addresses
(cont’)
Only registers A, B, PSW, IP, IE,
BIT ACC, SCON, and TCON are bit-
ADDRESSES addressable
While all I/O ports are bit-addressable
Registers
Bit- In PSW register, two bits are set
Addressability aside for the selection of the
register banks
Upon RESET, bank 0 is selected
We
CY can
AC select
-- any
RS1 other
RS0 banks
OV using
-- the
P
CY AC -- RS1 RS0 OV -- P
Example 5-18
While a program to save the status of bit P1.7 on RAM address bit 05.
Solution:
MOV C,P1.7
MOV 05,C
The BIT directive is a widely used
BIT
ADDRESSES
directive to assign the bit-
addressable I/O and RAM locations
Using BIT Allow a program to assign the I/O or RAM
bit at the beginning of the program,
making it easier to modify them
Example 5-22
A switch is connected to pin P1.7 and an LED to pin P2.0. Write a
program to get the status of the switch and send it to the LED.
Solution:
LED BIT P1.7 ;assign bit
SW BIT P2.0 ;assign bit
HERE: MOV C,SW ;get the bit from the port
MOV LED,C ;send the bit to the port
SJMP HERE ;repeat forever
Example 5-20
BIT
Assume that bit P2.3 is an input and represents the condition of an
ADDRESSES oven. If it goes high, it means that the oven is hot. Monitor the bit
continuously. Whenever it goes high, send a high-to-low pulse to port
P1.5 to turn on a buzzer.
Using BIT
Solution:
(cont’) OVEN_HOT BIT P2.3
BUZZER BIT P1.5
HERE: JNB OVEN_HOT,HERE ;keep monitoring
ACALL DELAY
CPL BUZZER ;sound the buzzer
ACALL DELAY
SJMP HERE
Use the EQU to assign addresses
BIT
ADDRESSES Defined by names, like P1.7 or P2
Defined by addresses, like 97H or 0A0H
Using EQU Example 5-24
A switch is connected to pin P1.7. Write a program to check the status
of the switch and make the following decision.
(a) If SW = 0, send “0” to P2
(b) If SW = 1, send “1“ to P2
Write a program to add two 16-bit numbers. Place the sum in R7 and
R6; R6 should have the lower byte.
Solution:
CLR C ;make CY=0
MOV A, #0E7H ;load the low byte now
A=E7H
ADD A, #8DH ;add the low byte
MOV R6, A ;save the low byte sum in
R6
MOV A, #3CH ;load the high byte
ADDC A, #3BH ;add with the carry
MOV R7, A ;save the high byte sum
The binary representation of the
ARITHMETIC digits 0 to 9 is called BCD (Binary
INSTRUCTIONS
Coded Decimal) Digit BCD
Packed BCD
In packed BCD, a single byte
has two BCD number in it, one
in the lower 4 bits, and one in
the upper 4 bits
Ex. 0101 1001 is packed BCD
for 59H
ARITHMETIC Adding two BCD numbers must give
INSTRUCTIONS a BCD result Adding these two
numbers gives
Unpacked and MOV A, #17H 0011 1111B (3FH),
ADD A, #28H Which is not BCD!
Packed BCD
The result above should have been 17 + 28 = 45 (0100 0101).
To correct this problem, the programmer must add 6 (0110) to the
low digit: 3F + 06 = 45H.
DA A ;decimal adjust for addition
ARITHMETIC The DA instruction is provided to
INSTRUCTIONS correct the aforementioned problem
DA Instruction
associated with BCD addition
The DA instruction will add 6 to the lower
nibble or higher nibble if need
Example: 6CH
MOV A,#47H ;A=47H first BCD operand
MOV B,#25H ;B=25H second BCD operand
ADD A,B ;hex(binary) addition(A=6CH)
DA A ;adjust for BCD addition
(A=72H)
72H
DA works only
after an ADD, The “DA” instruction works only on A. In other word, while the source
but not after INC can be an operand of any addressing mode, the destination must be in
register A in order for DA to work.
Summary of DA instruction
ARITHMETIC After an ADD or ADDC instruction
INSTRUCTIONS
1. If the lower nibble (4 bits) is greater than 9,
or if AC=1, add 0110 to the lower 4 bits
DA Instruction 2. If the upper nibble is greater than 9, or
(cont’) if CY=1, add 0110 to the upper 4 bits
+ 6 + 0110
47 Since AC=10111
0100 after the
addition, ”DA A” will add 6 to the
lower nibble.
The final result is in BCD format.
Assume that 5 BCD data items are stored in RAM locations starting
at 40H, as shown below. Write a program to find the sum of all the
ARITHMETIC
numbers. The result must be in BCD.
INSTRUCTIONS
40=(71)
41=(11)
42=(65)
DA Instruction 43=(59)
(cont’) 44=(37)
Solution:
MOV R0,#40H ;Load pointer
MOV R2,#5 ;Load counter
CLR A ;A=0
MOV R7,A ;Clear R7
AGAIN: ADD A,@R0 ;add the byte pointer
;to by R0
DA A ;adjust for BCD
JNC NEXT ;if CY=0 don‟t
;accumulate carry
INC R7 ;keep track of carries
NEXT: INC R0 ;increment pointer
DJNZ R2,AGAIN ;repeat until R2 is 0
In many microprocessor there are
ARITHMETIC
two different instructions for
INSTRUCTIONS
subtraction: SUB and SUBB
Subtraction of (subtract with borrow)
Unsigned In the 8051 we have only SUBB
Numbers The 8051 uses adder circuitry to perform
the subtraction
SUBB A,source ;A = A – source – CY
To make SUB out of SUBB, we have
to make CY=0 prior to the execution
of the instruction
Notice that we use the CY flag for the
borrow
SUBB when CY = 1
ARITHMETIC
This instruction is used for multi-byte
INSTRUCTIONS
numbers and will take care of the borrow
of the lower operand
Subtraction of
A = 62H – 96H – 0 = CCH
Unsigned CLR C CY = 1
Numbers MOV A,#62H ;A=62H
(cont’) SUBB A,#96H ;62H-96H=CCH with CY=1
MOV R7,A ;save the result
MOV A,#27H ;A=27H
SUBB A,#12H ;27H-12H-1=14H
MOV R6,A ;save the result
A = 27H - 12H - 1 = 14H
Solution: CY = 0
We have 2762H - 1296H = 14CCH.
The 8051 supports byte by
ARITHMETIC
byte multiplication only
INSTRUCTIONS
The byte are assumed to be unsigned data
MUL AB ;AxB, 16-bit result in B, A
Unsigned MOV A,#25H ;load 25H to reg. A
Multiplication MOV B,#65H ;load 65H to reg. B
MUL AB ;25H * 65H = E99 where
;B = OEH and A = 99H
CY is always 0
If B 0, OV = 0
If B = 0, OV = 1 indicates error
(a) Write a program to get hex data in the range of 00 – FFH from
port 1 and convert it to decimal. Save it in R7, R6 and R5.
ARITHMETIC (b) Assuming that P1 has a value of FDH for data, analyze program.
INSTRUCTIONS Solution: MOV A,#0FFH
MOV P1,A ;make P1 an input port
(a)
Application for MOV
MOV
A,P1
B,#10
;read data from P1
;B=0A hex
DIV DIV AB ;divide by 10
MOV R7,B ;save lower digit
MOV B,#10
DIV AB ;divide by 10 once more
Complement
ANL destination,source ;dest = dest AND source
MSB LSB
CLR C ;make CY = 0
MOV A,#26H ;A = 0010 0110
RRC A ;A = 0001 0011 CY = 0
RRC A ;A = 0000 1001 CY = 1
RRC A ;A = 1000 0100 CY = 1
RLC A ;rotate left through carry
ROTATE
INSTRUCTION In RLC A
AND DATA Bits are shifted from right to left
SERIALIZATION They exit the MSB and enter the carry flag,
and the carry flag enters the LSB
Rotating
through Carry
(cont’) CY MSB LSB
RL A ;A = 0111 0010
RL A ;A = 0111 0010
RL A ;A = 0111 0010
RL A ;A = 0111 0010
BCD AND ASCII ASCII code and BCD for digits 0 - 9
APPLICATION
PROGRAMS Key ASCII (hex) Binary BCD (unpacked)
0 30 011 0000 0000 0000
1 31 011 0001 0000 0001
2 32 011 0010 0000 0010
3 33 011 0011 0000 0011
4 34 011 0100 0000 0100
5 35 011 0101 0000 0101
6 36 011 0110 0000 0110
7 37 011 0111 0000 0111
8 38 011 1000 0000 1000
9 39 011 1001 0000 1001
To convert ASCII to packed BCD
BCD AND ASCII
APPLICATION It is first converted to unpacked BCD (to
PROGRAMS get rid of the 3)
Combined to make packed BCD
ASCII to
key ASCII Unpacked BCD Packed BCD
Packed BCD
Conversion 4 34 0000 0100
7 37 0000 0111 0100 0111 or 47H
SWAP A ;A=40H
ORL A, R1 ;A=47H, packed BCD
Assume that register A has packed BCD, write a program to convert
BCD AND ASCII packed BCD to two ASCII numbers and place them in R2 and R6.
APPLICATION MOV A,#29H ;A=29H, packed BCD
PROGRAMS MOV R2,A ;keep a copy of BCD data
ANL A,#0FH ;mask the upper nibble (A=09)
ASCII to ORL A,#30H ;make it an ASCII, A=39H(„9‟)
MOV R6,A ;save it
Packed BCD MOV A,R2 ;A=29H, get the original
Conversion data
(cont’) ANL A,#0F0H ;mask the lower nibble
RR A ;rotate right
RR A ;rotate right
RR A ;rotate right SWAP A
RR A ;rotate right
ORL A,#30H ;A=32H, ASCII char. ‟2‟
MOV R2,A ;save ASCII char in R2
Assume that the lower three bits of P1 are connected to three
switches. Write a program to send the following ASCII characters
to P2 based on the status of the switches.
BCD AND ASCII
APPLICATION 000 „0‟
001 „1‟
PROGRAMS 010 „2‟
011 „3‟
Using a Look- 100 „4‟
101 „5‟
up Table for 110 „6‟
ASCII 111 „7‟
MOV DPTR,#MYTABLE
Solution:
MOV A,P1 ;get SW status
ANL A,#07H ;mask all but lower 3
MOVC A,@A+DPTR ;get data from table
MOV P2,A ;display value
SJMP $ ;stay here
;------------------
ORG 400H
MYTABLE DB „0‟,„1‟,„2‟,„3‟,„4‟,„5‟,„6‟,„7‟
END