PI CSE30 Lecture 6
PI CSE30 Lecture 6
Diba Mirza
Dept. of Computer Science and Engineering
University of California, San Diego
1
So Far...
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
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;
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;
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 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
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
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