0% found this document useful (0 votes)
21 views23 pages

PI CSE30 Lecture 6

Uploaded by

lohohod641
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)
21 views23 pages

PI CSE30 Lecture 6

Uploaded by

lohohod641
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/ 23

Control Flow Instructions

CSE 30: Computer Organization and Systems Programming

Diba Mirza
Dept. of Computer Science and Engineering
University of California, San Diego

1
So Far...

v All instructions have allowed us to manipulate


data
v So we’ve built a calculator
v In order to build a computer, we need ability to
change the flow of control in a program…

2
Ways to change Control flow in C
1.  goto <label>
2.  if (condition) { //do something }
3.  if-else
4.  Loops
I.  do-while
II.  for
III.  while
IV.  switch

3
Labels
v  Any instruction can be associated with a label
v  Example:
start ADD r0,r1,r2 ; a = b+c
next SUB r1,r1,#1 ; b--
v  In fact, every instruction has a label regardless if
the programmer explicitly names it
v  The label is the address of the instruction
v  A label is a pointer to the instruction in memory
v  Therefore, the text label doesn’t exist in binary code

4
ARM goto Instruction
v  The simplest control instruction is equivalent to a
C goto statement
v  goto label (in C) is the same as:
v  B label (in ARM)
v  B is shorthand for “branch”. This is called an
unconditional branch meaning that the branch is
done regardless of any conditions.
v  There are also conditional branches:

5
Conditional Branch
v  To perform a conditional branch,
1.  First set the condition bits (N,Z,V,C) in the program
status register
2.  Then check on these condition bits to branch
conditionally
v  What are the ways we have learnt to set condition
bits so far?
v  Append S to arithmetic/logical instruction
v  There is another way using a ‘Comparison
Instruction’
6
Comparison Instructions
v  CMP – Compare and set condition bits
v  subtracts a register or an immediate value from a
register value and updates condition codes
v  Unlike SUB, it doesn’t store the result anywhere

v  Examples:
v  CMP r3, #0 ; set Z flag if r3 == 0
v  CMP r3, r4 ; set Z flag if r3 == r4

All flags are set as result of this operation, not just Z


Conditional branches often preceded by CMP
7
ARM Decision Instructions
v  ARM has variants of the branch instruction that only
goto the label if a certain condition is TRUE
v  Examples:
v  BEQ label ; BRANCH EQUAL
v  BNE label ; BRANCH NOT EQUAL
v  BLE label ; BRANCH LESS THAN EQUAL
v  BLT label ; BRANCH LESS THAN
v  BGE label ; BRANCH GREATER THAN EQUAL
v  BGT label ; BRANCH GREATER THAN
v  Plus more …

v  The
condition is T/F based upon the fields in the
Program Status Register 8
Condition Codes
v  The possible condition codes are listed below
v  Note AL is the default and does not need to be specified
Suffix Description Flags tested
EQ Equal Z=1
NE Not equal Z=0
CS/HS Unsigned higher or same C=1
CC/LO Unsigned lower C=0
MI Minus N=1
PL Positive or Zero N=0
VS Overflow V=1
VC No overflow V=0
HI Unsigned higher C=1 & Z=0
LS Unsigned lower or same C=0 or Z=1
GE Greater or equal N=V
LT Less than N!=V
GT Greater than Z=0 & N=V
LE Less than or equal Z=1 or N=!V
AL Always 9
If(X == 0)
C Code
X = Y + Z;

Assume X, Y, and Z are integers in registers r0, r1, and r2,


respectively.
Q: Which one is the equivalent assembly code?

CMP r0, #0
A BEQ Label
ADD r0, r1, r2
Label:
CMP r0, #0
BNE Label C – Neither of these is
B
ADD r0, r1, r2 correct.
Label: 10
If(X == 0)
C Code
X = Y + Z;

Assume X, Y, and Z are integers in registers r0, r1, and r2,


respectively.
Q: Which is the equivalent assembly code?

CMP r0, #0
A BEQ Label
ADD r0, r1, r2
Label:
CMP r0, #0
BNE Label C – Neither of these is
B
ADD r0, r1, r2 correct.
Label: 11
C if-else
v if statements in C
v if (condition) {clause}
v if (condition) {clause1} else {clause2}

v Rearrange if-else into following:


if (condition) goto L1;
clause2;
goto End;
L1: clause1;
End:
v Not as elegant as if-else, but same meaning
v Now let’s try to write equivalent ARM code
12
Compiling C if into ARM

v Compile by hand
(true) 
 (false) 

if (i == j) f=g+h; i == j? i != j!
i == j!
else f=g-h;
v Use this mapping: f=g+h f=g-h
f: r0
g: r1
Exit!
h: r2
i: r3
j: r4

13
Compiling C if into ARM
Compile by hand (true) 
 r3 == r4? (false) 

! !
if (r3 == r4) r0=r1+r2;
else r0=r1-r2;!
r0=r1+r2 r0=r1-r2
v Final compiled ARM code:
CMP r3, r4 ; Z = 1 if i==j
Exit!
BEQ True ; goto True when i==j
SUB r0,r1,r2 ; f=g-h(false)
B Fin ; goto Fin
True ADD r0,r1,r2 ; f=g+h (true)
Fin
Note: Compiler automatically creates labels to handle decisions
(branches) appropriately. Generally not found in C code.
14
Loops in C/Assembly
v There are three types of loops in C:
v while
v do… while
v for

15
Loops in C/Assembly
v Simple loop in C;
do{
g--;
i = i + j;}
while (i != h);
v Rewrite this as:
Loop: g--;
i = i + j;
if (i != h) goto Loop;
v Use
this mapping:
g: r1, h: r2, i: r3, j: r4
16
Loops in C/Assembly

v Replace variables with equivalent registers


Loop: r1--;
r3 = r3 + r4;
if (r3 != r2) goto Loop;
v Final compiled ARM code:
Loop SUB r1,r1,#1 ; r1--
ADD r3,r3,r4 ; r3=r3+r4
CMP r3,r2 ; cmp r3,r2
BNE Loop ;goto Loop if r3!=r2

17
Inequalities in ARM
v Untilnow, we’ve only tested equalities
(== and != in C). General programs need to test < and
> as well.
v Use CMP and BLE, BLT, BGE, BGT
v Examples:
if (f < 10) goto Loop; => CMP r0,#10
BLT Loop
if (f >= i) goto Loop; => CMP r0,r3
BGE Loop
Try on your own:
for(i=0;i<20;i++) {Statements}
while(x<30) {Statements}
18
Example: The C Switch Statement

v Choose
among four alternatives depending
on whether k has the value 0, 1, 2 or 3.
Compile this C code:
switch (k) {
case 0: f=i+j; break; /* k=0*/
case 1: f=g+h; break; /* k=1*/
case 2: f=g–h; break; /* k=2*/
case 3: f=i–j; break; /* k=3*/
}

19
Example: The C Switch Statement

v Thisis complicated, so simplify.


v Rewrite it as a chain of if-else statements,
which we already know how to compile:
if(k==0) f=i+j;
else if(k==1) f=g+h;
else if(k==2) f=g–h;
else if(k==3) f=i–j;
v Use this mapping:
f: r0, g: r1, h: r2, i: r3, j:
r4, k: r5
20
Example: The C Switch Statement

! !CMP r5,#0 ! !; compare k, 0!


! !BNE L1 !; branch k!=0

"ADD r0,r3,r4 !; k==0 so f=i+j
 C code:
!B Exit ! !; end case, Exit
 If(r5==0) r0=r3+r4;
L1 !CMP r5,#1 ! !; compare k, -1
 else if(r5==1)
!BNE L2 ! r0=r1+r2;
else if(r5==2)
! !ADD r0,r1,r2 !; k==1 so f=g+h
 r0=r1–r2;
!B Exit ! !; end case, Exit
 else if(r5==3)
L2 !CMP r5,#2 ! !; compare k, 2
 r0=r3–r4;
!BNE L3 ! !; branch k!=2

"SUB r0,r1,r2 !; k==2 so f=g-h

!B Exit ! !; end case, Exit

L3 !CMP r5,#3 ! !; compare k, 3 ! ! !
!BNE Exit ! !; branch k!=3 ! ! !
! !SUB r0,r3,r4 !; k==3 so f=i-j 

Exit!
21
Summary
v A Decision allows us to decide which pieces of code to
execute at run-time rather than at compile-time.
v C Decisions are made using conditional statements
within an if, while, do while or for.
v CMP instruction sets status register bits
v ARM Decision making instructions are the conditional
branches: BNE,BEQ,BLE,BLT,BGE,BGT.

22
Conclusion
v Instructions so far:
v  Previously:
ADD, SUB, MUL, MULA, [U|S]MULL, [U|S]MLAL, RSB
AND, ORR, EOR, BIC
MOV, MVN
LSL, LSR, ASR, ROR
LDR, LDR, STR, LDRB, STRB, LDRH, STRH
v  New:
CMP, B{EQ,NE,LT,LE,GT,GE}

23

You might also like