Chapter 1 C for Embedded Systems.

Slides:



Advertisements
Similar presentations
Goal: Write Programs in Assembly
Advertisements

COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
Binary numbers and arithmetic. ADDITION Addition (decimal)
Development. Development Environment Editor Assembler or compiler Embedded emulator/debugger IAR Embedded Workbench Kickstart Code Composer Essentials.
Lecture 5.
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
CHAPTER 6 ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS.
Microprocessor & Assembly Language
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Lecture 4: Digital Systems & Binary Numbers (4)
Representing Positive and Negative Numbers
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Computer Architecture & Operations I
CSE 220 – C Programming Bitwise Operators.
The Machine Model Memory
Chapter 7: Expressions and Assignment Statements
Status Register Status = system byte (supervisor only) + user byte = system status + condition code register usually, it is not important to know.
Digital Logic & Design Dr. Waseem Ikram Lecture 02.
Microprocessor Systems Design I
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Manipulating Bit Fields in C
Morgan Kaufmann Publishers
Instructor: David Ferry
Microprocessor Systems Design I
Chapter 7: Expressions and Assignment Statements
Chapter 3 - Operators Arithmetic Operators Assignment
Computer Architecture & Operations I
Integer Representations and Arithmetic
Bit Operations Horton pp
CS213 Integers Topics Numeric Encodings Programming Implications
CS 105 “Tour of the Black Holes of Computing”
CS 105 “Tour of the Black Holes of Computing”
COMP3221: Microprocessors and Embedded Systems
Fundamental Data Types
Bit-Level Discussion of Numeric Types
Chapter 1 C for Embedded Systems.
Conversions of the type of the value of an expression
Unit 2 Programming.
The University of Adelaide, School of Computer Science
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
MISP Assembly.
Computer Architecture & Operations I
Digital Logic & Design Lecture 02.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Shift & Rotate Instructions)
CS/COE0447 Computer Organization & Assembly Language
1 The Hardware/Software Interface CSE351 Spring 2011 Module 3: Integers Monday, April 4, 2011.
Shift & Rotate Instructions)
CS/COE0447 Computer Organization & Assembly Language
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Differences between Java and C
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
Comp Org & Assembly Lang
Computer Organization COMP 210
Binary to Decimal Conversion
Fundamental Data Types
Computer Organization COMP 210
Operations and Arithmetic
MIPS Assembly.
ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS
Module 10 Operations on Bits
Number Systems and Circuits for Addition
Computer Organization and Assembly Language
Bit Manipulations CS212.
Part I Data Representation and 8086 Microprocessors
Bit Operations Horton pp
Two’s Complement & Binary Arithmetic
Presentation transcript:

Chapter 1 C for Embedded Systems

Chapter Review Section 1.1: C Data types for Embedded Systems Section 1.2: Bit-wise Operations in C

Sizes of Data Types three methods to find out the exact sizes of the data types: Read the compiler manual. Use pseudo function sizeof(). Use C99 data types.

Why should I care about which data type to use? Performance Overflow coercion

Performance Using 64-bit data type for saving 32-bit variable will cause: Waste RAM resource Twice RAM access time Additional arithmetic instructions

ANSI C (ISO C89) integer data types and their ranges Size Range Min Range Max char 1 byte -128 127 unsigned char 255 short int 2 bytes -32,768 32,767 unsigned short int 65,535 int 4 bytes -2,147,483,648 2,147,483,647 unsigned int 4,294,967,295 long unsigned long 0 to 4,294,967,295 long long 8 bytes -9,223,372,036,854,775,808 9,223,372,036,854,775,807 unsigned long long 18,446,744,073,709,551,615

Overflow Unlike assembly language programming, high level language programs do not provide indications when overflow occurs and the program just fails silently. If you use a short int to hold the number of seconds of a day, the second count will overflow from 32,767 to -32,768. Even if your program handles negative second count, the time will jump back to the day before.

Coercion If you write a statement with different operand data types for a binary operation, the compiler will convert the smaller data type to the bigger data type. These implicit data type is called coercion. The compiler may or may not give you warning when coercion occurs. If the variable is signed and the data sized is increased, the new bits are filled with the sign bit (most significant bit) of the original value. When you assign a larger data type to a smaller data type variable, the higher order bits will be truncated.

ISO C99 integer data types and their ranges Size Range Min Range Max int8_t 1 byte -128 127 uint8_t 0 to 255 int16_t 2 bytes -32,768 32,767 uint16_t 65,535 int32_t 4 bytes -2,147,483,648 2,147,483,647 uint32_t 4,294,967,295 int64_t 8 bytes -9,223,372,036,854,775,808 9,223,372,036,854,775,807 uint64_t 18,446,744,073,709,551,615

Bit-wise operators in C AND (A & B) OR (A | B) EX-OR (A^B) Invert ~B 1

Setting and Clearing (masking) bits Anything ORed with a 1 results in a 1; anything ORed with a 0 results in no change. Anything ANDed with a 1 results in no change; anything ANDed with a 0 results in a zero. Anything EX-ORed with a 1 results in the complement; anything EX-ORed with a 0 results in no change.

Testing bit with bit-wise operators in C When it is necessary to test a given bit to see if it is high or low, the unused bits are masked and then the remaining data is tested. Example: if (var1 & 0x20)

Bit-wise shift operation in C Symbol Format of Shift Operation Shift Right >>  data >> number of bit-positions to be shifted right Shift Left <<  data << number of bit-positions to be shifted left

Its equivalent using compound operators Instruction Its equivalent using compound operators a = a + 6; a += 6; a = a – 23; a –= 23; y = y * z; y *= z; z = z / 25; z /= 25; w = w | 0x20; w |= 0x20; v = v & mask; v &= mask; m = m ^ togBits; m ^= togBits;

Bit-wise operations using compound operators The majority of hardware access level code involves setting a bit or bits in a register, clearing a bit or bits in a register, toggling a bit or bits in a register, and monitoring the status bits. For the first three cases, the compound operators are very suitable.

Using shift operator to generate mask One way to ease the generation of the mask is to use the left shift operator. To generate a mask with bit n set to 1, use the expression: 1 << n If more bits are to be set in the mask, they can be “or” together. To generate a mask with bit n and bit m set to 1, use the expression: (1 << n) | (1 << m) register |= (1 << 6) | (1 << 1);

Setting the value in a multi-bit field register |= 1 << 30; register &= ~(1 << 29); register |= 1 << 28; register &= ~(7 << 28); register |= 5 << 28; register = register & ~(7 << 28) | (5 << 28);