SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Similar documents
SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Computer Organization & Systems Exam I Example Questions

LAB A Translating Data to Binary

Beginning C Programming for Engineers

More on C programming

5.Coding for 64-Bit Programs

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

SISTEMI&EMBEDDED& AA&2013/2014&

Embedded Systems - FS 2018

Internal representation. Bitwise operators

Internal representation. Bitwise operators

Internal representation. Bitwise operators

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Bitwise Operators. Fall Jinkyu Jeong GEBD029: Basis and Practice in Programming Fall 2014 Jinkyu Jeong

A Fast Review of C Essentials Part I

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Embedded Systems - FS 2018

Embedded Systems - FS 2018

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

ME 461 C review Session Fall 2009 S. Keres

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Fundamental of Programming (C)

Fundamentals of Programming

ISA 563 : Fundamentals of Systems Programming

Topic 6: A Quick Intro To C

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

fpp: Fortran preprocessor March 9, 2009

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Basic Types and Formatted I/O

SISTEMI EMBEDDED. Basic Concepts about Computers. Federico Baronti Last version:

Unit 4 Preprocessor Directives

Integer Representation

Have examined process Creating program Have developed program Written in C Source code

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

AGENDA Binary Operations CS 3330 Samira Khan

SAINT2. System Analysis Interface Tool 2. Emulation User Guide. Version 2.5. May 27, Copyright Delphi Automotive Systems Corporation 2009, 2010

EL6483: Brief Overview of C Programming Language

CSCI 2212: Intermediate Programming / C Chapter 15

C and C++ 2. Functions Preprocessor. Alan Mycroft

SIGNED AND UNSIGNED SYSTEMS

CSE 303 Lecture 18. Bitwise operations. reading: Programming in C Ch. 12. slides created by Marty Stepp

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Number Systems, Scalar Types, and Input and Output

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Work relative to other classes

A flow chart is a graphical or symbolic representation of a process.

THE FUNDAMENTAL DATA TYPES

C Language Programming

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Motivation was to facilitate development of systems software, especially OS development.

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Lecture 2 Tao Wang 1

Motivation was to facilitate development of systems software, especially OS development.

Bits, Bytes and Integers

Chapter 7. Basic Types

Selection from Susta:Computer System Structures & John Loomis: Computer organization & M.Mudawar:Computer Architecture & Assembly Language. Cvičení 1.

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Exercise Session 2 Simon Gerber


Programming. Data Structure

Types, Variables, and Constants

Fundamental of C programming. - Ompal Singh

CS 241 Data Organization Binary

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Lecture 2: C Programming Basic

Programing in C. Pierre-Alain FOUQUE

OBJECT ORIENTED PROGRAMMING USING C++

Programming, numerics and optimization

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Arithmetic and Bitwise Operations on Binary Data

Appendix. Bit Operators

Applied Computer Programming

AGH University of Science and Technology Cracow Department of Electronics

Chapter 7: Preprocessing Directives

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS Programming In C

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Goals for this Week. CSC 2400: Computer Systems. Bits, Bytes and Data Types. Binary number system. Finite representations of binary integers

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

UNIT- 3 Introduction to C++

Course Outline Introduction to C-Programming

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

srl - shift right logical - 0 enters from left, bit drops off right end note: little-endian bit notation msb lsb "b" for bit

Bit Manipulation in C

Transcription:

SISTEMI EMBEDDED The C Pre-processor Fixed-size integer types Bit Manipulation Federico Baronti Last version: 20170307

The C PreProcessor CPP (1) CPP is a program called by the compiler that processes the text of the program before its actual translation It basically does the following operations: Includes the content of other files (usually header files) Expands the SYMBOLS with their related definitions Includes/Excludes part of the code to the text that will be actually compiled These actions are controlled by directives A directive is a single code line that starts with # You can use the character \ to go to a new line within the same directive

The C PreProcessor (2) Inclusion of header files (files with.h extension that contains only declarations). E.g. #include <stdint.h> #include my_header.h The file stdint.h is searched in a standard directory list; my_header.h is searched in the same directory as the including source file The list of directories searched for header files can be changed with a compiler option

The C PreProcessor (3) First_Nios2_Prog.c /* */ #include../firstnios2_prog_bsp/system.h /* */ Compiling First_Nios2_Prog.c (other compiler options omitted) nios2-elf-gcc -c o First_Nios2_Prog.o First_Nios2_Prog.c Header path can be omitted by using -Idir compiler option /* */ #include system.h /* */ First_Nios2_Prog.c Compiling First_Nios2_Prog.c (other compiler options omitted) nios2-elf-gcc I../FirstNios2_Prog_bsp/ -c o First_Nios2_Prog.o First_Nios2_Prog.c

The C PreProcessor (4) Macro is a symbol that is replaced with its definition before compilation (it can be followed by one or or more arguments). E.g. of macro def. #define MASK 0xF #define MAX(A,B) ((A) > (B)? (A) : (B)) The instructions: b = a & MASK; y = 1 + MAX(10,x); are expanded by the preprocessor to: b = a & 0xF; y = 1 + ((10) > (x)? (10) : (x));

The C PreProcessor (5) Macro are largely used in C programming of embedded systems to access peripheral registers. E.g. of definition: #include system.h #define RED_LEDS_DATA_REG \ (*(volatile unsigned int*) RED_LEDS_BASE) #define SLIDER_DATA_REG \ (*(volatile unsigned int*) SLIDER_SWITCHES_BASE) E.g. of use: RED_LEDS_DATA_REG = SLIDER_DATA_REG; /* Show the status of the slider switches on the red leds */

The C PreProcessor (6) The macro name_of_the_macro exists from its definition to the end of the file or when it is undefined using the directive: #undef name_of_the_macro A macro can also be defined with an option passed to the compiler: -D name_of_the_macro=def Do a large use of parenthesis to avoid unintended behaviors when the MACRO is expanded Write macro SYMBOLS with all CAPITAL letters

The C PreProcessor (7) Conditional compilation makes it possible to include/exclude code segments if certain expressions evaluated by the preprocessor are true or false. E.g. #ifdef DEBUG printf( Debug mode enabled\n ); /* or any other code that we want to include for debug purposes */ #endif #define DEBUG 1 includes the debug code

The C PreProcessor (8) A common use of conditional compilation is to avoid multiple inclusions of a header file. To this end, start the header file, say config.h, with: #ifndef CONFIG_H_ #define CONFIG_H_ and end it with: #endif /* CONFIG_H_ */ After the first inclusion of my_header.h, the symbol MY_HEADER_H is defined. Thus, further inclusions are filtered out by the conditional compilation directives

Integer types 2 basic integer types: char, int and some type-specifiers: sign: signed, unsigned size: short, long The actual size of an integer type depends on the compiler implementation sizeof(type) returns the size (in number of bytes) used to represent the type argument sizeof(char) sizeof(short) sizeof(int) sizeof(long)... sizeof(long long)

Fixed-size integers (1) In embedded system programming integer size is important Controlling minimum and maximum values that can be stored in a variable Increasing efficiency in memory utilization Managing peripheral registers To increase software portability, fixed-size integer types can be defined in a header file using the typedef keyword

Fixed-size integers (2) C99 update of the ISO C standard defines a set of standard names for signed and unsigned fixed-size integer types 8-bit: int8_t, uint8_t 16-bit: int16_t, uint16_t 32-bit: int32_t, uint32_t 64-bit: int64_t, uint64_t These types are defined in the standardlibrary header file stdint.h

Fixed-size integers (3) Altera HAL (Hardware Abstraction Layer) also provides the header file alt_types.h (<project_name_bsp>/hal/inc/) with definition of fixed-size integer types: typedef signed char typedef unsigned char typedef signed short typedef unsigned short typedef signed long typedef unsigned long typedef long long typedef unsigned long long alt_8; alt_u8; alt_16; alt_u16; alt_32; alt_u32; alt_64; alt_u64; These type definitions are used in Altera HAL source files. To increase portability, you d better code using C99 fixed-size integer types (including the header file stdint.h)

Logical operators Integer data can be interpreted as logical values in conditions (if, while,...) or in logical expressions: = 0, FALSE ANY OTHER VALUE, TRUE Logical operators: AND && OR NOT! Integer data can store the result of a logical expressions: 1 (TRUE), 0 (FALSE)

Bitwise operators (1) Operate on the bits of the operand/s AND & OR XOR ^ NOT ~ SHIFT LEFT << SHIFT RIGHT >>

Shift operators A << n The result is the bits of A moved to the left by n positions and padded on the right with 0 It is equivalent to multiply A by 2 n if the result can be represented A >> n The result is the bits of A moved to the right by n positions and padded on the left with 0 if type of A is unsigned or with the MSB of A if type is signed It is equivalent to divide A by 2 n

Bit manipulation (1) << and operands can be used to create expressive binary constants by specifying the positions of the bits equal to 1 E.g. (1<<7) (1<<5) (1<<0) = 0xA1 (10100001) Better not to use magic numbers as 7, 5 and 0. Use instead symbolic names to specify bit positions For instance, the symbolic names can reflect the function of the bit within a peripheral register (1<<x) can be encapsulated into a macro: #define BIT(x) (1<<(x))

Bit manipulations (2) Altering only the bits in given positions E.g. bits: 7, 5, 0 #define MSK = BIT(7) BIT(5) BIT(0) Clearing bits A &= ~MSK; Setting bits A = MSK; Toggling bits A ^= MSK;

Bit manipulations (3) Testing bits E.g. do something if bit 0 (LSB) of A is set, regardeless of the other bits of A if (A & BIT(0)) { /* some code here */ }

Putting into practice (1) Write a program that shows on the 7-seg display HEX3-HEX0 the sizes in number of bytes of long long, long, short and char integer data types Do they match with the definitions of fixed-size integer types in alt_types.h?

Putting into practice (2) 7-seg display Parallel Ports Address 0x10000020 31 30 24... 23 22 16... 15 14... 8 7 6... 0 Data register 0 5 6 1 HEX3 6-0 HEX2 6-0 HEX1 6-0 HEX0 6-0 4 3 2 Segments 0x10000030 31 30 24... 23 22 16... 15 14... 8 7 6... 0 Data register HEX7 6-0 HEX6 6-0 HEX5 6-0 HEX4 6-0

Putting into practice (3) To go on: 1. Show on the 4x 7-Seg HEX3_HEX0 display the 4 hexadecimal digits of the 16-bit unsigned number (Sw 15 -Sw 0 ) 2. Show on the 4x 7-Seg HEX3_HEX0 display the 4 decimal digits of the 16-bit unsigned number (Sw 15 -Sw 0 ) if the number can be represented; E otherwise 3. Allow the user to choice the representation between hexadecimal and decimal by the slider Sw 17

Putting into practice (4) To go on: 4. Show on the 4x 7-Seg HEX3_HEX0 display the module of the 16-bit signed number (Sw 15 -Sw 0 ) and on LEDG 8 the sign of the number (LEDG 8 is ON if and only if the number is negative). Show the module using hexadecimal and decimal digits as before 5. Allow the user to choice if (Sw 15 -Sw 0 ) code an unsigned or signed number by the slider Sw 16 6. Combine all the features in a single program

References Altera Basic Computer System for the Altera DE2 Board Altera Parallel Port for Altera DE-Series Boards