Topic 7: Control Flow Instructions CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering.

Slides:



Advertisements
Similar presentations
Slides created by: Professor Ian G. Harris Efficient C Code  Your C program is not exactly what is executed  Machine code is specific to each ucontroller.
Advertisements

ARM versions ARM architecture has been extended over several versions.
CENG 311 Decisions in C/Assembly Language
ARM Microprocessor “MIPS for the Masses”.
Lecture 5: Decision and Control CS 2011 Fall 2014, Dr. Rozier.
cs61c L5 Decisions 9/13/00 1 CS61C - Machine Structures Lecture 5 - Decisions in C/Assembly Language September 13, 2000 David Patterson
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 6 – Introduction to MIPS Data Transfer & Decisions I Pieter Abbeel’s recent.
COMP3221 lec13-decision-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 13: Making Decisions in C/Assembly Language - I
CS61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L10 Introduction to MIPS : Decisions II (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
COMP3221 lec14-decision-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 14: Making Decisions in C/Assembly Language – II.
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Lecture 4: Loads, Logic, Loops. Review Memory is byte-addressable, but lw and sw access one word at a time. These instructions transfer the contents of.
CS61C L7 MIPS Decisions (1) Garcia, Fall 2005 © UCB Lecturer PSOE, new dad Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L10 Introduction to MIPS : Decisions II (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Decisions in C/Assembly Language Jen-Chang Liu, Spring 2006 Adapted from
CS61C L7 MIPS: Load & Store, Decisions (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture.
CS61C L7 MIPS Decisions (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #7 – MIPS.
CS 61C L07 MIPS Memory (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #7: MIPS Memory & Decisions.
CS1104 Assembly Language: Part 1
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 07 Introduction to MIPS : Decisions II Researchers at Microsoft and UW.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
CS61C L09 Introduction to MIPS : Data Transfer and Decisions (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
More decisions and logic (1) Fall 2010 Lecture 4: Loads, Logic, Loops.
ARM Instructions I Prof. Taeweon Suh Computer Science Education Korea University.
Embedded System Design Center Sai Kumar Devulapalli ARM7TDMI Microprocessor Thumb Instruction Set.
Intel has developed a new technique to generate random numbers that is suitable for integration directly into the CPU! This circuit can turn out 2.4 billion.
Topic 8: Data Transfer Instructions CSE 30: Computer Organization and Systems Programming Winter 2010 Prof. Ryan Kastner Dept. of Computer Science and.
Topic 10: Instruction Representation CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and.
Lecture 2: Basic Instructions CS 2011 Fall 2014, Dr. Rozier.
Chapter 3-1 ARM ISA ARM Instruction Set Architecture ARM Instruction Set Architecture Next Lecture Next Lecture  ARM program examples.
Making Decision – Microprocessor
Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
IT 251 Computer Organization and Architecture More MIPS Control Instructions Chia-Chi Teng.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
ADVANCED PROCESSOR ARCHITECTURES AND MEMORY ORGANISATION – ARM
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 07 Introduction to MIPS : Decisions II Guest Lecturer Alan Christopher.
Lecture 2: Advanced Instructions, Control, and Branching EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
EECS 370 Discussion 1 xkcd.com. EECS 370 Discussion Topics Today: – ARM Addressing Endianness, Loading, and Storing Data – Data Layout Struct Packing.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
1 TM 1 Embedded Systems Lab./Honam University r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 (sp) r14 (lr) r15 (pc) cpsr r13 (sp) r14 (lr) spsr r13 (sp)
1 TM T H E A R C H I T E C T U R E F O R T H E D I G I T A L W O R L D The ARM Architecture.
ARM7 TDMI INTRODUCTION.
Instruction Set Architectures Early trend was to add more and more instructions to new CPUs to do elaborate operations –VAX architecture had an instruction.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Lecture 6: Decision and Control CS 2011 Spring 2016, Dr. Rozier.
1 TM T H E A R C H I T E C T U R E F O R T H E D I G I T A L W O R L D The ARM Architecture.
ARM Instructions ARM instructions are written as an operation code (opcode), followed by zero or more operands Operands may be constants (8-bit value),
MIPS: Load & Store, Decisions. Memory management review Three 3’s Picking blocks off free list  best-, first-, next-fit Attempts to solve external fragmentation.
Architecture Revisions
Chapter 15: Higher Level Constructs
ECE 3430 – Intro to Microcomputer Systems
ARM Registers Register – internal CPU hardware device that stores binary data; can be accessed much more rapidly than a location in RAM ARM has.
Making Decisions and Writing Loops
Processor Instructions set. Learning Objectives
C second, objective-c, Go up!
Topic 6: Bitwise Instructions
The University of Adelaide, School of Computer Science
March 2006 Saeid Nooshabadi
Branching instructions
Overheads for Computers as Components 2nd ed.
Multiply Instructions
Immediate data Immediate operands : ADD r3, r3, #1 valid ADD r3, #1,#2 invalid ADD #3, r1,r2 invalid ADD r3, r2, #&FF ( to represent hexadecimal immediate.
Branch & Call Chapter 4 Sepehr Naimi
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

Topic 7: Control Flow Instructions CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University of California, San Diego

So Far...  All instructions have allowed us to manipulate data  So we’ve built a calculator  In order to build a computer, we need ability to make decisions…

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

C Decisions: if Statements  if statements in C  if ( condition ) clause  if ( condition ) clause1 else clause2  Rearrange 2nd if into following: if ( condition ) goto L1; clause2; goto L2; L1: clause1; L2:  Not as elegant as if-else, but same meaning

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

ARM Decision Instructions  ARM also has variants of the branch instruction that only goto the label if a certain condition is TRUE  Examples:  BEQ label ; BRANCH EQUAL  BNE label ; BRANCH NOT EQUAL  BLE label ; BRANCH LESS THAN EQUAL  BLT label ; BRANCH LESS THAN  BGE label ; BRANCH GREATER THAN EQUAL  BGT label ; BRANCH GREATER THAN  Plus more …  The condition is T/F based upon the fields in the Program Status Register

Program Status Registers  Condition code flags  N = Negative result from ALU  Z = Zero result from ALU  C = ALU operation Carried out  V = ALU operation oVerflowed  Sticky Overflow flag - Q flag  Architecture 5TE/J only  Indicates if saturation has occurred  J bit  Architecture 5TEJ only  J = 1: Processor in Jazelle state  Interrupt Disable bits.  I = 1: Disables the IRQ.  F = 1: Disables the FIQ.  T Bit  Architecture xT only  T = 0: Processor in ARM state  T = 1: Processor in Thumb state  Mode bits  Specify the processor mode 2731 N Z C V Q 2867 I F T mode fsxc U n d e f i n e dJ

Flags and Their Use  The N flag  Set if the result is negative or equivalently if the MSB == ‘1’  The Z flag  Set if the result is zero  The C flag  Set if  The result of an addition is greater than 2 32  The result of a subtraction is positive  Carryout from the shifter is ‘1’  The V flag (oVerflow)  Set if there is overflow

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 SuffixDescription 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  The possible condition codes are listed below  Note AL is the default and does not need to be specified

The ARM Register Set r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 (sp) r14 (lr) r15 (pc) cpsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r8 r9 r10 r11 r12 r13 (sp) r14 (lr) spsr FIQIRQSVCUndefAbort User Mode r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 (sp) r14 (lr) r15 (pc) cpsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r8 r9 r10 r11 r12 r13 (sp) r14 (lr) spsr Current Visible Registers Banked out Registers FIQIRQSVCUndefAbort r0 r1 r2 r3 r4 r5 r6 r7 r15 (pc) cpsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r8 r9 r10 r11 r12 r13 (sp) r14 (lr) spsr Current Visible Registers Banked out Registers UserIRQSVCUndefAbort r8 r9 r10 r11 r12 r13 (sp) r14 (lr) FIQ ModeIRQ Mode r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r15 (pc) cpsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r8 r9 r10 r11 r12 r13 (sp) r14 (lr) spsr Current Visible Registers Banked out Registers UserFIQSVCUndefAbort r13 (sp) r14 (lr) Undef Mode r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r15 (pc) cpsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r8 r9 r10 r11 r12 r13 (sp) r14 (lr) spsr Current Visible Registers Banked out Registers UserFIQIRQSVCAbort r13 (sp) r14 (lr) SVC Mode r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r15 (pc) cpsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r8 r9 r10 r11 r12 r13 (sp) r14 (lr) spsr Current Visible Registers Banked out Registers UserFIQIRQUndefAbort r13 (sp) r14 (lr) Abort Mode r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r15 (pc) cpsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r13 (sp) r14 (lr) spsr r8 r9 r10 r11 r12 r13 (sp) r14 (lr) spsr Current Visible Registers Banked out Registers UserFIQIRQSVCUndef r13 (sp) r14 (lr) Only need to worry about cpsr (current program status register)

Compiling C if into ARM  Compile by hand if (i == j) f=g+h; else f=g-h;  Use this mapping: f : r0, g : r1, h : r2, i : r3, j : r4 Exit i == j? f=g+hf=g-h (false) i != j (true) i == j

Comparison Instructions  In order to perform branch on the “==“ operation we need a new instruction  CMP – Compare: subtracts a register or an immediate value from a register value and updates condition codes  Examples:  CMP r3, #0 ; set Z flag if r3 == 0  CMP r3, r4 ; set Z flag if r3 == r4 All flags are set as result of this operation, not just Z.

Compiling C if into ARM  Final compiled MIPS code: CMP r3, r4 ; Z = 1 if i==j BEQ True ; goto True when i==j SUB r0,r1,r2 ; f=g-h(false) BFin ; 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 i == j? f=g+hf=g-h (false) i != j (true) i == j Compile by hand if (i == j) f=g+h; else f=g-h;

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

Loops in C/Assembly  Final compiled MIPS code: LoopSUB r1,r1,#1 ; g-- ADD r3,r3,r4 ; i=i+j CMP r3,r2; cmp i,h BNE Loop; goto Loop ; if i!=h

Inequalities in ARM  Until now, we’ve only tested equalities ( == and != in C). General programs need to test as well.  Use CMP and BLE, BLT, BGE, BGT  Examples: if (f CMP r0,#10 BLT Loop if (f >= i) goto Loop; => CMP r0,r3 BGE Loop

Loops in C/Assembly  There are three types of loops in C:  while  do … while  for  Each can be rewritten as either of the other two, so the method used in the previous example can be applied to while and for loops as well.  Key Concept: Though there are multiple ways of writing a loop in ARM, conditional branch is key to decision making

Example: The C Switch Statement  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*/ }

Example: The C Switch Statement  This is complicated, so simplify.  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;  Use this mapping: f: $s0, g: $s1, h: $s2, i: $s3, j: $s4, k: $s5

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 of case so Exit L1 CMP r5,#1; compare k, -1 BNE L2 ADD r0,r1,r2 ; k==1 so f=g+h B Exit ; end of case so 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 of case so Exit L3 CMP r5,#3; compare k, 3 BNE Exit; branch k!=3 SUB r0,r3,r4 ; k==3 so f=i-j Exit

Predicated Instructions if (r0 == 0) { r1 = r1 + 1; } else { r2 = r2 + 1; } C source code  5 instructions  5 words  5 or 6 cycles  3 instructions  3 words  3 cycles CMP r0, #0 BNE else ADD r1, r1, #1 B end else ADD r2, r2, #1 end... ARM instructions unconditional CMP r0, #0 ADDEQ r1, r1, #1 ADDNE r2, r2, #1... conditional  All instructions can be executed conditionally. Simply add {EQ,NE,LT,LE,GT,GE, etc.} to end

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

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