You are on page 1of 28

CHAPTER 5 ASSEMBLY LANGUAGE:

DECISIONS, COMPARE AND


CONDITIONAL BRANCHES
5.1 UNDERSTANDING BRANCH
INSTRUCTION 5.3 Apply in line assembler in C
5.1.1 Elaborate basic branch to perform the branches
instruction.
5.1.2 Elaborate call function technique
task.
5.3.1 Draw the flowchart for a processing
branches problem.
5.2 Understanding Flag in APSR
5.3.2 Apply assembly-level structured
programming techniques using flag
5.2.1 Explain flag bit in APSR that can be condition and branches.
used for conditional branches.
5.3.3 Apply assembly-level structured
5.2.2 Explain combination of the flag to programming techniques using
create branches conditions. compare and branches.

5.2.3 Explain a compare with zero and


conditional branches operation.
5.1.1 Elaborate basic branch instruction.

➔ A branch instruction changes the flow of


execution or is used to call a routine
5.1.1 Elaborate basic branch instruction.

➔Two types of branching

(A) (B)
Unconditional Conditional
branching branching
Instruction: Instruction:
BEQ BGE BCS
B - branch BNE BGT BCC
BL – branch link BLE BMI BVS
BLT BPL BVC
(A)
Unconditional branching

Instruction:
B – branch BL – branch link

Syntax:
B label BL label

➔ these instructions cause a branch to label (a PC-relative expression)


(A)
Unconditional branching
Instruction:
B – branch BL – branch link
5.1.2 Elaborate call function technique
BL – branch link

subroutine ➔ a sequence of instructions that performs a specific task


➔ a set of instructions designed to perform a frequently used
operation within a program.
5.2 Understanding Flag in APSR
Conditional branching

➔ change the flow of execution with a


condition

Instruction:
BEQ BGE BCS
BNE BGT BCC
BLE BMI BVS
BLT BPL BVC
5.2 Understanding Flag in APSR
Example:

❖ BCS add_again ; goto to label add_again when


carry = 1
CMP R3, #0
SUBGT R0,R1,R2 ; perform R0=R1-R2 when (R3) is
greater than #0 (Z=0 & N=V)
ADDLE R4,R5,R6 ; perform R4=R5+R6 when (R3) is
lesser than or equal #0
(Z=1 & N!=V)
Example:

CMP R3, #3
BGT mul_5 ;goto mul_3 (label) when
(R3) is greater than #3
(Z=0 & N=V)
DIVS R0,R2,R1 ;if not, perform
R0=R2/R1
B END
mul_5 MULS R0,R2,R1 ;perform R0=R2*R1
END
Try It Yourself

1. Write the programming code to inspect the


content of R4 and if the content is greater than
0x55, divide the value with 0x5 and store the result
at register R5. Otherwise, multiply it with 0x3 and
store the result in register R6.
Try It Yourself Answer:
5.3 Apply in line assembler in C to perform the
branches task.

Structured Programming Techniques


Selection Structure
Example:
Try It Yourself

1. Draw the flow chart and write the programming


code to inspect the content of R1 and R2. If the
content of R1 is same as content of R2, multiply
the content of R1 with 0xA and store the result at
register R3. Otherwise, divide the content of R2
with 0x3 and store the result in register R4.
Try It Yourself

1. Draw the flow chart and write the programming


code to inspect the content of R4 and R5. If the
content of R4 is lesser than content of R5, subtract
the content of R4 with 0x1 and store the result at
register R3. Otherwise, add the content of R4 with
0x3 and store the result in register R6.
Iteration @ Looping Structure

3 types of Iteration structure:

i. for ➔ numbers of loop has been determined

ii. while number of loops


has/hasn’t been
iii. do..while determined
i. for loop
Assembly codes:
Syntax in C:
Initial Test Increment
counter counter counter
Sum = 0;
for( i=0 ; i<=10 ; i++)
{
Sum=sum+i;
}
ii. do..while loop
Example (Lab work 4):
Try It Yourself

Write a assembly program to solve the algebraic


problem below. Then, draw the flow chart to
express the problem:
15
2 - 163
START
Answer
SET R0➔2
SET R1➔2
MOVS R0, #0x2 ;multiplication by 2 SET R2➔E
MOVS R1, #0x2 ;base 2
R0 = R1 X R0
MOVS R2, #0xE ;14 times of looping process X
(2 )

loop_x MULS R0,R1,R0 ;R0 = R1 X R0


SUBS R2, #1 ;R2=R2 – 1 ➔ looping process R2 - 1
BNE loop_x ;loops for 14 times
SUBS R0, R0,#0xA3 ; 215 - 163 NO
14 X
END LOOPS?

YES
15
2 - 163

END
Try It Yourself

Write a assembly program to solve the algebraic


problems below. Then, draw the flow chart to
express the problem:
12
i. 4 – 890
20 13
ii. 5 +6
Try It Yourself

Write a assembly
program to solve the
algebraic problem
below. Then, draw the
flow chart to express
the problem:
THE END

You might also like