Control Structures in ARM Implementation of Decisions Similar to accumulator instructions One instruction sets the flags, followed by another instruction.

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.
Cosc 2150: Computer Organization
Chapter 3 Introduction to the 68000
MIPS assembly. Review  Lat lecture, we learnt  addi,  and, andi, or, ori, xor, xori, nor,  beq, j, bne  An array is stored sequentially in the memory.
EECC250 - Shaaban #1 Lec # 2 Winter Addressing Modes  Addressing modes are concerned with the way data is accessed  Addressing can be.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Comp Sci Control structures 1 Ch. 5 Control Structures.
CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson.
LAB Flow Control Instructions
Lecture 5: Decision and Control CS 2011 Fall 2014, Dr. Rozier.
© 2010 Kettering University, All rights reserved..
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 Instructions I Prof. Taeweon Suh Computer Science Education Korea University.
CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 1 Flow Control.
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
Making Decision – Microprocessor
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Computer Architecture 2 Patrick Marshall. Gates per CPU Vacuum tube Transistor Small Scale integration –1965 on –Up to 100 devices.
ECE 447: Lecture 12 Logic, Arithmetic, Data Test and Control Instructions of MC68HC11.
Topic 7: Control Flow Instructions CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering.
(Flow Control Instructions)
Natawut NupairojAssembly Language1 Control Structure.
Accumulator machine We will use the accumulator machine architecture to demonstate pass1 and pass2. The accumulator machine has one processor register:
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 4 –Binary Arithmetic These are lecture notes to accompany the book SPARC.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
Lecture 2: Advanced Instructions, Control, and Branching EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
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.
SPARC Programming Model 24 “window” registers 8 global registers Control registers –Multiply step –PSR (status flags, etc.) –Trap Base register –Window.
Lecture 6: Decision and Control CS 2011 Spring 2016, Dr. Rozier.
ARM Instructions ARM instructions are written as an operation code (opcode), followed by zero or more operands Operands may be constants (8-bit value),
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Unit 1 Instruction set M.Brindha AP/EIE
Slide 7 Mikroprosesor Sub. Algoritma Program___
Chapter 15: Higher Level Constructs
Microprocessor T. Y. B. Sc..
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.
3.Instruction Set of 8085 Consists of 74 operation codes, e.g. MOV
Making Decisions and Writing Loops
ECE 3430 – Intro to Microcomputer Systems
Decision Making.
Decision Making.
Processor Instructions set. Learning Objectives
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Instructions for Making Decisions
Pick up the handout on your way in!!
ARM Control Structures
How to represent signed integers
CS 235 Computer Organization & Assembly Language
Program Logic and Control
Program Logic and Control
Write a program to calculate x**y, given x and y.
The University of Adelaide, School of Computer Science
ARM Control Structures
March 2006 Saeid Nooshabadi
Branching instructions
Overheads for Computers as Components 2nd ed.
MIPS Assembly.
MIPS assembly.
Chapter 7 –Program Logic and Control
Chapter 7 –Program Logic and Control
Branch & Call Chapter 4 Sepehr Naimi
An Introduction to the ARM CORTEX M0+ Instructions
MIPS instructions.
Presentation transcript:

Control Structures in ARM Implementation of Decisions Similar to accumulator instructions One instruction sets the flags, followed by another instruction that uses the flags to make the actual branch decision ARM compare and test instructions InstructionOperationNotes cmp rn, cmn rn, rn - rn += Always updates NZCV tst rn, teq rn, rn & rn ^ Always updates NZ, if shifted. may affect C flag

Condition Codes Conditional branch instructions make a decision to branch based on the state of the appropriate flag. SuffixDescriptionFlags tested eqEqualZ = 1 neNot equalZ = 0 cs / hsUnsigned higher or sameC = 1 cc / loUnsigned lowerC = 0 miMinusN = 1 plPositive or ZeroN = 0 vsOverflowV = 1 vcNo overflowV = 0 hiUnsigned higherC = 1 & Z = 0 lsUnsigned lower or sameC = 0 or Z = 1 geGreater than or equalN = V ltLess thanN != V gtGreater thanZ = 0 & N = V leLess than or equalZ = 1 or N = !V alAlways

ARM Branch Instructions Unconditional branch B (or BAL) branch always testing individual condition codes: bmi – branch on negative (N==1) bpl – branch on positive or zero (N==0) bvs – branch on overflow set (V==1) bvc – branch on overflow clear (V==0) bcs – branch on carry set (C==1) bcc – branch on carry clear (C==0)

ARM Branch Instructions testing result of compare or other operation (signed arithmetic): beq – branch on equal (Z==1) bne – branch on not equal (Z==0) bls – branch on less than ((N xor V)==1) ble – branch on less than or equal ((Z or (N xor V))==1) bge – branch on greater than or equal ((N xor V)==0) bgt – branch on greater than ((Z or (N xor V))==0)

Comparison Instructions CMP – Compare: subtracts a register or an immediate value from a register value and updates condition codes Examples: – CMP r3, set Z flag if r3 == 0 – CMP r3, set Z flag if r3 == r4 All flags are set as a result of this operation, not just Z.

C if statement in ARM int x; int y; if(x == 0) y = 1; /* assume x is in r0, y is in r1 */ exit x == 0? y = 1 (false) (true) x == 0 C Source CodeIneffficient AssemblyEfficient Assembly int x; int y; if(x == 0) y = 1; cmp r0, #0 beq then b endif then: mov r1, now store r1 in [y] endif:... cmp r0, #0 bne endif then: mov r1, now store r1 in [y]

C if statement in ARM if((x+b)>z) x+=y; ARM code: /* assume x is in r0 y is in r1, and z is in r2 */ add r3, r0, r1 cmp r3, r2 ble branch to false is false add r0, r0, x = x+y /* now store content of r0 to [x] */ false: exit x+y > z? x += y (false) (true) (x+y)>z

C if-else statement in ARM if(i == j) f=g+h; else f=g-h; ARM code: /* assume f is in r0, i is in r1, j is in r2, g is in r3, h is in r4, */ cmp r1, Z = 1 if i==j beq branch to true when i==j sub r0, r3, f = g-h (false) branch to done true: add r0, f = g+h (true) done: exit i == j? f=g+h f=g-h (false) i != j (true) i == j

Conditional execution in ARM An unusual ARM feature is that all instructions may be conditional: CMP r0, if (r0 != 5) { ADDNE r1, r1, r1 := r1 + r0 - r2 SUBNE r1, r1, r2 } this removes the need for some short branches, improving performance and code density

ARM Control Structures Implementing Loops All for loops, while loops, and do-while loops have an implicit branch from the bottom to the top of the loop. This branch instruction becomes explicit when translated into assembly. while loop /* assume x is in r0 while ( x <= 10 ) and y is in r1 */ { loop: x = x + y; cmp r0, test condition } bgt done add r0, r0, r1 b loop done:

Loops in C/Assembly for x: r0, y: r1, z: r2 for ( x = 1; x <= y; x++ ) mov r0, #1 { loop: z *= x; cmp r0, test condition } bgt done mul r2, r2, r0 rewritten as while loop add r0, r0, #1 x = 1; b loop while ( x <= y ) done: { z *= x; x++; }

Control Structures in ARM for loop, counted up from 0 to n-1 /* i : r0, n: r1 */ for ( i = 0; i < n; i++ ) { mov i, itest: } loop: cmp r0, r1 rewritten as while loop bge done i = 0; while ( i < n ) add r0, r0, #1 { b loop done: i++; }

Control Structures in ARM for loop, counted up from 1 to n i.req r0 for ( i = 1; i <= n; i++ ) { n.req r1 mov r1, #1 } loop: cmp i, n bgt done rewritten as while loop i = 1; add i, i, #1 while(i <= n) b loop { done: i++; }

Control Structures in ARM for loop, counted down from to n to 1 for ( i = n; i > 0; i-- ) i.req r0 { n.req r1 mov n, i } loop: cmp i, #0 ble done rewritten as while loop i = n;sub i, i, #1 while ( i > 0 ) b loop { done: i--; }