Making Decision – Microprocessor

Slides:



Advertisements
Similar presentations
ARM versions ARM architecture has been extended over several versions.
Advertisements

CENG 311 Decisions in C/Assembly Language
Chapter 3 Introduction to the 68000
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
Comp Sci Control structures 1 Ch. 5 Control Structures.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
CONDITION CODE AND ARITHMETIC OPERATIONS – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson.
Lecture 5: Decision and Control CS 2011 Fall 2014, Dr. Rozier.
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.
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.
CS2422 Assembly Language & System Programming October 17, 2006.
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.
Conditional Processing If … then … else While … do; Repeat … until.
ARM Instructions I Prof. Taeweon Suh Computer Science Education Korea University.
CEG 320/520: Computer Organization and Assembly Language ProgrammingFlow Control 1 Flow Control.
FUNCTION – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
BITWISE OPERATIONS – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
ITEC 352 Lecture 14 ISA(6). Review Questions? Beginning / End Memory locations Variable / Memory syntax PSR Loops / Branches.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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.
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.
Review : C Programming Language
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 11 Conditional Operations.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
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.
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),
Computer Architecture & Operations I
Deeper Assembly: Addressing, Conditions, Branching, and Loops
CS2100 Computer Organisation
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
Computer Architecture & Operations I
ECE 3430 – Intro to Microcomputer Systems
Decision Making.
Decision Making.
Processor Instructions set. Learning Objectives
More on logical instruction and
Microprocessor and Assembly Language
Instructions for Making Decisions
ARM Control Structures
CSC 3210 Computer Organization and Programming
ECE 3430 – Intro to Microcomputer Systems
The University of Adelaide, School of Computer Science
COMS 361 Computer Organization
ARM Control Structures
March 2006 Saeid Nooshabadi
Branching instructions
Overheads for Computers as Components 2nd ed.
MIPS Assembly.
March, 2006 Saeid Nooshabadi
MIPS assembly.
Branch & Call Chapter 4 Sepehr Naimi
An Introduction to the ARM CORTEX M0+ Instructions
MIPS instructions.
Presentation transcript:

Making Decision 353156 – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat

Review: C Decision (Control Flow) 2 kinds of if statement in C if (condition) statement if (condition) statement1 else statement2 Example 1: if ( a == 5 ) printf(“Hello”); Example 2 : if ( a == 5 ) printf(“Hello”); else printf(“Hi”);

Using Goto and Label C syntax : Goto and Label syntax : if ( a == 5 ) printf(“Hello”); else printf(“Hi”); Goto and Label syntax : if ( a == 5) goto Label1; printf(“Hi”); goto Label2; Label1: printf(“Hello”); Label2: a = 5 ? true false printf(“Hello”) printf(“Hi”)

ARM Decision Instructions (Control Flow) Decision Instruction in ARM CMP register1, operand2 ;compare register1 with operand 2 BEQ Label ; branch to Label if equal BNE Label ; branch to Label if not equal B Label ; branch to Label C Code if ( a == 5) goto Label1; a = a + 5; goto Label2; Label1: a = a - 5; Label2: ARM Code (a maps to R0) CMP R0, #5 ;if (a == 5) BEQ Label1 ; goto Label1 ADD R0, R0, #5 ;a = a + 5 B Label2 ; goto Label2 Label1 SUB R0, R0, #5 ; a = a - 5 Label2

Transform to ARM assembly Example 1 Convert the following C program to ARM assembly. Assume that a maps to R0, and b maps to R1 if ( a == 5 ) goto Endif b = a + 5; a = a - 2; Endif: a = a + b; if ( a != 5 ) { b = a + 5; a = a - 2; } a = a + b; Transform to goto-label Do if condition is true Transform to ARM assembly CMP R0, #5 BEQ Endif ADD R1, R0, #5 SUB R0, R0, #2 Endif ADD R0, R0, R1 Do always

Exercise 1 Convert the following C program to ARM assembly. Assume that a maps to R0, and b maps to R1 if ( a != 10) { b = a + 10; a = a - 5; } else { b = a + 20; a = a – 10; a = a + b; Do if condition is true Do if condition is false Do always

Simple Loop (1) Simple loop in C (goto-label) Loop: i = i + 1; if (i != 5) goto Loop; ARM Assembly (i maps to R0) Loop ADD R0, R0, #1 CMP R0, #5 BNE Loop i = i + 1 i != 5 ? true false

Simple Loop (2) In C, normally we use while or for instead of goto- label while( i != 10) i = i + 1; ARM Assembly (i maps to R0) Loop CMP R0, #10 BEQ EndLoop ADD R0, R0, #1 B Loop EndLoop i != 10 ? false true i = i + 1

More on Branch Instructions So far, we branch on Uncondition, Equal, and Not Equal (B, BEQ, and BNE) How about Greater than > Greater than or equal >= Less than < Less than or equal <= Lucky for us, ARM has these instructions for you. BGT (Branch greater than) BGE (Branch greater than or equal) BLT (Branch less than) BLE (Branch less than or equal)

Transform to ARM assembly Example 2 Convert the following C program to ARM assembly. Assume that a maps to R0, and b maps to R1 WhileLoop: if ( a >= 5 ) goto EndWhile a = a + 1; b = b + 2; goto WhileLoop EndWhile: while (a < 5) { a = a + 1; b = b + 2; } Transform to goto-label Transform to ARM assembly WhileLoop CMP R0, #5 BGE EndWhile ADD R0, R0, #1 ADD R1, R1, #2 B WhileLoop EndWhile

Exercise 2 while ( a <= 10) { b = a + 10; a = a - 5; } a = a + b; Convert the following C program to ARM assembly. Assume that a maps to R0, and b maps to R1 while ( a <= 10) { b = a + 10; a = a - 5; } a = a + b;

If- else if - else In C : if – else if - else if (k == 1) a = a + 5; else a = a – 5; In ARM : (R0 = k, R1 = a) CMP R0, #1 BNE Cond2 ADD R1, R1, #5 B Endif Cond2 CMP R0, #2 BNE Cond3 ADD R1, R1, #10 Cond3 CMP R0, #3 BNE ElseCond SUB R1, R1, #10 ElseCond SUB R1, R1, #5 Endif What is the a value after program is executed if assume a = 20 and k = 1 k = 2 k = 3 k = 10

Comparing unsigned data Up until now, BGT, BGE, BLT, and BLE use to compare signed data NOT unsigned data. Example (8-bit register) A1 = 0xF5 , A2 = 0x05 So A1 > A2 or A2 > A1 ?? Signed : A1 = -1110, A2 = 510 (A2 > A1) Unsigned : A1 = 24510, A2 = 510 (A1 > A2) In ARM, there are instruction for comparing and branching unsigned data BLO Branch Lower (unsigned) BLS Branch Less or Same (unsigned) BHI Branch Higher (unsigned) BHS Branch Higher or Same (Unsigned)

Example 3 What is the value in R2 after program is executed ? (1) (2) LDR R0, =0xFFFF5011 LDR R1, =0x000ABCD MOV R2, #5 CMP R0, R1 BGE Exit ADD R2, R2, #1 Exit LDR R0, =0xFFFF5011 LDR R1, =0x000ABCD MOV R2, #5 CMP R0, R1 BHS Exit ADD R2, R2, #1 Exit

How can branch instruction know the output of CMP instruction ? In the process of comparison and branching (CMP), actually, processor does the following steps : CMP instruction does operand1 – operand2 Update CPSR flags Branch instruction checks CPSR flags and does the operation Review : CPSR

CMP and CSPR (=) CMP R0, R1 ; it does R0 – R1 R0 = R1 (R0 = 0x15, R1 = 0x15) 0 0 0 1 0 1 0 1 + 1 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 N = 0, Z = 1, C = 1, V = 0 Thus, if R0 = R1 then Z = 1

CMP and CSPR (!=) CMP R0, R1 ; it does R0 – R1 R0 != R1 (R0 = 0x15, R1 = 0x10) 0 0 0 1 0 1 0 1 + 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 1 N = 0, Z = 0, C = 1, V = 0 Thus, if R0 != R1 then Z = 0

CMP and CSPR (unsigned data >=) CMP R0, R1 ; it does R0 – R1 R0 >= R1 (R0 is higher than or same as R1) R0 = 0xF0, R1=0x05 1 1 1 1 0 0 0 0 + 1 1 1 1 1 0 1 1 1 0 0 0 0 1 0 1 1 R0 = 0x06, R1 = 0x05 0 0 0 0 0 1 1 0 + 1 0 0 0 0 0 0 0 1 N = ?, Z = 0, C = 1, V = 0

CMP and CSPR (unsigned data <) CMP R0, R1 ; it does R0 – R1 R0 < R1 (R0 is lower than R1) R0 = 0x05, R1=0xF0 0 0 0 0 0 1 0 1 + 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 1 R0 = 0x05, R1 = 0x06 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 N = ?, Z = 0, C = 0, V = 0

Conclusion : CPSR Flags for CMP Unsigned Data CMP R0, R1 N Z C V Remark X 1 R0 = R1 R0 != R1 R0 >= R1 (higher or same) R0 < R1 (lower) X 1 R0 > R1 (higher) R0 != R1 AND R0 >= R1 X 1 R0 <= R1 (lower or same) R0 = R1 OR R0 < R1

CMP and CSPR (signed data >=) CMP R0, R1 ; it does R0 – R1 R0 >= R1 (R0 is greater than or equals R1) R0 – R1 (always positive N = 0, V = 0) Exceptional case : when result of subtraction is overflow R0 = 0x75, R1=0x85 0 1 1 1 0 1 0 1 + 0 1 1 1 1 0 1 1 0 1 1 1 1 0 0 0 0 Carry In (1) XOR Carry Out (0) => V = 1 N = 1 Thus, if N = V then R0 is greater than or equals to R1

CMP and CSPR (signed data <) CMP R0, R1 ; it does R0 – R1 R0 < R1 (R0 is less than R1) R0 – R1 (always negative N = 1, V = 0) Exceptional case : when result of subtraction is overflow R0 = 0x85, R1=0x75 1 0 0 0 0 1 0 1 + 1 0 0 0 1 0 1 1 1 0 0 0 1 0 0 0 0 Carry In (0) XOR Carry Out (1) => V = 1 N = 0 Thus, if N != V then R0 is greater than or equals to R1

Conclusion : CPSR Flags for CMP Signed Data CMP R0, R1 N Z C V Remark X 1 R0 = R1 R0 != R1 R0 >= R1 (greater than or equal) R0 < R1 (less than) 1 X R0 > R1 (greater than) R0 != R1 AND R0 >= R1 X 1 R0 <= R1 (less than or equal) R0 = R1 OR R0 < R1

Flag Setting Instructions Compare CMP R0, R1 ;update flags after R0 – R1 Compare Negated CMN R0, R1 ;update flags after R0 + R1 Test TST R0, R1 ;update flags after R1 AND R2 Test Equivalence TEQ R0, R1 ;update flags after R1 EOR R2

Assignment 7 int a = 0, b = 0; while ( b <= 10) { if ( a < 5) Convert the following part of C program to ARM Assembly int a = 0, b = 0; while ( b <= 10) { if ( a < 5) a = a + 10; else a = a - 5; b = b + 1; } a = a + b; a = 0, b = 0 b <= 10 ? false true a < 5 ? true false a = a + 10 a = a - 5 b = b + 1 a = a + b