Skip to content
Snippets Groups Projects
Commit 9892f8f0 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Vorbereitung 12.12.2019

parent 1878ac95
Branches
No related tags found
No related merge requests found
File added
% hp-20191212.pdf - Lecture Slides on Low-Level Programming
% Copyright (C) 2012, 2013, 2015, 2016, 2017, 2018, 2019 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
% README: make, Byte-Reihenfolge - Endianness, Binärdarstellung negativer Zahlen, Speicherausrichtung - Alignment
\documentclass[10pt,t]{beamer}
\usepackage{pgslides}
\usepackage{pdftricks}
\usepackage{tikz}
\begin{psinputs}
\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage[T1]{fontenc}
\usepackage{helvet}
\renewcommand*\familydefault{\sfdefault}
\usepackage{pstricks,pst-grad}
\end{psinputs}
\title{Hardwarenahe Programmierung}
\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski}
\date{12.\ Dezember 2019}
\begin{document}
\maketitleframe
\nosectionnonumber{\inserttitle}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp}}}
\item[\textbf{2}] \textbf{Einführung in C}
\item[\textbf{3}] \textbf{Bibliotheken}
\begin{itemize}
\item[3.1] Der Präprozessor
\item[3.2] Bibliotheken einbinden
\item[3.3] Bibliotheken verwenden
\color{red}
\item[3.4] Projekt organisieren: make
\end{itemize}
\vspace*{-\smallskipamount}
\item[\textbf{4}] \textbf{Hardwarenahe Programmierung}
\begin{itemize}
\vspace*{-0.1cm}
\item[\dots]
% \item[4.3] Interrupts
\color{orange}
\item[4.4] volatile-Variable
\color{red}
\item[4.6] Byte-Reihenfolge -- Endianness
\item[4.7] Binärdarstellung negativer Zahlen
\item[4.8] Speicherausrichtung -- Alignment
\end{itemize}
\item[\textbf{5}] \textbf{Algorithmen}
\begin{itemize}
\color{medgreen}
\item[5.1] Differentialgleichungen
\color{black}
\vspace*{-0.1cm}
\item[\dots]
\end{itemize}
\vspace*{-\smallskipamount}
\item[\textbf{\dots}]
\end{itemize}
\end{frame}
\setcounter{section}{3}
\section{Algorithmen}
\subsection{Differentialgleichungen}
\begin{frame}[fragile]
\showsection
\showsubsection
\textbf{Beispiel 1: Gleichmäßig beschleunigte Bewegung} -- Schräger Wurf
\strut\hfill
\begin{minipage}{2.5cm}
\vspace*{0.6cm}
\begin{align*}
x'(t) &= v_x(t) \\[0.65cm]
y'(t) &= v_y(t) \\[0.75cm]
v_x'(t) &= 0 \\[0.65cm]
v_y'(t) &= -g
\end{align*}
\vspace*{0.0cm}
\end{minipage}%
% \only<1>{\hspace*{9.49cm}}\strut
\only<1->{\hfill$\Rightarrow$\hfill}%
\begin{onlyenv}<1>
\begin{minipage}{8.3cm}
\begin{align*}
x(t) &= \int v_x(t)\,dt
\visible<1>{= \int v_{0x}\,dt}
\visible<1>{= x_0 + v_{0x}\cdot t}\\[\medskipamount]
y(t) &= \int v_y(t)\,dt
\visible<1>{= \int v_{0y} - g\cdot t\,dt}
\visible<1>{= y_0 + v_{0y}\cdot t
- {\textstyle\frac12}gt^2}\\[\bigskipamount]
v_x(t) &= \int 0\,dt
\visible<1>{= v_{0x}} \\[\medskipamount]
v_y(t) &= \int -g\,dt
\visible<1>{= v_{0y} - g\cdot t}
\end{align*}
\end{minipage}%
\end{onlyenv}%
\begin{onlyenv}<2>
\begin{minipage}{3.5cm}
\vspace*{0.5cm}
\begin{lstlisting}[gobble=8,xleftmargin=0.5em]
¡x += vx * dt;¿
\end{lstlisting}
\vspace{0.75cm}
\begin{lstlisting}[gobble=8,xleftmargin=0.5em]
¡y += vy * dt;¿
\end{lstlisting}
\vspace{0.90cm}
\begin{lstlisting}[gobble=8,xleftmargin=0.5em]
¡vx += 0 * dt;¿
\end{lstlisting}
\vspace{0.75cm}
\begin{lstlisting}[gobble=8,xleftmargin=0.5em]
¡vy += -g * dt;¿
\end{lstlisting}
\end{minipage}%
\begin{minipage}{5.13cm}
Siehe: \file{gtk-16.c}
\end{minipage}
\end{onlyenv}%
\hfill\strut
\end{frame}
\begin{frame}[fragile]
\showsection
\showsubsection
\textbf{Beispiel 1: Gleichmäßig beschleunigte Bewegung}
\medskip
\textbf{Beispiel 2: Mathematisches Pendel}
\vspace*{-2\bigskipamount}
\begin{picture}(0,0)
\put(8,-6.5){\includegraphics{pendulum.pdf}}
\end{picture}
\begin{eqnarray*}
\varphi'(t) &=& \omega(t) \\[\smallskipamount]
\omega'(t) &=& -\frac{g}{l}\cdot\sin\varphi(t)\hspace*{7.1cm}
\end{eqnarray*}
\vspace*{-1.5\medskipamount}
\begin{itemize}
\item
Von Hand (analytisch):\\
Lösung raten (Ansatz), Parameter berechnen
\item
Mit Computer (numerisch):\\
Eulersches Polygonzugverfahren
\end{itemize}
\smallskip
\begin{lstlisting}[gobble=0]
phi += dt * omega;
omega += - dt * g / l * sin (phi);
\end{lstlisting}
% \pause
\bigskip
\textbf{Beispiel 3:} Praktikumsaufgabe
\end{frame}
\setcounter{section}{2}
\section{Bibliotheken}
\setcounter{subsection}{3}
\subsection{Projekt organisieren: make}
\begin{frame}[fragile]
\showsubsection
\begin{itemize}
\item
\only<4->{explizite und implizite} Regeln
\begin{onlyenv}<2>
\smallskip
\begin{lstlisting}[language=make,gobble=10]
hello-6: hello-6.o pruzzel.o
gcc hello-6.o pruzzel.o -o hello-6
pruzzel.o: pruzzel.c pruzzel.h
gcc -Wall -O pruzzel.c -c
hello-6.o: hello-6.c pruzzel.h
gcc -Wall -O hello-6.c -c
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<4>
\smallskip
\begin{lstlisting}[language=make,gobble=10]
TARGET = hello-6
OBJECTS = hello-6.o pruzzel.o
HEADERS = pruzzel.h
CFLAGS = -Wall -O
$(TARGET): $(OBJECTS)
gcc $(OBJECTS) -o $(TARGET)
%.o: %.c $(HEADERS)
gcc $(CFLAGS) $< -c
clean:
rm -f $(OBJECTS) $(TARGET)
\end{lstlisting}
\end{onlyenv}
\item
Makros
\begin{onlyenv}<3>
\smallskip
\begin{lstlisting}[language=make,gobble=10]
TARGET = hello-6
OBJECTS = hello-6.o pruzzel.o
HEADERS = pruzzel.h
CFLAGS = -Wall -O
$(TARGET): $(OBJECTS)
gcc $(OBJECTS) -o $(TARGET)
pruzzel.o: pruzzel.c $(HEADERS)
gcc $(CFLAGS) pruzzel.c -c
hello-6.o: hello-6.c $(HEADERS)
gcc $(CFLAGS) hello-6.c -c
clean:
rm -f $(OBJECTS) $(TARGET)
\end{lstlisting}
\vspace*{-1cm}
\end{onlyenv}
\begin{onlyenv}<5->
\smallskip
\arrowitem
3 Sprachen: C, Präprozessor, make
\end{onlyenv}
\end{itemize}
\end{frame}
\setcounter{section}{3}
\section{Hardwarenahe Programmierung}
\setcounter{subsection}{3}
\subsection{volatile-Variable}
\begin{frame}[fragile]
\showsubsection
Externes Gerät ruft (per Stromsignal) Unterprogramm auf
Zeiger hinterlegen: "`Interrupt-Vektor"'
Beispiel: Taster
\vspace*{-2.5pt}
\begin{minipage}[t]{5cm}
\begin{onlyenv}<1>
\begin{lstlisting}[gobble=8]
¡#include <avr/interrupt.h>
...
uint8_t key_pressed = 0;
ISR (INT0_vect)
{
key_pressed = 1;
}¿
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{lstlisting}[gobble=8]
¡#include <avr/interrupt.h>
...
volatile uint8_t key_pressed = 0;
ISR (INT0_vect)
{
key_pressed = 1;
}¿
\end{lstlisting}
\end{onlyenv}
\end{minipage}\hfill
\begin{minipage}[t]{6cm}
\begin{lstlisting}[gobble=6]
¡int main (void)
{
...
while (1)
{
while (!key_pressed)
; /* just wait */
PORTD ^= 0x40;
key_pressed = 0;
}
return 0;
}¿
\end{lstlisting}
\end{minipage}
\pause
\begin{picture}(0,0)
\color{red}
\put(10.3,4.0){\makebox(0,0)[b]{\begin{minipage}{6cm}
\begin{center}
\textbf{volatile}:\\
Speicherzugriff\\
nicht wegoptimieren
\end{center}
\end{minipage}}}
\put(10.3,3.95){\makebox(0,0)[tr]{\tikz{\draw[-latex](0,0)--(-0.5,-0.9);}}}
\end{picture}
\end{frame}
\begin{frame}[fragile]
\showsubsection
Was ist eigentlich \lstinline{PORTD}?
\bigskip
\pause
\lstinline[style=cmd]{avr-gcc -Wall -Os -mmcu=atmega328p blink-3.c -E}
\bigskip
\pause
\lstinline{PORTD = 0x01;}\\
\textarrow\quad
\lstinline[style=terminal]{(*(volatile uint8_t *)((0x0B) + 0x20)) = 0x01;}\\
\pause
\begin{picture}(0,2)(0,-1.7)
\color{red}
\put(5.75,0.3){$\underbrace{\rule{2.95cm}{0pt}}_{\mbox{Zahl: \lstinline|0x2B|}}$}
\pause
\put(1.55,0.3){$\underbrace{\rule{4.0cm}{0pt}}_{\mbox{\shortstack[t]{Umwandlung in Zeiger\\
auf \lstinline|volatile uint8_t|}}}$}
\pause
\put(1.32,-1){\makebox(0,0)[b]{\tikz{\draw[-latex](0,0)--(0,1.3)}}}
\put(1.12,-1.1){\makebox(0,0)[tl]{Dereferenzierung des Zeigers}}
\end{picture}
\pause
\textarrow\quad
\lstinline|volatile uint8_t|-Variable an Speicheradresse \lstinline|0x2B|
\pause
\bigskip
\bigskip
\textarrow\quad
\lstinline|PORTA = PORTB = PORTC = PORTD = 0| ist eine schlechte Idee.
\end{frame}
\subsection{Byte-Reihenfolge -- Endianness}
\subsubsection{Konzept}
\begin{frame}[fragile]
\showsubsection
\showsubsubsection
Eine Zahl geht über mehrere Speicherzellen.\\
Beispiel: 16-Bit-Zahl in 2 8-Bit-Speicherzellen
\smallskip
Welche Bits liegen wo?
\pause
\bigskip
$1027 = 1024 + 2 + 1 = 0000\,0100\,0000\,0011_2 = 0403_{16}$
\pause
\bigskip
Speicherzellen:
\medskip
\begin{tabular}{|c|c|l}\cline{1-2}
\raisebox{-0.25ex}{04} & \raisebox{-0.25ex}{03} & \strut Big-Endian "`großes Ende zuerst"' \\\cline{1-2}
\multicolumn{2}{c}{} & \pause für Menschen leichter lesbar \pause \\
\multicolumn{3}{c}{} \\[-5pt]\cline{1-2}
\raisebox{-0.25ex}{03} & \raisebox{-0.25ex}{04} & \strut Little-Endian "`kleines Ende zuerst"' \\\cline{1-2}
\multicolumn{2}{c}{} & \pause bei Additionen effizienter
\end{tabular}
\pause
\medskip
\textarrow\ Geschmackssache
\pause\\
\quad\textbf{\dots\ außer bei Datenaustausch!}
% \pause
% \bigskip
%
% Aber: nicht verwechseln! \qquad $0304_{16} = 772$
\end{frame}
\begin{frame}
\showsubsection
\showsubsubsection
Eine Zahl geht über mehrere Speicherzellen.\\
Beispiel: 16-Bit-Zahl in 2 8-Bit-Speicherzellen
\smallskip
Welche Bits liegen wo?
\medskip
\textarrow\ Geschmackssache\\
\textbf{\dots\ außer bei Datenaustausch!}
\begin{itemize}
\item
Dateiformate
\item
Datenübertragung
\end{itemize}
\end{frame}
\subsubsection{Dateiformate}
\begin{frame}
\showsubsection
\showsubsubsection
Audio-Formate: Reihenfolge der Bytes in 16- und 32-Bit-Zahlen
\begin{itemize}
\item
RIFF-WAVE-Dateien (\file{.wav}): Little-Endian
\item
Au-Dateien (\file{.au}): Big-Endian
\pause
\item
ältere AIFF-Dateien (\file{.aiff}): Big-Endian
\item
neuere AIFF-Dateien (\file{.aiff}): Little-Endian
\end{itemize}
\pause
\bigskip
Grafik-Formate: Reihenfolge der Bits in den Bytes
\begin{itemize}
\item
PBM-Dateien: Big-Endian\only<4->{, MSB first}
\item
XBM-Dateien: Little-Endian\only<4->{, LSB first}
\end{itemize}
\only<4->{MSB/LSB = most/least significant bit}
\end{frame}
\subsubsection{Datenübertragung}
\begin{frame}
\showsubsection
\showsubsubsection
\begin{itemize}
\item
RS-232 (serielle Schnittstelle): LSB first
\item
I$^2$C: MSB first
\item
USB: beides
\pause
\medskip
\item
Ethernet: LSB first
\item
TCP/IP (Internet): Big-Endian
\end{itemize}
\end{frame}
\subsection{Binärdarstellung negativer Zahlen}
\begin{frame}[fragile]
\showsubsection
Speicher ist begrenzt!\\
\textarrow\ feste Anzahl von Bits
\medskip
8-Bit-Zahlen ohne Vorzeichen: \lstinline{uint8_t}\\
\textarrow\ Zahlenwerte von \lstinline{0x00} bis \lstinline{0xff} = 0 bis 255\\
\pause
\textarrow\ 255 + 1 = 0
\pause
\medskip
8-Bit-Zahlen mit Vorzeichen: \lstinline{int8_t}\\
\lstinline{0xff} = 255 ist die "`natürliche"' Schreibweise für $-1$.\\
\pause
\textarrow\ Zweierkomplement
\pause
\medskip
Oberstes Bit = 1: negativ\\
Oberstes Bit = 0: positiv\\
\textarrow\ 127 + 1 = $-128$
\end{frame}
\begin{frame}[fragile]
\showsubsection
Speicher ist begrenzt!\\
\textarrow\ feste Anzahl von Bits
\medskip
16-Bit-Zahlen ohne Vorzeichen:
\lstinline{uint16_t}\hfill\lstinline{uint8_t}\\
\textarrow\ Zahlenwerte von \lstinline{0x0000} bis \lstinline{0xffff}
= 0 bis 65535\hfill 0 bis 255\\
\textarrow\ 65535 + 1 = 0\hfill 255 + 1 = 0
\medskip
16-Bit-Zahlen mit Vorzeichen:
\lstinline{int16_t}\hfill\lstinline{int8_t}\\
\lstinline{0xffff} = 66535 ist die "`natürliche"' Schreibweise für $-1$.\hfill
\lstinline{0xff} = 255 = $-1$\\
\textarrow\ Zweierkomplement
\medskip
Oberstes Bit = 1: negativ\\
Oberstes Bit = 0: positiv\\
\textarrow\ 32767 + 1 = $-32768$
\bigskip
Literatur: \url{http://xkcd.com/571/}
\end{frame}
\begin{frame}[fragile]
\showsubsection
Frage: \emph{Für welche Zahl steht der Speicherinhalt\,
\raisebox{2pt}{%
\tabcolsep0.25em
\begin{tabular}{|c|c|}\hline
\rule{0pt}{11pt}a3 & 90 \\\hline
\end{tabular}}
(hexadezimal)?}
\pause
\smallskip
Antwort: \emph{Das kommt darauf an.} ;--)
\pause
\medskip
Little-Endian:
\smallskip
\begin{tabular}{lrl}
als \lstinline,int8_t,: & $-93$ & (nur erstes Byte)\\
als \lstinline,uint8_t,: & $163$ & (nur erstes Byte)\\
als \lstinline,int16_t,: & $-28509$\\
als \lstinline,uint16_t,: & $37027$\\
\lstinline,int32_t, oder größer: & $37027$
& (zusätzliche Bytes mit Nullen aufgefüllt)
\end{tabular}
\pause
\medskip
Big-Endian:
\smallskip
\begin{tabular}{lrl}
als \lstinline,int8_t,: & $-93$ & (nur erstes Byte)\\
als \lstinline,uint8_t,: & $163$ & (nur erstes Byte)\\
als \lstinline,int16_t,: & $-23664$\\
als \lstinline,uint16_t,: & $41872$\\
als \lstinline,int32_t,: & $-1550843904$ & (zusätzliche Bytes\\
als \lstinline,uint32_t,: & $2744123392$ & mit Nullen aufgefüllt)\\
als \lstinline,int64_t,: & $-6660823848880963584$\\
als \lstinline,uint64_t,: & $11785920224828588032$\\
\end{tabular}
\vspace*{-1cm}
\end{frame}
\subsection{Speicherausrichtung -- Alignment}
\begin{frame}[fragile]
\showsubsection
\begin{lstlisting}
#include <stdint.h>
uint8_t a;
uint16_t b;
uint8_t c;
\end{lstlisting}
\pause
\bigskip
Speicheradresse durch 2 teilbar -- "`16-Bit-Alignment"'
\begin{itemize}
\item
2-Byte-Operation: effizienter
\pause
\item
\dots\ oder sogar nur dann erlaubt
\pause
\arrowitem
Compiler optimiert Speicherausrichtung
\end{itemize}
\medskip
\pause
\begin{minipage}{3cm}
\begin{lstlisting}[gobble=6]
¡uint8_t a;
uint8_t dummy;
uint16_t b;
uint8_t c;¿
\end{lstlisting}
\end{minipage}
\pause
\begin{minipage}{3cm}
\begin{lstlisting}[gobble=6]
¡uint8_t a;
uint8_t c;
uint16_t b;¿
\end{lstlisting}
\end{minipage}
\pause
\vspace{-1.75cm}
\strut\hfill
\begin{minipage}{6.5cm}
Fazit:
\begin{itemize}
\item
\textbf{Adressen von Variablen\\
sind systemabhängig}
\item
Bei Definition von Datenformaten\\
Alignment beachten \textarrow\ effizienter
\end{itemize}
\end{minipage}
\end{frame}
\end{document}
../common/pendulum.pdf
\ No newline at end of file
......@@ -26,6 +26,7 @@ Vortragsfolien und Beispiele:
* [21.11.2019: Interrupts, volatile-Variable, Bibliotheken: Einführung](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20191121/hp-20191121.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20191121/)
* [28.11.2019: Präprozessor, Bibliotheken](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20191128/hp-20191128.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20191128/)
* [05.12.2019: Bibliotheken, Differentialgleichungen](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20191205/hp-20191205.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20191205/)
* [12.12.2019: make, Byte-Reihenfolge - Endianness, Binärdarstellung negativer Zahlen, Speicherausrichtung - Alignment](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20191212/hp-20191212.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20191212/)
* [alle in 1 Datei](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/hp-slides-2019ws.pdf)
Übungsaufgaben:
......@@ -62,6 +63,7 @@ Praktikumsunterlagen:
---------------------
* [Versuch 1, 17. und 31.10.2019: RSA-Verschlüsselung](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20191010/hp-2019ws-p1.pdf)
* [Versuch 2, 14. und 21.11.2019: Druckknopfampel](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20191107/hp-2019ws-p2.pdf)
* [Versuch 3, 12. und 19.12.2019: Weltraum-Simulation](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20191212/hp-2019ws-p3.pdf)
Alte Klausuren:
---------------
......
No preview for this file type
......@@ -28,4 +28,6 @@
\includepdf[pages=-]{20191128/hp-20191128.pdf}
\pdfbookmark[1]{05.12.2019: Bibliotheken, Differentialgleichungen}{20191205}
\includepdf[pages=-]{20191205/hp-20191205.pdf}
\pdfbookmark[1]{12.12.2019: make, Byte-Reihenfolge - Endianness, Binärdarstellung negativer Zahlen, Speicherausrichtung - Alignment}{20191212}
\includepdf[pages=-]{20191212/hp-20191212.pdf}
\end{document}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment