0% found this document useful (0 votes)
61 views

Microprocessor Assembly Language Programming: 2.7 Loop and Branch

The document discusses microprocessor assembly language programming and concepts such as branching, looping, comparisons, and time delay. It provides examples of assembly code to demonstrate conditional jumps, unconditional jumps, looping, comparing values, and subroutines. It also explains how to calculate time delays for delay subroutines based on the clock cycle counts of the instructions used.

Uploaded by

Mohamad Azim
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)
61 views

Microprocessor Assembly Language Programming: 2.7 Loop and Branch

The document discusses microprocessor assembly language programming and concepts such as branching, looping, comparisons, and time delay. It provides examples of assembly code to demonstrate conditional jumps, unconditional jumps, looping, comparing values, and subroutines. It also explains how to calculate time delays for delay subroutines based on the clock cycle counts of the instructions used.

Uploaded by

Mohamad Azim
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/ 22

Chapter 2:

Microprocessor Assembly
Language Programming

2.7
Loop and Branch
Branching

Program Branching is caused when JUMP


instruction is executed
It directs the program flow to the immediate
destination address
Two kinds of Jump instructions;
 CONDITIONAL JUMP
 UNCONDITIONAL JUMP
CONDITIONAL JUMP

a jump to the target location in which the control


of the jump is valid only if a condition is met,
e.g. the status of Carry Flag, result of comparison,
etc.
Looping
 Repeating a sequence of instructions or an operation a certain number of
times is called a loop.
 Example

Repeat 9+9 six times


Looping
 Using BNE,
Comparison of unsigned numbers
 The CMP instruction compares two operands and changes the flags according
to the result of the comparison.
 The CMP instruction has the following format:

CMP Rn, Op2 ; Compare Rn with Op2 and set


the flags
Conditional Branch
 Conditional branch for increment counter
 Example,

 write a program to inspect data in R2. If it is equal, save


answer in R3. Otherwise save in R7.
Unconditional Branch

 The unconditional branch is a jump in which control is transferred


unconditionally to the target location
 In cases where there is no operating system or monitor program, we use the
Branch to itself in order to keep the microcontroller busy.

 Can jump anywhere

All branch is short jump


size 32M or 24 bit. To
jump over 32M can use
BX
EXAMPLE 1
write a program to inspect data in R2 with value
10 . If it is equal, copy data R2 into R3. Otherwise
copy the data into R7.
SOLUTION
AREA MY_PROG, CODE, READONLY
EXPORT main
ENTRY
main MOV R0, #10
CMP R2, R0
BEQ SAMA
MOV R7, R2
B HABIS
SAMA MOV R3, R2
HABIS NOP
B HABIS
END
EXAMPLE 2
 Let Data1 = 0x689A and Data2 = 0x1234. Write a
program for STM32F446RE that will subtract Data2
from Data1. If Data1 is bigger than Data2, let the
content of memory 0X40040000 to be 0xFF, else
let it be 0x00. Draw a flowchart for your program.
SOLUTION 1
AREA MY_PROG, CODE, READONLY
EXPORT main
ENTRY
main MOVW R0,#0x689A
MOVW R1,#0x1234
LDR R2,=0x40040000
SUBS R0, R0, R1
BCS ADA
MOV R3,#0x00
B LOMPAT
ADA MOV R3,#0xFF
LOMPAT STRB R3,[R2]
HABIS NOP
B HABIS
END
EXAMPLE 3
 Write a program using assembly language for
STM32F446RE microcontroller that will add the
content in memory location 0x2001C000 until
0x2001C004. Draw a flowchart for your program
SOLUTION 2
AREA MY_PROG, CODE, READONLY
EXPORT main
ENTRY
main MOV R0,#5; //SET COUNTER = 5
LDR R1,=0x2001C000 //SET STARTING ADDRESS
MOV R2,#0 //CLEAR R2
LAGI LDRB R3, [R1],#1// COPY CONTENT OF MEMORY POINTED BY R1 TO R3 THEN R1 = R1+1
ADD R2,R2,R3 //R2 = R2+R3
SUB R0,R0,#1// DECREASE COUNTER, R0 = R0-1
CMP R0,#0 // IS R0 = 0?
BNE LAGI //IF NOT 0, JUMP TO LABEL LAGI
HABIS B HABIS
END
Subroutine

 BL (branch and link) instruction, used to call a subroutine.


 To return from subroutine used BX LR. LR is (R14)

Can also used BX R14,


MOV PC,LR
Chapter 2:
Microprocessor Assembly
Language Programming

2.8
Time Delay
Time Delay

 Time for CPU to execute an instruction is called machine cycle.


 Execution time = 1/f x machine cycle
 For delay subroutine, the time delay is calculated as follows; (let CPU
frequency = 100MHz)
 Time Delay= [1+((1+1+3)*255)+1] x 1/ 100M =13
EXAMPLE
Calculate the following delay program. Clock frequency is
16MHz.
CLOCK CYCLE
MOV R0, =30 1
DELAY LDR R1,=5325 2
DL1 SUBS R1, R1, #1 1
BNE DL1 3
SUBS R0,R0,#1 1
BNE DELAY 3
BX LR 3
THANK YOU

You might also like