Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views9 pages

Lecture 5

The document discusses decision making and conditional branch instructions in ARM assembly language. It provides examples of how if and while statements from C code would compile to ARM instructions using conditional branches, compares, and jumps. It also covers basic blocks, condition codes, signed vs unsigned comparisons, and branch addressing modes.

Uploaded by

zeinaakhaled1
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)
7 views9 pages

Lecture 5

The document discusses decision making and conditional branch instructions in ARM assembly language. It provides examples of how if and while statements from C code would compile to ARM instructions using conditional branches, compares, and jumps. It also covers basic blocks, condition codes, signed vs unsigned comparisons, and branch addressing modes.

Uploaded by

zeinaakhaled1
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/ 9

COMPUTER ORGANIZATION AND DESIGN ARM

Edition
The Hardware/Software Interface

Chapter 2
Instructions: Language
of the Computer

Lecture 5: ECE4206 Intro to Microprocessors


§2.7 Instructions for Making Decisions
Decision Making Instructions
◼ Decision-making is commonly represented in programming
languages using the if statement, sometimes combined with go-to
statements and labels. LEGv8 assembly language includes two
decision-making instructions called conditional branches.
◼ Branch to a labeled instruction if a condition is true
◼ Otherwise, continue sequentially

◼ CBZ register, L1 // CBZ stands for compare and branch if zero


◼ if (register == 0) branch to instruction labeled L1;

◼ CBNZ register, L1 // CBNZ stands for compare and branch if not zero
◼ if (register != 0) branch to instruction labeled L1;

◼ B L1
◼ branch unconditionally to instruction labeled L1;

Chapter 2 — Instructions: Language of the Computer — 2


Compiling If Statements
◼ C code:
if (i==j) f = g+h;
else f = g-h;
◼ i, j, … in X22, X23, …
◼ g,h …. In X20, X21

◼ Compiled LEGv8 code:


SUB X9,X22,X23 // X9 = i − j
CBNZ X9,Else // go to Else if i ≠ j (X9 ≠ 0)
ADD X19,X20,X21 // f = g + h
B Exit
Else: SUB X9,X22,x23 // f = g − h
Exit: …
Assembler calculates addresses
Chapter 2 — Instructions: Language of the Computer — 3
Compiling Loop Statements
◼ C code:
while (save[i] == k) i += 1;
◼ i in x22, k in x24, address of save in x25
◼ Compiled LEGv8 code:
Loop: LSL X10,X22,#3 // Temp reg X10 = i * 8
ADD X10,X10,X25 // X10 = address of save[i]
LDUR X9,[X10,#0] // Temp reg X9 = save[i]
SUB X11,X9,X24 // X11 = save[i] − k
CBNZ X11,Exit // go to Exit if save[i] ≠ k(X11 ≠ 0)
ADDI X22,X22,#1 // i = i + 1
B Loop
Exit: …
Chapter 2 — Instructions: Language of the Computer — 4
Basic Blocks
◼ A basic block is a sequence of instructions
with
◼ No embedded branches (except at end)
◼ No branch targets (except at beginning)

◼ A compiler identifies basic


blocks for optimization
◼ An advanced processor
can accelerate execution
of basic blocks

Chapter 2 — Instructions: Language of the Computer — 5


More Conditional Operations
◼ Condition codes, set from arithmetic instruction with S-
suffix (ADDS, ADDIS, ANDS, ANDIS, SUBS, SUBIS)
◼ negative (N): result had 1 in MSB
◼ zero (Z): result was 0
◼ overlow (V): result overflowed
◼ carry (C): result had carryout from MSB
◼ Use subtract to set flags, then conditionally branch:
◼ B.EQ
◼ B.NE
◼ B.LT (less than, signed), B.LO (less than, unsigned)
◼ B.LE (less than or equal, signed), B.LS (less than or equal, unsigned)
◼ B.GT (greater than, signed), B.HI (greater than, unsigned)
◼ B.GE (greater than or equal, signed),
◼ B.HS (greater than or equal, unsigned)

Chapter 2 — Instructions: Language of the Computer — 6


Conditional Example
◼ if (a > b) a += 1;
◼ a in X22, b in X23

SUBS X9,X22,X23 // use subtract to make comparison


B.LTE Exit // conditional branch
ADDI X22,X22,#1
Exit:

Chapter 2 — Instructions: Language of the Computer — 7


Signed vs. Unsigned
◼ Signed comparison
◼ Unsigned comparison
◼ Example
◼ X22 = 1111 1111 1111 1111 1111 1111 1111 1111
◼ X23 = 0000 0000 0000 0000 0000 0000 0000 0001
◼ X22 < X23 # signed
◼ –1 < +1
◼ X22 > X23 # unsigned
◼ +4,294,967,295 > +1

Chapter 2 — Instructions: Language of the Computer — 8


Branch Addressing
◼ B-type
◼ B 1000 // go to location 10000ten

5 10000ten
6 bits 26 bits

◼ CB-type
◼ CBNZ X19, Exit // go to Exit if X19 != 0

181 Exit 19
8 bits 19 bits 5 bits

◼ Both addresses are PC-relative


◼ Address = PC + offset (from instruction)

Chapter 2 — Instructions: Language of the Computer — 9

You might also like