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

Computer Organization & Systems Exam I Example Questions

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

Beginning C Programming for Engineers

Embedded Systems - FS 2018

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

More on C programming

LAB A Translating Data to Binary

5.Coding for 64-Bit Programs

Internal representation. Bitwise operators

Embedded Systems - FS 2018

Internal representation. Bitwise operators

Internal representation. Bitwise operators

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

SISTEMI&EMBEDDED& AA&2013/2014&

A Fast Review of C Essentials Part I

CS107, Lecture 3 Bits and Bytes; 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

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

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

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

Embedded Systems - FS 2018

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

Integer Representation

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

Fundamental of Programming (C)

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

AGENDA Binary Operations CS 3330 Samira Khan

Fundamentals of Programming

ISA 563 : Fundamentals of Systems Programming

CSCI 2212: Intermediate Programming / C Chapter 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.

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

Basic Types and Formatted I/O

Unit 4 Preprocessor Directives

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

C Language Programming

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.

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

Chapter 7. Basic Types

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

Bits, Bytes and Integers

Types, Variables, and Constants

EL6483: Brief Overview of C Programming Language

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

SIGNED AND UNSIGNED SYSTEMS

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

Number Systems, Scalar Types, and Input and Output

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

Arithmetic and Bitwise Operations on Binary Data

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

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

Arithmetic and Bitwise Operations on Binary Data

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

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

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

Lecture 2 Tao Wang 1

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

Review Activity 1 CALL and RET commands in assembler

The C Programming Language Guide for the Robot Course work Module

BLM2031 Structured Programming. Zeyneb KURT

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

Exercise Session 2 Simon Gerber


Programming. Data Structure

Fundamental of C programming. - Ompal Singh

Course Coding Standards

CS 241 Data Organization Binary

Embedded programming, AVR intro

Lecture 2: C Programming Basic

OBJECT ORIENTED PROGRAMMING USING C++

Programing in C. Pierre-Alain FOUQUE

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

Programming, numerics and optimization

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

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

Appendix. Bit Operators

AGH University of Science and Technology Cracow Department of Electronics

Applied Computer Programming

Chapter 7: Preprocessing Directives

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Using peripherals on the MSP430 (if time)

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

Transcription:

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

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: Include the content of other files (usually header files) Expand the SYMBOLS defined by the programmer with their related definitions Include/Exclude part of the text to the code 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) 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 (6) 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 uint32_t*) (LEDS_BASE)) #define SLIDER_DATA_REG \ (*(volatile uint32_t*) (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 (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, always start a header file, say config.h, with: #ifndef CONFIG_H_ #define CONFIG_H_ and end it with: #endif /* CONFIG_H_ */ After the first inclusion of config.h, the symbol CONFIG_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 0xFF200020 31... 24 23... 16 15... 8 7... 0 Data register 0 5 6 1 HEX3 7-0 HEX2 7-0 HEX1 7-0 HEX0 7-0 4 3 2 7 Segments 0xFF200030 31 24 23 16 Unused 15... 8 7... 0 Data register HEX5 7-0 HEX4 7-0

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

Putting into practice (4) To go on: 4. Show on the 4x 7-Seg HEX3_HEX0 display the module of the 8-bit signed number (Sw 7 -Sw 0 ) and on LEDR 8 the sign of the number (LEDR 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 7 -Sw 0 ) represent an unsigned or signed number by the slider Sw 9 6. Combine all the features in a single program