Lecture 6: Decision and Control CS 2011 Spring 2016, Dr. Rozier.

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.
Embedded Systems Programming
Appendix D The ARM Processor
Overheads for Computers as Components 2nd ed.
© 2000 Morgan Kaufman Overheads for Computers as Components ARM instruction set zARM versions. zARM assembly language. zARM programming model. zARM memory.
Chapter 2 Instruction Sets 金仲達教授 清華大學資訊工程學系 (Slides are taken from the textbook slides)
Embedded System Design Center ARM7TDMI Microprocessor Data Processing Instructions Sai Kumar Devulapalli.
Control Structures in ARM Implementation of Decisions Similar to accumulator instructions One instruction sets the flags, followed by another instruction.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /15/2013 Lecture 11: MIPS-Conditional Instructions Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
CONDITION CODE AND ARITHMETIC OPERATIONS – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
ARM Microprocessor “MIPS for the Masses”.
Lecture 5: Decision and Control CS 2011 Fall 2014, Dr. Rozier.
COMP3221: Microprocessors and Embedded Systems--Lecture 8 1 COMP3221: Microprocessors and Embedded Systems Lecture 8: Program Control Instructions
Embedded Systems Programming ARM assembler. Creating a binary from assembler source arm=linux-as Assembler Test1.S arm-linux-ld Linker Arm-boot.o Executable.
COMP3221 lec14-decision-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 14: Making Decisions in C/Assembly Language – II.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
ARM programmer’s model and assembler Embedded Systems Programming.
Topics covered: ARM Instruction Set Architecture CSE 243: Introduction to Computer Architecture and Hardware/Software Interface.
ECE 232 L5 Assembl.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 5 MIPS Assembly.
MIPS Instruction Set Advantages
ARM Instructions I Prof. Taeweon Suh Computer Science Education Korea University.
Embedded System Design Center Sai Kumar Devulapalli ARM7TDMI Microprocessor Thumb Instruction Set.
Assembly Programming on the TI-89 Created By: Adrian Anderson Trevor Swanson.
Lecture 4: Advanced Instructions, Control, and Branching cont. EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
Lecture 2: Basic Instructions CS 2011 Fall 2014, Dr. Rozier.
Lecture 4. ARM Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University ECM586 Special Topics in Embedded Systems.
Making Decision – Microprocessor
Lecture 2: Basic Instructions EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014, Dr.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Execution of an instruction
Topic 7: Control Flow Instructions CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
1 Chapter 4 ARM Assembly Language Smruti Ranjan Sarangi Computer Organisation and Architecture PowerPoint Slides PROPRIETARY MATERIAL. © 2014 The McGraw-Hill.
Lecture 2: Advanced Instructions, Control, and Branching EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
Unit-2 Instruction Sets, CPUs
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Lecture 4: Load/Store Architectures CS 2011 Fall 2014, Dr. Rozier.
EECS 370 Discussion 1 xkcd.com. EECS 370 Discussion Topics Today: – ARM Addressing Endianness, Loading, and Storing Data – Data Layout Struct Packing.
Lecture 6: Branching CS 2011 Fall 2014, Dr. Rozier.
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.
Instruction Set Architectures Early trend was to add more and more instructions to new CPUs to do elaborate operations –VAX architecture had an instruction.
ARM Instructions ARM instructions are written as an operation code (opcode), followed by zero or more operands Operands may be constants (8-bit value),
Smruti Ranjan Sarangi, IIT Delhi Chapter 4 ARM Assembly Language
MIPS Instruction Set Advantages
Chapter 15: Higher Level Constructs
CS2100 Computer Organisation
Control Unit Lecture 6.
ECE 3430 – Intro to Microcomputer Systems
The University of Adelaide, School of Computer Science
ARM Registers Register – internal CPU hardware device that stores binary data; can be accessed much more rapidly than a location in RAM ARM has.
The Cortex-M3/m4 Embedded Systems: Cortex-M3/M4 Instruction Sets
Making Decisions and Writing Loops
Processor Instructions set. Learning Objectives
The University of Adelaide, School of Computer Science
ARM Control Structures
ECE232: Hardware Organization and Design
ARM Control Structures
March 2006 Saeid Nooshabadi
Branching instructions
Overheads for Computers as Components 2nd ed.
MIPS Assembly.
Computer Architecture
Branch & Call Chapter 4 Sepehr Naimi
Introduction to Assembly Chapter 2
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

Lecture 6: Decision and Control CS 2011 Spring 2016, Dr. Rozier

For next time MP0 Your first task is to form into groups of me a list of your 6+2’s and an alphanumeric group username. MP0 will get you acquainted with github and our cluster, as well as some VERY basic ARM. Due next Thursday

LADIES AND TIGERS

The Lady and the Tiger Doors containing either Ladies or Tigers

The Lady and the Tiger Once again, you’ll have to tell Ladies from Tigers A new twist is being added. – Two doors – If a lady is in Room I, then the sign on the door is true. If a tiger is in Room I, then the sign on the door is false. – The opposite is true for Room II.

The Lady and the Tiger Q1 Room ?? This room contains a tiger. Both rooms contain tigers Room ??

The Lady and the Tiger Q1 Room II Room I

The Lady and the Tiger Once again, you’ll have to tell Ladies from Tigers A new twist is being added. – Three doors! – One lady, TWO tigers – At most one of the signs is true

The Lady and the Tiger Q2 Room I A tiger is in this room. Room II A lady is in this room. A tiger is in room II. Room III

The Lady and the Tiger Q2 Room I Room II Room III

The Lady and the Tiger Once again, you’ll have to tell Ladies from Tigers A new twist is being added. – Three doors! – One lady, TWO tigers – The sign on the door of the room with the lady is true. At least one of the other two signs is false!

The Lady and the Tiger Q3 Room I A tiger is in room II. Room II A tiger is in this room. A tiger is in room I. Room III

The Lady and the Tiger Q3 Room I Room II Room III

BASIC INSTRUCTIONS

The Memory Hierarchy

Load-Store Architecture RISC architectures, like ARM and MIPS utilize a load-store architecture. Memory cannot be part of arithmetic operations. – Only registers can do this Access memory is through loads and stores.

Register Memory Architecture Featured on many CISC architectures, like x86 Allows direct access to memory by instructions.

Load Store and ARM Register space is pretty cramped!!! LoaD to a Register with LDR SToRe to memory with STR ldr, [ {, }] – Loads a byte from + into str, [ {, }] – Stores a byte from into +

Load Store and ARM Example – ldr r0, [r1,r2] Load data from location r1+r2 into r0. – ldr r0, =string Load data from label string into r0. Special cases exist, see ARM manual – Example: ldrb loads a single byte, padded with zeros.

Constants or Immediates Operands can contain registers, or immediate values. – An immediate is like a constant – Represent immediates as follows: #20 add r0, r1, #20 – adds 20 to the value of r1 and stores it in r0.

Arithmetic Instructions Addition – add, adc, adds, etc Subtraction – sub, sbc, rsb, subs, etc Multiply – mul, mla, etc

Move Instruction mov, – mov r0, r1 – copy the contents of r1 into r0. – mov r0, #20 – copy an immediate value of 20 into r0. mvn, – Move negative, negates operand before copying it.

Compare Instructions cmp, cmn, Don’t change the operands, update special status register flags. cmp – subtracts operand2 from operand1 and discards the result. cmn – adds operand2 to operand1 and discards the result.

CURRENT PROGRAM STATUS REGISTER

Status Register Flags Compare instructions and the special “S” versions of instructions (adds, subs, movs) set the status register flags. Can be used with conditional suffixes to make conditionally executed instructions.

CPSR The Current Program Status Register – Special register that holds information on the side effects of instructions. – Condition code flags N – Negative result from the ALU Z – Zero result from the ALU C – ALU operation Carried out V – ALU operation oVerflowed

CPSR Operand 1 – 32 bits Operand 2 – 32 bits Result – 32 bits

CPSR FlagLogical InstArith Inst N = 1No meaningBit 31 of the result has been set. Indicates a negative number for signed operations Z = 1Result bits are all zero Result of the operation was zero C = 1After shift operation a ‘1’ was left in the carry Result was greater than 32 bits V = 1No meaningResult was greater than 31 bits, possible corruption of sign bit

CPSR The CPSR bits are set by: Compare/Test instructions: – The only effect of a comparison is to UPDATE THE CONDITIONS FLAGS. CMP : op1 – op2 CMN: op1 + op2 TST: op1 AND op2 TEQ: op1 EOR op2

CPSR The CPSR bits are set by: Data processing operations do not normally effect CPSR! – Can be caused to effect them by adding the S bit of the instruction.

CPSR The CPSR bits are set by: Data processing operations and CPSR – Add the “S” suffix to set the “S” bit. – ADDS – SUBS – ANDS

Conditional Execution The NZCV flags form the basis for conditional execution in the ARM, one of its most powerful features. – Most architectures must use “branch” or “jump” instructions. – The ARM can enable or disable individual instructions based on the CPSR.

Conditional Execution

Just as the special “S” suffix can be added to set status flags, other suffixes can be added to act on status flags.

EQ: Equal Z=1 Using the EQ suffix on an instruction will cause it to only be executed if the zero flag is set. cmp r0, Set flags based on r0-r1 adds r0, r1, Set flags based on r0 = r1 + r2 movs r0, Set flags based on r0 = r1

EQ: Equal Z=1 Using the EQ suffix on an instruction will cause it to only be executed if the zero flag is set. Example cmp r0, Set flags based on r0-r1 addeq r2, r0, Conditional addition

NE: Equal Z=0 Using the NE suffix on an instruction will cause it to only be executed if the zero flag is not set.

Other conditional suffixes VS – overflow set, V=1 VC – overflow clear, V=0 MI – minus set, N=1 PL – minus clear, N=0 CS – carry set, C=1 CC – carry clear, C=0 AL – always, unconditional NV – never, unconditional

Multiple Conditional Suffixes HI – higher (unsigned), C=1 and Z=0 – Unsigned greater than LS – lower (unsigned), C=0 || Z=1 – Unsigned less than GE – greater or equal (signed), N=1 and V=1 OR N=0, V=0 – Signed greater than or equal to LT – less than (signed), N=1 and V=0, OR N=0 and V=1 – Signed less than

Multiple Conditional Suffixes GT – greater than (signed), (N=1 and V=1, OR N=0 and V=1) AND Z=0 – Signed greater than LE – less than or equal (signed), (N=1 and V=0, OR N=0 and V=1) OR Z=1 – Signed less than or equal to

Conditional Execution EQ – enable this instruction if the results of the last CMP or “S” instruction indicate equality: Example: CMP r0, r1 ADDEQ r0, r0, r1

Conditional Execution To understand conditional execution, let’s think in terms of CMP. – CMP r0, r1 What does this mean to the processor?

Conditional Execution To understand conditional execution, let’s think in terms of CMP. – CMP r0, r1 r0 - r1, and set NZCV flags – EQ is the conditional execution suffix for r0 == r1. – NE is the conditional execution suffix for r0 != r1. – HI is the unsigned conditional execution suffix for r0 > r1 – LO is the unsigned conditional execution suffix for r0 < r1 In groups, what are the values of NZCV that enable these conditionals?

Conditional Execution To understand conditional execution, let’s think in terms of CMP. – CMP r0, r1 r0 - r1, and set NZCV flags – HS is the conditional execution suffix for r0 >= r1. – LS is the conditional execution suffix for r0 <= r1. In groups, what are the values of NZCV that enable these conditionals?

Conditional Execution How would you build GT, GE and LT, LE (the signed equivalents)?

Conditional Execution CodeSuffixMeaningCodeSuffixMeaning 0000EQZ = 11001LSC = 0 || Z = NEZ = 01010GE(N=1 && V=1) || (N=0 && V=0) 0010HS/CSC = 11011LT(N=1 && V=0) || (N=0 && V=1) 0011LO/CCC = 01100GTZ=0 && ((N=1 && V=1) || (N=0 && V=0)) 0100MIN = 11101LEZ=1 || ((N=1 && V=0) || (N=0 && V=1)) 0101PLN = 01110ALAlways 0110VSV = 11111NVReserved/deprec ated 0111VCV = HIC = 1 && Z = 0

BRANCHING

Branching Conditional execution isn’t the only tool in our belt.

Branching Branches allow us to transfer control of the program to a new address. – b ( ) – bl ( ) b start bl start

Branching Basic branches do not operate on registers. Typically we branch to an indicated LABEL, example: MAIN: b END END: b MAIN

Branching Branches are calculated by the assembler relative to the current address. – Allows branching +/- 32 Mbytes Branch stores the target address in the Program Counter Branch and link also stores the next address in the link register.

Branch (b) Branch, possibly conditionally, to a new address. beq If Z=1, branch Good practice to use bal instead of b.

Branch with link (bl) Branch, possibly conditionally, to a new address. – Before the branch is complete, store the PC in the LR. – Allows easy return from the branch. bleq If Z=1, branch, saving the PC

Branch with link (bl) How do we get back once we’ve saved the PC? mov pc, lr Moves the contents of the link register to the program counter.

Implementing If Statements C code: if (i == j) f = g+h; else f = g - h; ARM code cmp r0, Set flags via r0-r1 and discard beq Else add r2, r3, r2 = r3 + r4 bal Exit Else: sub r2, r3, r2 = r3 + r4 Exit:

Implementing Loop Statements C code: while (i < j) i += 1; ARM code Loop: cmp r0, r1 bge Exit add r0, r0, #1 bal Loop Exit: i < j? i=i+1 i<j Exit i>=j

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