Topic 7: Control Flow Instructions

Similar documents
THUMB Instruction Set

An Introduction to the ARM 7 Architecture

The ARM Architecture. With a focus on v7a and Cortex-A8

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Exception and Interrupt Handling in ARM

Developer Suite ARM. Assembler Guide. Version 1.2. Copyright 2000, 2001 ARM Limited. All rights reserved. ARM DUI 0068B

ARM Instruction Set. ARM7TDMI-S Data Sheet. Final - Open Access

Reduced Instruction Set Computer (RISC)

We will use the accumulator machine architecture to demonstrate pass1 and pass2.

Graded ARM assembly language Examples

A Choices Hypervisor on the ARM architecture

ARM Cortex-M3 Assembly Language

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

Programming Techniques

What to do when I have a load/store instruction?

EECS 427 RISC PROCESSOR

Translating C code to MIPS

Instruction Set Design

StrongARM** SA-110 Microprocessor Instruction Timing

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

How It All Works. Other M68000 Updates. Basic Control Signals. Basic Control Signals

Instruction Set Architecture

PROBLEMS #20,R0,R1 #$3A,R2,R4

Interrupt handling. Andrew N. Sloss

Computer Organization and Architecture

HC12 Assembly Language Programming

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

Instruction Set Architecture (ISA) Design. Classification Categories

ARM Microprocessor and ARM-Based Microcontrollers

Keil C51 Cross Compiler

Typy danych. Data types: Literals:

ADVANCED PROCESSOR ARCHITECTURES AND MEMORY ORGANISATION Lesson-12: ARM

ARM Architecture. ARM history. Why ARM? ARM Ltd developed by Acorn computers. Computer Organization and Assembly Languages Yung-Yu Chuang

ARM, the ARM Powered logo, BlackICE and ICEbreaker are trademarks of Advanced RISC Machines Ltd.

Central Processing Unit Simulation Version v2.5 (July 2005) Charles André University Nice-Sophia Antipolis

ARM. The ARM Instruction Set. Advanced RISC Machines. The ARM Instruction Set - ARM University Program - V1.0 1

CS201: Architecture and Assembly Language

ARM7TDMI-S. Technical Reference Manual. (Rev 3) Copyright ARM Limited All rights reserved. ARM DDI 0084F

A-level COMPUTER SCIENCE

Keil Debugger Tutorial

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

6. Control Structures

Microprocessor & Assembly Language

Software based Finite State Machine (FSM) with general purpose processors

MACHINE ARCHITECTURE & LANGUAGE

UNIT 4 Software Development Flow

EE282 Computer Architecture and Organization Midterm Exam February 13, (Total Time = 120 minutes, Total Points = 100)

Intel 8086 architecture

Two-way selection. Branching and Looping

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Instruction Set Architecture (ISA)

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

M6800. Assembly Language Programming

Adding and Subtracting Positive and Negative Numbers

X86-64 Architecture Guide

VLIW Processors. VLIW Processors

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Chapter 9 Computer Design Basics!

MICROPROCESSOR AND MICROCOMPUTER BASICS

Systems I: Computer Organization and Architecture

WAR: Write After Read

3D GRAPHICS OPTIMIZATIONS FOR ARM ARCHITECTURE

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Building A RISC Microcontroller in an FPGA

RTOS Debugger for ecos

ARM and Thumb -2 Instruction Set Quick Reference Card

Lecture 2. Binary and Hexadecimal Numbers

MACHINE INSTRUCTIONS AND PROGRAMS

The programming language C. sws1 1

Lecture 8: Binary Multiplication & Division

CHAPTER 7: The CPU and Memory

Review: MIPS Addressing Modes/Instruction Formats

The Operating System and the Kernel

How To Write A Microsoft Microsoft 8D (Droid) (Program) (Powerbook) (I386) (Microsoft) (Donga) (Opera) And (Dungeo) (Dugeo

Introduction. What is an Operating System?

Lecture #21 April 2, 2004 Relays and Adder/Subtractors

CS352H: Computer Systems Architecture

UNIVERSITY OF CALIFORNIA, DAVIS Department of Electrical and Computer Engineering. EEC180B Lab 7: MISP Processor Design Spring 1995

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Zuse's Z3 Square Root Algorithm Talk given at Fall meeting of the Ohio Section of the MAA October College of Wooster

Compilers I - Chapter 4: Generating Better Code

Faculty of Engineering Student Number:

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Cortex-A9 MPCore Software Development

İSTANBUL AYDIN UNIVERSITY

What Is Recursion? Recursion. Binary search example postponed to end of lecture

IA-64 Application Developer s Architecture Guide

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Lecture 22: C Programming 4 Embedded Systems

1 Description of The Simpletron

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

The string of digits in the binary number system represents the quantity

Overview of the Cortex-M3

ARM VIRTUALIZATION FOR THE MASSES. Christoffer Dall

(Refer Slide Time: 00:01:16 min)

Section 1. Inequalities

Administrative Issues

Chapter 5 Instructor's Manual

Transcription:

Topic 7: Control Flow Instructions CSE 30: Computer Organization and Systems Programming Summer Session II Dr. Ali Irturk 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 31 28 27 24 23 16 15 8 7 6 5 4 0 N Z C V Q JU n d e f i n e d I F T mode f s x c 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 J bit Architecture 5TE/J only Indicates if saturation has occurred 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

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

Suffix EQ NE CS/HS CC/LO MI PL VS VC HI LS GE LT GT LE AL Condition Codes The possible condition codes are listed below Note AL is the default and does not need to be specified Description Equal Not equal Unsigned higher or same Unsigned lower Minus Positive or Zero Overflow No overflow Unsigned higher Unsigned lower or same Greater or equal Less than Greater than Less than or equal Always Flags tested Z=1 Z=0 C=1 C=0 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

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

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

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 Compile by hand if (i == j) f=g+h; else f=g-h; (true) i == j f=g+h i == j? f=g-h (false) i!= j Final compiled MIPS 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.

Simple loop in C; do{ g--; Loops in C/Assembly 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: Loop SUB 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 < and > as well. Use CMP and BLE, BLT, BGE, BGT Examples: if (f < 10) goto Loop; => if (f >= i) goto Loop; => CMP r0,#10 BLT 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 Exit ; k==3 so f=i-j

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

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.

Instructions so far: Previously: Conclusion 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}