0% found this document useful (0 votes)
31 views29 pages

MicroprocessorBasedSystems Term-II Lec4 BranchInstructionsandLooping

This document discusses branch instructions and looping in assembly language programming. It covers topics like DJNZ for decrementing a register and looping if it is not zero, nested loops using multiple registers, and conditional jumps. It also discusses subroutines, calling subroutines using LCALL and ACALL, returning from a subroutine using RET. Finally, it discusses using time delays in programming including using loops and NOP instructions.

Uploaded by

Abubakr Sherif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views29 pages

MicroprocessorBasedSystems Term-II Lec4 BranchInstructionsandLooping

This document discusses branch instructions and looping in assembly language programming. It covers topics like DJNZ for decrementing a register and looping if it is not zero, nested loops using multiple registers, and conditional jumps. It also discusses subroutines, calling subroutines using LCALL and ACALL, returning from a subroutine using RET. Finally, it discusses using time delays in programming including using loops and NOP instructions.

Uploaded by

Abubakr Sherif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

E1322

Microprocessor based Systems II

‘Branch instructions and looping’

Dr. Ahmed El-Awamry

Date: 04.03.2023, Time: 11:30


Objective

▪ Be able to program an assembly language


including branch instructions and itration
loops

© Dr. Ahmed El-Awamry 05.03.2023 2


Looping

▪ Repeating a sequence of instructions a


certain number of times is called a loop
▪ Loop action is performed by DJNZ reg, Label
▪ The register is decremented
▪ If it is not zero, it jumps to the target address referred to
by the label
▪ Prior to the start of loop the register is loaded with the
counter for the number of repetitions
▪ Counter can be R0 – R7 or RAM location

© Dr. Ahmed El-Awamry 05.03.2023 3


Looping

▪ If we want to repeat an action more times


than 256, we use a loop inside a loop, which
is called nested loop
▪ We use multiple registers to hold the count

© Dr. Ahmed El-Awamry 05.03.2023 4


Looping

The overall number


of iterations
= N0 × N1

Time delay nested loop


© Dr. Ahmed El-Awamry 05.03.2023 5
Conditional Jumps

▪ Jump only if a certain condition is met


JZ label ;jump if A=0
MOV A,R0 ;A=R0
JZ OVER ;jump if A = 0
MOV A,R1 ;A=R1
JZ OVER ;jump if A = 0
...
OVER:

© Dr. Ahmed El-Awamry 05.03.2023 6


Conditional Jumps
JNC label ;jump if no carry, CY=0
▪ If CY = 0, the CPU starts to fetch and execute
instruction from the address of the label
▪ If CY = 1, it will not jump but will execute the next
instruction below JNC

Ex. Draw the


flowchart?

© Dr. Ahmed El-Awamry 05.03.2023 7


Conditional Jumps

▪ All conditional jumps are short jumps


▪ The address of the target must within -128 to
+127 bytes of the contents of PC
© Dr. Ahmed El-Awamry 05.03.2023 8
Unconditional Jumps

▪ The unconditional jump is a jump in which


control is transferred unconditionally to the
target location
LJMP (long jump)
▪ 3-byte instruction
▪ First byte is the opcode
▪ Second and third bytes represent the 16-bit target address
▪ Any memory location from 0000 to FFFFH
SJMP (short jump)
▪ 2-byte instruction
▪ First byte is the opcode
▪ Second byte is the relative target address
▪ 00 to FFH (forward +127 and backward -128 bytes
from the current PC)
© Dr. Ahmed El-Awamry 05.03.2023 9
Calculating Short Jump Address

▪ To calculate the target address of a short


jump (SJMP, JNC, JZ, DJNZ, etc.)
▪ The second byte is added to the PC of the
instruction immediately below the jump
▪ If the target address is more than -128 to
+127 bytes from the address below the short
jump instruction
▪ The assembler will generate an error stating the
jump is out of range

© Dr. Ahmed El-Awamry 05.03.2023 10


Calculating Short Jump Address

© Dr. Ahmed El-Awamry 05.03.2023 11


Call Instructions

▪ Call instruction is used to call subroutine


▪ Subroutines are often used to perform tasks that
need to be performed frequently
▪ This makes a program more structured in addition
to saving memory space
LCALL (long call)
▪ 3-byte instruction
▪ First byte is the opcode
▪ Second and third bytes are used for address of target
subroutine
▪ Subroutine is located anywhere within 64K byte
address space
ACALL (absolute call)
▪ 2-byte instruction
▪ 11 bits are used for address within 2K-byte
© Dr. Ahmed El-Awamry 05.03.2023
range 12
LCALL Instruction

▪ When a subroutine is called, control is


transferred to that subroutine, the processor
▪ Saves on the stack the the address of the
instruction immediately below the LCALL
▪ Begins to fetch instructions form the new location
▪ After finishing execution of the subroutine
▪ The instruction RET transfers control back to the
caller
▪ Every subroutine needs RET as the last instruction

© Dr. Ahmed El-Awamry 05.03.2023 13


LCALL Instruction

© Dr. Ahmed El-Awamry 05.03.2023 14


LCALL Instruction

© Dr. Ahmed El-Awamry 05.03.2023 15


LCALL Instruction and Stack

© Dr. Ahmed El-Awamry 05.03.2023 16


LCALL Instruction & PUSH POP

© Dr. Ahmed El-Awamry 05.03.2023 17


Calling Subroutines

© Dr. Ahmed El-Awamry 05.03.2023 18


ACALL

▪ The only difference between ACALL and


LCALL is
▪ The target address for LCALL can be anywhere
within the 64K byte address
▪ The target address of ACALL must be within a 2K-
byte range
▪ The use of ACALL instead of LCALL can save
a number of bytes of program ROM space

© Dr. Ahmed El-Awamry 05.03.2023 19


ACALL

© Dr. Ahmed El-Awamry 05.03.2023 20


Time Delay

▪ CPU executing an instruction takes a certain


number of clock cycles
▪ These are referred as to as machine cycles
▪ The length of machine cycle depends on the
frequency of the crystal oscillator connected
to 8051
▪ In original 8051, one machine cycle lasts 12
oscillator periods

© Dr. Ahmed El-Awamry 05.03.2023 21


Time Delay

© Dr. Ahmed El-Awamry 05.03.2023 22


Time Delay

© Dr. Ahmed El-Awamry 05.03.2023 23


Time Delay

© Dr. Ahmed El-Awamry 05.03.2023 24


Time Delay, Large

© Dr. Ahmed El-Awamry 05.03.2023 25


Time Delay

▪ Two factors can affect the accuracy of the


delay
▪ Crystal frequency
▪ The duration of the clock period of the machine cycle is a
function of this crystal frequency
▪ 8051 design
▪ 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

© Dr. Ahmed El-Awamry 05.03.2023 26


Delay Calculation for Other 8051

© Dr. Ahmed El-Awamry 05.03.2023 27


Delay Calculation for Other 8051

© Dr. Ahmed El-Awamry 05.03.2023 28


Appendix

You might also like