4-1 EE 319K Introduction to Embedded Systems Lecture 4: Arithmetic overflow, Branches, Control Structures, Abstraction & Refinement Bard, Erez, Gerstlauer,

Slides:



Advertisements
Similar presentations
EE 319K Introduction to Embedded Systems
Advertisements

11-1 Bard, Gerstlauer, Valvano, Yerraballi EE 319K Introduction to Embedded Systems Lecture 11: Data Acquisition, Numerical Fixed-Point Calculations, Lab.
13-1 Bard, Gerstlauer, Valvano, Erez, Telang, Yerraballi EE 319K Introduction to Embedded Systems Lecture 13: 2-D Arrays, Bitmaps, Sprites, Structs, Lab.
14-1 Bard, Gerstlauer, Valvano, Yerraballi EE 319K Introduction to Embedded Systems Lecture 14: Gaming Engines, Coding Style, Floating Point.
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.
EE 319K Introduction to Embedded Systems
Ch.3 Representation and Manipulation of Information From Introduction to Embedded Systems: Interfacing to the Freescale 9s12 by Valvano, published by CENGAGE.
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Week 3. Assembly Language Programming  Difficult when starting assembly programming  Have to work at low level  Use processor instructions >Requires.
Control Structures in ARM Implementation of Decisions Similar to accumulator instructions One instruction sets the flags, followed by another instruction.
1-1 Bard, Gerstlauer, Valvano, Yerraballi EE 319K Introduction to Microcontrollers Lecture 1: Introduction, Embedded Systems, Product Life-Cycle, ARM Programming.
EE 319K Introduction to Microcontrollers
10-1 Bard, Tiwari, Telang, Janapa Reddi, Gerstlauer, Valvano, Yerraballi EE 319K Introduction to Embedded Systems Lecture 10: Sampling, Analog-to-Digital.
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
Lecture 5: Decision and Control CS 2011 Fall 2014, Dr. Rozier.
Flow Control Instructions
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
11-1 Bard, Tiwari, Telang, Janapa Reddi, Gerstlauer, Valvano, Yerraballi EE 319K Introduction to Embedded Systems Lecture 11: Data Acquisition, Numerical.
6-1 EE 319K Introduction to Embedded Systems Lecture 6: SysTick Timer, Exam 1 Review Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi,
EE 319K Introduction to Embedded Systems
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
2-1 EE 319K Introduction to Embedded Systems Lecture 2: I/O, Logic/Shift Operations, Addressing modes, Memory Operations, Subroutines, Introduction to.
ARM Instructions I Prof. Taeweon Suh Computer Science Education Korea University.
Simple Data Type Representation and conversion of numbers
1 CS/COE0447 Computer Organization & Assembly Language Chapter 3.
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
ECE 2110: Introduction to Digital Systems Signed Addition/Subtraction.
Making Decision – Microprocessor
Lecture 4 ( Assembly Language).
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
4-1 EE 319K Introduction to Microcontrollers Lecture 4: Debugging, Arithmetic Operations, Condition Code Bits.
Arithmetic Flags and Instructions
(Flow Control Instructions)
ECS642U Embedded Systems ARM CPU and Assembly Code William Marsh.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 4 –Binary Arithmetic These are lecture notes to accompany the book SPARC.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Light Sensors ECE 4437 Dr. J. Glover Team 9: Jitin George Zachary Freeman.
MIPS ALU. Building from the adder to ALU ALU – Arithmetic Logic Unit, does the major calculations in the computer, including – Add – And – Or – Sub –
EE 319K Introduction to Embedded Systems
Lecture 2: Advanced Instructions, Control, and Branching EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
Outline Binary Addition 2’s complement Binary Subtraction Half Adder
EE 319K Introduction to Embedded Systems
3.4 Addressing modes Specify the operand to be used. To generate an address, a segment register is used also. Immediate addressing: the operand is a number.
ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.
Lecture 6: Decision and Control CS 2011 Spring 2016, Dr. Rozier.
5-1 EE 319K Introduction to Microcontrollers Lecture 5: Conditionals, Loops, Modular Programming, Sub- routines, Parameter passing.
Status Register Status = system byte (supervisor only) + user byte = system status + condition code register usually, it is not important to know.
EE 319K Introduction to Embedded Systems
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.
EE 319K Introduction to Embedded Systems
Making Decisions and Writing Loops
ICS312 SET 7 Flags.
ECE 3430 – Intro to Microcomputer Systems
Decision Making.
Processor Instructions set. Learning Objectives
More on logical instruction and
ECE 3430 – Intro to Microcomputer Systems
Lecture 4 ( Assembly Language).
Flags Register & Jump Instruction
Morgan Kaufmann Publishers Computer Organization and Assembly Language
ECE 3430 – Intro to Microcomputer Systems
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ARM Control Structures
March 2006 Saeid Nooshabadi
Branching instructions
COMS 361 Computer Organization
Branch & Call Chapter 4 Sepehr Naimi
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

4-1 EE 319K Introduction to Embedded Systems Lecture 4: Arithmetic overflow, Branches, Control Structures, Abstraction & Refinement Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-2 Agenda  Recap  Debugging  I/O oSwitch and LED interfacing  C Programming oRandom number generator, NOT gate in Keil  Outline  Arithmetic Overflow  Conditional Branches  Conditional and Iterative Statements oif, while, for (In assembly and C)  Abstraction & Refinement oDevice Driver Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-3 Condition Codes  C set after an unsigned addition if the answer is wrong  C cleared after an unsigned subtract if the answer is wrong  V set after a signed addition or subtraction if the answer is wrong BitNameMeaning after add or sub Nnegativeresult is negative Zzeroresult is zero Voverflowsigned overflow Ccarryunsigned overflow Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

bit unsigned number wheel C bit Cleared C bit Set  The carry bit, C, is set after an unsigned addition when the result is incorrect.  The carry bit, C, is clear after an unsigned subtraction when the result is incorrect. Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

bit unsigned number wheel C bit SetC bit Cleared  The carry bit, C, is set after an unsigned addition when the result is incorrect.  The carry bit, C, is clear after an unsigned subtraction when the result is incorrect. Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-6 Trick (unsigned) 1.Convert both numbers to unsigned 2.Perform addition or subtraction 3.Does the result fit as an unsigned? No -> addition C=1, subtraction C=0 Yes -> addition C=0, subtraction C=1 For example: = 260, C = 1 and the actual answer is = 4 Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

bit signed number wheel V bit ClearedV bit Set  The overflow bit, V, is set after a signed addition or subtraction when the result is incorrect. Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

bit signed number wheel V bit Cleared V bit Set  The overflow bit, V, is normally set when we cross over from 127 to -128 while adding or cross over from -128 to 127 while subtracting. Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-9 Trick (signed) 1.Convert both numbers to signed 2.Perform addition or subtraction 3.Does the result fit as a signed? No -> V=1 Yes -> V=0 8-bit Examples: 10 – 5 = 5, V= – 100 = -200, V=1 Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-10 Addition Summary Let the 32-bit result R be the result of the 32-bit addition X+Y.  N bit is set  if unsigned result is above or  if signed result is negative.  N = R 31  Z bit is set if result is zero  V bit is set after a signed addition if result is incorrect   C bit is set after an unsigned addition if result is incorrect  Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-11 Subtraction Summary Let the 32-bit result R be the result of the 32-bit subtraction X-Y.  N bit is set  if unsigned result is above or  if signed result is negative.  N = R 31  Z bit is set if result is zero  V bit is set after a signed subtraction if result is incorrect   C bit is clear after an unsigned subtraction if result is incorrect  Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-12 Trick Question  Answer = 159  NZVC = 1010  When the subtraction (32 – 129) is performed in an 8-bit system what is the result and the status of the NZVC bits ? Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-13 Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari Unsigned Promotion  Promotion involves increasing the precision of the input numbers, and performing the operation at that higher precision  Then truncate the result back to the original precision Decimal 8-bit 32-bit , ,0000,0000,0000,0000,0000,1110, , ,0000,0000,0000,0000,0000,0100, , ,0000,0000,0000,0000,0001,0010,0000

4-14 Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari Unsigned Ceiling and Floor

4-15 Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari Signed Promotion  To promote a signed number, we duplicate the sign bit Decimal 8-bit 32-bit , ,1111,1111,1111,1111,1111,1010, , ,0000,0000,0000,0000,0000,0100, , ,1111,1111,1111,1111,1111,0110,0000

4-16 Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari Signed Ceiling and Floor

4-17 Conditional Branch Instructions  Unsigned conditional branch  follow SUBS CMN or CMP BLO target ; Branch if unsigned less than (if C=0, same as BCC) BLS target ; Branch if unsigned less than or equal to (if C=0 or Z=1) BHS target ; Branch if unsigned greater than or equal to (if C=1, same as BCS) BHI target ; Branch if unsigned greater than (if C=1 and Z=0) CMP R0,R1 BLO R0<R1 target Next instruction R0≥R1 Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-18 Conditional Branch Instructions  Signed conditional branch  follow SUBS CMN or CMP BLT target ; if signed less than(if (~N&V | N&~V)=1, i.e. if N≠V) BGE target ; if signed greater than or equal to (if (~N&V | N&~V)=0, i.e. if N=V) BGT target ; if signed greater than (if (Z | ~N&V | N&~V)=0, i.e. if Z=0 and N=V) BLE target ; if signed less than or equal to (if (Z | ~N&V | N&~V)=1, i.e. if Z=1 or N≠V) CMP R0,R1 BLT R0<R1 target Next instruction R0≥R1 Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-19 Equality Test Program 5.8. Conditional structures that test for equality. Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-20 Unsigned Conditional Structures Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari Program 5.9. Unsigned conditional structures.

4-21 Signed Conditional Structures Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari Program Signed conditional structures.

4-22 If-then-else Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-23 While Loops LDR R4, =G1 ; R4 -> G1 LDR R5, =G2 ; R5 -> G2 loop LDR R0, [R5] ; R0 = G2 LDR R1, [R4] ; R1 = G1 CMP R0, R1 ; is G2 <= G1? BLS next ; if so, skip to next BL Body ; body of the loop B loop next uint32_t G1,G2; while(G2 > G1){ Body(); } Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-24 For Loops Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-25 For Loops MOV R4, #0 ; R4 = 0 loop CMP R4, #100 ; index >= 100? BHS done ; if so, skip to done BL Process ; process function* ADD R4, R4, #1 ; R4 = R4 + 1 B loop done for(i=0; i<100; i++){ Process(); } MOV R4, #100 ; R4 = 0 loop BL Process ; process function SUBS R4, R4, #1 ; R4 = R4 - 1 BNE loop done for(i=100; i!=0; i--){ Process(); } Count up Count down Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-26 System Design  What does being in a state mean?  List state parameters  What is the starting state of the system?  Define the initial state  What information do we need to collect?  List the input data  What information do we need to generate?  List the output data  How do we move from one state to another?  Actions we could do  What is the desired ending state?  Define the ultimate goal Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-27 System Design  Successive Refinement  Stepwise Refinement  Systematic Decomposition Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-28 System Design  Start with a task and decompose the task into a set of simpler subtasks  Subtasks are decomposed into even simpler sub-subtasks  Each subtask is simpler than the task itself  Make design decisions  document decisions and subtask requirements  Ultimately, subtask is so simple, it can be converted to software Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-29 System Design  Four building blocks:  “do A then do B” → sequential  “do A and B in either order” → sequential (parallel)  “if A, then do B” → conditional  “for each A, do B” → iterative  “do A until B” → iterative  “repeat A over & over forever” → iterative (condition always true)  “on external event do B” → interrupt  “every t msec do B” → interrupt Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-30 Successive Refinement Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-31 Successive Refinement Successive refinement example for iterative approach Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-32 Abstraction - Device Driver Abstraction allows us to modularize our code and give us the option to expose what we want users to see and hide what we don’t want them to see. A Device Driver is a good example where abstraction is used to expose public routines that we want users of the driver to call and use private routines to hide driver internals from the user (more on private routines later) Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari LED Driver (PE0) LED_Init LED_Off LED_On LED_Toggle A user simply has to know what a routine expects and what it returns in order to call it (calling convention). Internals do not matter to caller

4-33 Port E LED Abstraction PE0 EQU 0x4005C004 ;bit-specific address Port E bit 0 LED_Init LDR R1, =SYSCTL_RCGCGPIO_R ; R1 -> SYSCTL_RCGCGPIO_R LDR R0, [R1] ; previous value ORR R0, R0, #0x ; activate clock for Port E STR R0, [R1] NOP NOP ; allow time to finish activating LDR R1, =GPIO_PORTE_DIR_R ; R1 -> GPIO_PORTE_DIR_R LDR R0, [R1] ; previous value ORR R0, R0, #0x01 ; PE0 output STR R0, [R1] ; set direction register LDR R1, =GPIO_PORTE_AFSEL_R ; R1 -> GPIO_PORTE_AFSEL_R LDR R0, [R1] ; previous value BIC R0, R0, #0x01 ; disable alt funct STR R0, [R1] ; set alternate function register LDR R1, =GPIO_PORTE_DEN_R ; R1 -> GPIO_PORTE_DEN_R LDR R0, [R1] ; previous value ORR R0, R0, #0x01 ; enable PE0 digital port STR R0, [R1] ; set digital enable register BX LR Program 4.3. Software interface for an LED on PE0 (SSR_xxx.zip). Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari

4-34 Port E LED Abstraction LED_Off LDR R1, =PE0 ; R1 is 0x4005C004 MOV R0, #0 STR R0, [R1] ; affect just PE0 BX LR LED_On LDR R1, =PE0 ; R1 is 0x4005C004 MOV R0, #1 STR R0, [R1] ; affect just PE0 BX LR LED_Toggle LDR R1, =PE0 ; R1 is 0x4005C004 LDR R0, [R1] ; previous value EOR R0, R0, #1 ; flip bit 0 STR R0, [R1] ; affect just PE0 BX LR Program 4.3. Software interface for an LED on PE0 (SSR_xxx.zip). Bard, Erez, Gerstlauer, Valvano, Yerraballi, Telang, Janapa Reddi, Tiwari