Some Basic Concepts EL6483. Spring EL6483 Some Basic Concepts Spring / 22

Similar documents
EL6483: Brief Overview of C Programming Language

CS201 - Introduction to Programming Glossary By

Short Notes of CS201

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

Jump Tables A jump table is essentially a list of places in the code to jump to. This can be thought of in C as an array of function pointers. In Asse

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

Arduino Uno. Power & Interface. Arduino Part 1. Introductory Medical Device Prototyping. Digital I/O Pins. Reset Button. USB Interface.

CS401 - Computer Architecture and Assembly Language Programming Glossary By

EE4144: Basic Concepts of Real-Time Operating Systems


L2 - C language for Embedded MCUs

CSC 1600 Memory Layout for Unix Processes"

538 Lecture Notes Week 1

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

CS 61C: Great Ideas in Computer Architecture. (Brief) Review Lecture

Practical Malware Analysis

ELEG3924 Microprocessor

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

CMPE-013/L. Introduction to C Programming

ELEG3923 Microprocessor Ch.2 Assembly Language Programming

8051 Microcontrollers

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

ARM Cortex-M4 Programming Model

Run-time Environments

Run-time Environments

In Java we have the keyword null, which is the value of an uninitialized reference type

Special Topics for Embedded Programming

Come and join us at WebLyceum

CS Programming In C

CSCI 171 Chapter Outlines

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Advanced Operating Systems Embedded from Scratch: System boot and hardware access. Federico Terraneo

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Microcontrollers. Microcontroller

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

ECE 471 Embedded Systems Lecture 5

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

CS61C : Machine Structures

Chapter 1. Microprocessor architecture ECE Dr. Mohamed Mahmoud.

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

CPE 323 MSP430 INSTRUCTION SET ARCHITECTURE (ISA)

Chapter 3 Machine Instructions & Programs. Jin-Fu Li Department of Electrical Engineering National Central University Jungli, Taiwan

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

17. Instruction Sets: Characteristics and Functions

Computer Systems Lecture 9

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Older geometric based addressing is called CHS for cylinder-head-sector. This triple value uniquely identifies every sector.

Name, Scope, and Binding. Outline [1]

QUIZ. What are 3 differences between C and C++ const variables?

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

CS201 Some Important Definitions

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Contents. Preface. Introduction. Introduction to C Programming

ECE 498 Linux Assembly Language Lecture 1

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Data Transfer Instructions

Memory Architectures. Hongwei Zhang. Chapter 9

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

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

ARM Architecture and Assembly Programming Intro

CS240: Programming in C

Chapter 2A Instructions: Language of the Computer

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

CPSC213/2014W1 Midterm EXTRA Practice

CIT Week13 Lecture

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

Work relative to other classes

ECE 471 Embedded Systems Lecture 6

o Code, executable, and process o Main memory vs. virtual memory

Review of the C Programming Language for Principles of Operating Systems

Chapter 1 INTRODUCTION

For Teacher's Use Only Q No Total Q No Q No

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Embedded Systems - FS 2018

The Design of C: A Rational Reconstruction

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Reliable C++ development - session 1: From C to C++ (and some C++ features)

The Design of C: A Rational Reconstruction"

Assembly Language Programming

Embedded Systems - FS 2018

Fundamentals of Programming

Technical Questions. Q 1) What are the key features in C programming language?

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Review of the C Programming Language

Introduction to Java Programming

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

CMU MSP 36601: Computer Hardware and Data Representation

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

Advanced Programming & C++ Language

CS201 Latest Solved MCQs

Lecture 03 Bits, Bytes and Data Types

C H A P T E R 1. Introduction to Computers and Programming

A S H O R T I S H O V E R V I E W O F T H E A N S I C P R O G R A M M I N G L A N G U A G E

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

LABORATORIO DI ARCHITETTURE E PROGRAMMAZIONE DEI SISTEMI ELETTRONICI INDUSTRIALI

Transcription:

Some Basic Concepts EL6483 Spring 2016 EL6483 Some Basic Concepts Spring 2016 1 / 22

Embedded systems Embedded systems are rather ubiquitous these days (and increasing rapidly). By some estimates, there are currently over a hundred embedded devices per person. Many types of embedded systems... printers, routers, watches, phones, robots and autonomous vehicles, etc. Many devices have multiple embedded systems (e.g., a car with several embedded devices for various functionalities, a laptop computer with several embedded devices for functionalities such as hard drive control, fan control, etc.). One definition of an embedded device is a device that includes a programmable processor, but is not itself a general-purpose computer. EL6483 Some Basic Concepts Spring 2016 2 / 22

Some common characteristics of embedded systems Event-driven behavior definition: The system behavior is defined in terms of desired responses to events (e.g., inputs from external devices). For example, an automatic garage door opener is supposed to command a motor (that opens the garage door) as a response to the event of a remote button press. An event can also be a time-related event, e.g., an embedded device that is supposed to perform some task every some number of milliseconds. Specific and well-defined behavior: the system task is well-defined. However, there is usually inherent randomness in the external events (e.g., the time at which the user might press a button is essentially random). The system should be designed to gracefully handle this randomness in the external inputs and have a robust well-defined behavior in response to the inputs. EL6483 Some Basic Concepts Spring 2016 3 / 22

Some common characteristics of embedded systems Real-time requirements: In its response to events, an embedded system is often required to satisfy some timing constraints. Note that real-time does not necessarily mean very fast. Instead, it means fast enough in the sense that the latency between an event and its response can be quantitatively specified. Latency is the delay between an event and the corresponding response. Jitter is the variation/deviation of the timing. Hard real-time (absolute guarantee), soft real-time (best effort). If a hard real-time requirement is not met, that is interpreted as a system failure; if a soft real-time requirement is not met, it simply means that system performance is somewhat reduced. EL6483 Some Basic Concepts Spring 2016 4 / 22

Some common characteristics of embedded systems Limited resources: Memory, processing power, etc. Embedded systems are often resource-constrained due to considerations of cost, size, power, etc. Hence, efficiency is often a key requirement. Sensors and actuators: Embedded systems often read data from the external environment using sensors (e.g., voltage sensors, microphones, cameras, temperature sensors, accelerometers, etc.) and send outputs to somehow influence/change the environment using actuators (e.g., voltage outputs, audio speakers, etc.). Combination of hardware and software components: The hardware and software parts of the system are often designed together (co-design) to achieve the specified system behavior. Parallelism: An embedded system might need to respond to multiple sources of events (e.g., a device connected to three user buttons with event-driven behavior specifications for the system response to button presses). Reliability: Reliability (and the related requirements of safety and security) is often part of the basic system requirements definition (e.g., an embedded device in an unmanned vehicle or in a medical robotic system, etc.). EL6483 Some Basic Concepts Spring 2016 5 / 22

Binary and hexadecimal Binary : 0 s and 1 s (bits) Hexadecimal (base 16): 0,..., 9, A, B, C, D, E, F ; each hexadecimal (hex) digit is often called a nibble (4 bits). A byte is usually 8 bits (2 hex digits). On a 32-bit processor, a word is usually 32 bits (4 bytes). Hexadecimal is usually written with prefix 0x Examples: 0xE = 14 in decimal = 1110 in binary 0x64 = 100 in decimal = 1100100 in binary Octal (base 8): A number written starting from 0 refers to an octal (base 8), e.g., 010 = 0x8 = 8 in decimal EL6483 Some Basic Concepts Spring 2016 6 / 22

Program compilation model An embedded system program might include, in general, multiple C/C++ files, multiple Assembly files, and precompiled library files. File type Commonly used filename extensions C, C++.c,.cpp Assembly.s or.asm Binary precompiled libraries.lib These multiple files need to be compiled and linked together to create an executable. In a bare-metal system, the resulting executable is the complete binary program that will run on the microprocessor. In a system with an operating system (e.g., embedded Linux), the program can utilize OS (operating system) functionalities (e.g., system calls) or other dynamically linked libraries at run-time (via a loader provided by the OS). EL6483 Some Basic Concepts Spring 2016 7 / 22

C/C++ program compilation model Conceptual sequence of steps in converting a set of.c/.cpp files into an executable each.c/.cpp file is processed separately as a translation unit (or compilation unit) preprocessor step : text substitutions (e.g., #define, #include), conditional compilation (e.g., #ifdef) preprocessor takes a.c/.cpp file and generates a modified C/C++ code based on the preprocessor directives within the.c/.cpp file (and included.h files) compilation step : compiler converts C/C++ code to assembly language code assembly step : assembler converts assembly language code to the machine language (binary) code as an object file (.o or.obj) the separate object files are then linked together to generate the executable EL6483 Some Basic Concepts Spring 2016 8 / 22

C/C++ program compilation model While preprocessing, compiling, assembling are conceptually separate steps, some C/C++ compilers combine multiple of these steps into one (e.g., they might not explicitly create an assembly language file during the compilation procedure). As part of linking, the inter-file dependencies (e.g., functions that are defined in one.c/.cpp file and called from another.c/.cpp file) are resolved (symbol resolution) symbol clash/conflict if the same symbol is defined in multiple translation units if we need to define different functions with the same name in more than one.c/.cpp files, then we can declare these functions as static to make them visible only at file scope and prevent symbol conflicts If we also have some assembly language source files (.s/.asm), then they are converted to object code and linked with the object codes from the other translation units in the complete program. EL6483 Some Basic Concepts Spring 2016 9 / 22

C/C++ program compilation model Compile-time errors vs. link-time errors vs. run-time errors Each C/C++ file is compiled separately. Examples of compile-time errors include syntax errors, type definition errors, etc. Symbols (i.e., function/variable names) are resolved during linking. If a file refers to a symbol not defined in it, the linker will try to find the symbol in another file. Examples of link-time errors include: multiple definitions of the same symbol name. For example, two files having definitions of functions with the same name unresolved definitions, i.e., the definition of a name not being found. For example, if a file refers to a function f, but no file providing a definition of this function is available. Run-time errors include logic errors, etc. EL6483 Some Basic Concepts Spring 2016 10 / 22

C/C++ program compilation model To refer to a function defined in another file, we just need to specify the function prototype (also called function signature), i.e., the types of arguments and return value of the function. Examples: void f(void);, float g(int,int); To refer to a variable defined in another file, we can use the keyword extern. Examples: extern float a;, extern int b;. If a variable is referred to as extern, but is not defined in any other file, that will result in a link-time error. Note that C++ compilers modify symbol names to support function overloading, etc. For example, a function void f(int) might be assigned the name f_int in the object file and a function void f(float) might be assigned the name f_float in the object file. This is called name mangling. Hence, to call between C and C++ functions, we need to tell the C++ compiler to not mangle names. This is done by using the keyword extern C. Example: extern "C" void g(int); A library (.lib) file is simply a collection of object files in a binary archive. If a C/C++/Assembly file refers to a function defined in an object file within the library archive, the linker will pull in the appropriate function definition during the linking stage. EL6483 Some Basic Concepts Spring 2016 11 / 22

Assembly language An assembler converts an assembly language code into binary object code. Assembly language is essentially machine language written using mnemonics. Assembly language has almost one-to-one correspondence to the processor s machine code instructions. Assembly language instruction sets for different processors are generally very different. C/C++ code is much more portable between different processors. Assembly language is specific to a particular processor. Also, much easier to write C/C++ code than assembly code. Cross compiler Since the machine instruction set is different for different processors, a cross compiler is needed to compile code on one processor architecture for a different processor architecture (e.g., using a laptop to compile code that eventually has to run on an ARM processor). EL6483 Some Basic Concepts Spring 2016 12 / 22

Assembly language Typically, most of the code (even in an embedded system) is written in a high-level language such as C/C++, with assembly utilized only for specific tasks such as in start-up routines and for small specialized functions that particularly need to be very optimized (especially functions that need to be executed a large number of times) or functions that utilize low-level hardware-specific functionalities of the processor that are easier to access from assembly. However, note that C/C++ compilers are very good at optimization these days and can often out-perform hand-written assembly codes, especially for larger more complicated functions. A knowledge of assembly language is also very useful in debugging (including debugging of C/C++ code since the resulting assembly code can help during debugging, especially if only the compiled binary executable of a program is available and not the original source code). EL6483 Some Basic Concepts Spring 2016 13 / 22

Flash and RAM An embedded device typically has two types of memory: Flash: Non-volatile memory typically utilized for storing the program binary. Non-volatile means that the contents of this memory survive a power cycle (power-off and power-on). This memory is typically written to by using a hardware debugging interface (such as ST Link for ARM microcontrollers from ST Microelectronics). In addition (but less frequently), the non-volatile memory might be used by code to store parameters, etc. RAM (Random Access Memory): Volatile memory used during program execution (e.g., for run-time variables). Volatile means that the contents of this memory are lost when the device is powered off. The variables in a program (e.g., int a;) are typically stored in RAM. RAM is much faster to read from and write to than Flash. In addition, some embedded devices might have other types of memory storage such as SD card, USB-connected data storage, etc. The microcontroller on the STM32F4 Discovery board has 192 kb of RAM and 1 MB of Flash. EL6483 Some Basic Concepts Spring 2016 14 / 22

Basic microprocessor/microcontroller concepts Microprocessor is the computational core (Arithmetic Logic Unit, Control Unit, Registers). Microcontroller is microprocessor + peripherals (input/output ports, memory, etc.). The microcontroller on the STM32F4 Discovery board is the STM32F407VGT6 which includes the 32-bit ARM Cortex-M4F core. The registers in an ARM microprocessor include general-purpose registers (used to hold values during program execution) and special registers including: Program Counter : keeps track of the location in the program that is currently being executed (i.e., memory address of the line of machine code currently being executed) Link Register : stores a bookmark (address) to the current location in the program when a different function is called so that when that function returns, the code execution returns to the correct spot Stack Pointer : points to the top of the stack EL6483 Some Basic Concepts Spring 2016 15 / 22

Stack and Heap The available memory in RAM can be thought of as a sequence of 1-byte memory locations. Each 1-byte memory location has an address. For a 32-bit processor, a memory address is typically 32 bits (4 bytes). In the available memory, two specific parts are usually defined: stack and heap. Local variables (also called automatic variables) in a C/C++ function (e.g., int a;) are stored in stack. Heap is used for dynamically allocated memory space (e.g., int *a = (int *)malloc(sizeof(int));). The specifications of how much memory space is allocated for stack and heap (as a memory address range for stack and a memory address range for heap) are specified to the linker as part of a linker script (usually a file with filename extension.ld). EL6483 Some Basic Concepts Spring 2016 16 / 22

Stack Variables are pushed onto the stack and popped off the stack. Last-in-first-out (LIFO) data storage. The last item pushed onto the stack is the first popped off the stack by a pop operation. Local variables defined in a function (automatic variables) are usually automatically allocated on the stack (e.g., int a;). When a function returns, its variables are automatically popped off the stack. Stack pointer (SP) holds the address of the current top of the stack. When items are pushed onto the stack (e.g., by defining a local variable), then the stack pointer value is updated (e.g., changed by 4 if a 32-bit int is pushed onto the stack). The direction in which the stack grows (i.e., whether stack pointer numerically increments or decrements when additional items are pushed onto the stack) is dependent on the processor architecture; on ARM architecture, the direction of stack growth can be chosen to be either incrementing or decrementing. Stack is also used on some processor architectures to store a bookmark when calling a function (so that the processor knows where to return to when the function returns); stack can also be used to pass arguments to functions. Stack has a finite size (defined when linking the binary executable); if we push too much onto the stack, we get a stack overflow. If we have a recursive function and too many nested function calls, then also a stack overflow can result. EL6483 Some Basic Concepts Spring 2016 17 / 22

Heap Heap is utilized for dynamic memory allocation (malloc and free in C, new and delete in C++). Examples of dynamic memory allocation in C (malloc and free): int *a = (int *)malloc(sizeof(int)); // a points to 1 int int *a = (int *)malloc(5*sizeof(int)); // a points to an array of 5 ints free(a); Examples of dynamic memory allocation in C++ (new and delete): int *a = new int; // a points to 1 int int *a = new int[5]; // a points to an array of 5 ints delete a; A variable that is dynamically allocated (e.g., using malloc) must be explicitly deallocated (e.g., using free) when no longer needed. Forgetting to deallocate can result in a memory leak. Do not mix C-style and C++-style dynamic memory allocation. If you used malloc to allocate memory, use free to free it. If you used new, use delete. EL6483 Some Basic Concepts Spring 2016 18 / 22

Heap Over time, as memory chunks of different sizes are allocated and deallocated, heap fragmentation can happen, wherein the free memory is in small gaps between allocated memory and the system cannot find contiguous memory chunks for allocation even though there is still some free memory. Variables on the heap are accessed via pointers. Allocating variables on the stack is typically much faster than on the heap. If the size of an array is not known at compile-time (i.e., variable length arrays), then typically dynamic memory allocation must be used (only a small number of rather new C compilers support variable length arrays on stack): int *a = (int *)malloc(n*sizeof(int)); EL6483 Some Basic Concepts Spring 2016 19 / 22

Endianness Memory in a computer is addressed in bytes, i.e., each byte of memory has its own address. When storing a variable of a data type that is larger than 1 byte, there are two possibilities: storing the least significant byte first (in the lowest memory address) or storing the most significant byte first. Endianness refers to the order in which the bytes of a variable that is larger than 1 byte is stored. Little-endian: least significant byte is stored first (i.e., at the lowest memory address) Big-endian: most significant byte is stored first (i.e., at the lowest memory address) For example, if a 32-bit integer has bytes B3, B2, B1, and B0 with B3 being the most significant byte and B0 being the least significant byte, a little-endian processor would store the bytes in the sequence B0 B1 B2 B3 in memory and a big-endian processor would store the bytes in the sequence B3 B2 B1 B0 in memory. EL6483 Some Basic Concepts Spring 2016 20 / 22

Bitwise operations Bitwise operation Symbol (in C) AND & OR XOR ^ NOT ~ Left shift << Right shift >> Bitwise operations are commonly used to set various flags in an embedded system. For example, consider a variable L that represents a control register for a voltage sensor. If it is defined that bit 0 (i.e., the right-most bit) specifies whether the voltage range of the voltage sensor with bit 0 being 0 corresponding to 0-10V and bit 0 being 1 corresponding to ± 10 V, then to select the ± 10 V option without changing any of the other bits in L, we could write (in C): L = L 0x1; EL6483 Some Basic Concepts Spring 2016 21 / 22

Bitwise operations Some examples of bitwise operations: 0x05 & 0x01 = 0x01 0x05 0x02 = 0x07 0x05 ^ 0x01 = 0x04 0x05 «2 = 0x14 0x05» 1 = 0x02 Example: If we have an integer variable named L and we want to set bit 1 (with bit 0 being the right-most bit) to 0 without changing the other bits, we can write (in C): or equivalently: L &= ( 0x02); L = L & ( 0x02); EL6483 Some Basic Concepts Spring 2016 22 / 22