Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization...

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

Transcript of Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization...

Page 1: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

Control Flow Instructions

CSE 30: Computer Organization and Systems Programming

Diba Mirza

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

1

Page 2: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 3: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 4: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 5: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 6: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 7: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 8: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 9: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

Condition Codes

Not equal Unsigned higher or same Unsigned lower Minus

Equal

Overflow No overflow Unsigned higher Unsigned lower or same

Positive or Zero

Less than Greater than Less than or equal Always

Greater or equal

EQ NE CS/HS CC/LO

PL VS

HI LS GE LT GT LE AL

MI

VC

Suffix Description

Z=0 C=1 C=0

Z=1 Flags tested

N=1 N=0 V=1 V=0 C=1 & Z=0 C=0 or Z=1 N=V N!=V Z=0 & N=V Z=1 or N=!V

v The possible condition codes are listed below v Note AL is the default and does not need to be specified

9

Page 10: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

If(X == 0) X = Y + Z;

C Code

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 BEQ Label

ADD r0, r1, r2 Label:

CMP r0, #0 BNE Label

ADD r0, r1, r2 Label:

A

B C – Neither of these is correct.

10

Page 11: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

If(X == 0) X = Y + Z;

C Code

Assume X, Y, and Z are integers in registers r0, r1, and r2, respectively. Q: Which is the equivalent assembly code?

CMP r0, #0 BEQ Label

ADD r0, r1, r2 Label:

CMP r0, #0 BNE Label ADD r0, r1, r2 Label:

A

B C – Neither of these is correct.

11

Page 12: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 13: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

Compiling C if into ARM

v Compile by hand if (i == j) f=g+h; else f=g-h;

v Use this mapping:

Exit!

i == j?

f=g+h f=g-h

(false) i != j!

(true) i == j!

f: r0 g: r1 h: r2 i: r3 j: r4

13

Page 14: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

Compiling C if into ARM

v Final compiled ARM code: CMP r3, r4 ; Z = 1 if i==j

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.

Exit!

r3 == r4?

r0=r1+r2 r0=r1-r2

(false) !

(true) !

Compile by hand if (r3 == r4) r0=r1+r2; else r0=r1-r2;!

14

Page 15: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

Loops in C/Assembly v There are three types of loops in C:

v while v do… while v for

15

Page 16: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 17: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 18: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

Inequalities in ARM v Until now, 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

18

Try on your own: for(i=0;i<20;i++) {Statements} while(x<30) {Statements}

Page 19: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 20: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

Example: The C Switch Statement

v This is 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

Page 21: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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 !B Exit ! !; end case, Exit L1 !CMP r5,#1 ! !; compare k, -1 !BNE L2 !! !ADD r0,r1,r2 !; k==1 so f=g+h !B Exit ! !; end case, Exit L2 !CMP r5,#2 ! !; compare k, 2 !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!

C code: If(r5==0) r0=r3+r4; else if(r5==1)

r0=r1+r2; else if(r5==2) r0=r1–r2; else if(r5==3)

r0=r3–r4;

21

Page 22: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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

Page 23: Control Flow Instructions · 2014-08-21 · Control Flow Instructions CSE 30: Computer Organization and Systems Programming Diba Mirza Dept. of Computer Science and Engineering University

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