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

Vortragsfolien 19.12.2019

parent 56b3b112
No related branches found
No related tags found
No related merge requests found
../common/Tower_of_Hanoi.jpeg
\ No newline at end of file
File added
% hp-20191219.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: 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{19.\ 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{medgreen}
\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
\item[4.4] volatile-Variable
\color{medgreen}
\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}
\item[5.1] Differentialgleichungen
\vspace*{-0.1cm}
\item[\dots]
\end{itemize}
\vspace*{-\smallskipamount}
\item[\textbf{\dots}]
\end{itemize}
\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
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}{} & für Menschen leichter lesbar \\
\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}{} & 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<1->{, MSB first}
\item
XBM-Dateien: Little-Endian\only<1->{, LSB first}
\end{itemize}
\only<1->{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}
\section{Algorithmen}
\setcounter{subsection}{1}
\subsection{Rekursion}
\begin{frame}[fragile]
\showsubsection
Vollständige Induktion:
\vspace*{-0.725cm}
\begin{displaymath}
\hspace*{4cm}
\left.
\begin{array}{r}
\mbox{Aussage gilt für $n = 1$}\\[2pt]
\mbox{Schluß von $n - 1$ auf $n$}
\end{array}
\right\}
\mbox{Aussage gilt für alle $n\in\mathbb{N}$}
\end{displaymath}
\vspace*{-0.5cm}
\pause
Türme von Hanoi
\begin{onlyenv}<2>
\begin{center}
\includegraphics[width=12.2cm]{Tower_of_Hanoi.jpeg}
\end{center}
\end{onlyenv}
\begin{onlyenv}<3->
\begin{itemize}
\item
64 Scheiben, 3 Plätze,
\only<3-4>{\hfill\makebox(0,0)[rt]{\includegraphics[width=6cm]{Tower_of_Hanoi.jpeg}}}\\
immer 1 Scheibe verschieben
\item
Ziel: Turm verschieben
\item
Es dürfen nur kleinere Scheiben\\
auf größeren liegen.
\bigskip
\pause
\pause
\item
$n = 1$ Scheibe: fertig
\item
Wenn $n - 1$ Scheiben verschiebbar:\\
schiebe $n - 1$ Scheiben auf Hilfsplatz,\\
verschiebe die darunterliegende,\\
hole $n - 1$ Scheiben von Hilfsplatz
\end{itemize}
\begin{onlyenv}<5>
\vspace{-4.3cm}
\begin{lstlisting}[gobble=8,xleftmargin=6.4cm]
void move (int from, int to, int disks)
{
if (disks == 1)
move_one_disk (from, to);
else
{
int help = 0 + 1 + 2 - from - to;
move (from, help, disks - 1);
move (from, to, 1);
move (help, to, disks - 1);
}
}
\end{lstlisting}
\end{onlyenv}
\begin{onlyenv}<6->
\vspace{-5.0cm}
\hspace*{7.4cm}\begin{minipage}[t]{5cm}
32 Scheiben:
\begin{lstlisting}[gobble=10,style=terminal]
$ ¡time ./hanoi-9a¿
...
real 0m32,712s
user 0m32,708s
sys 0m0,000s
\end{lstlisting}
\pause[7]
\begin{itemize}
\arrowitem
etwas über 1 Minute\\
für 64 Scheiben
\end{itemize}
\pause
\vspace*{-0.5cm}
\begin{picture}(0,0)
\color{red}
\put(0,0){\makebox(0,0)[bl]{\tikz[line width=1pt]{\draw(0,0)--(4,0.8);}}}
\put(0,0.8){\makebox(0,0)[tl]{\tikz[line width=1pt]{\draw(0,0)--(4,-0.8);}}}
\end{picture}
Für jede zusätzliche Scheibe\\verdoppelt sich die Rechenzeit!
% 32.712 * 2^32 / 3600 / 24 / 365.25 = 4452.08032888280477602859
\begin{itemize}
\arrowitem
$\frac{32,712\,\text{s}\,\cdot\,2^{32}}{3600\,\cdot\,24\,\cdot\,365,25} \approx 4452$
Jahre\\[\smallskipamount]
für 64 Scheiben
\end{itemize}
\end{minipage}
\end{onlyenv}
\end{onlyenv}
\end{frame}
\subsection{Aufwandsabschätzungen \protect\color{gray}-- Komplexitätsanalyse}
\begin{frame}[fragile]
\newcommand{\w}{\hspace*{0.75pt}}
\showsubsection
\begin{picture}(0,0)
\put(7.8,-0.3){%
\begin{minipage}[t]{5.3cm}
\vspace*{-1.0cm}\includegraphics{landau-symbols.pdf}
% \vspace*{-1.0cm}\alt<2->{\includegraphics{landau-symbols-2.pdf}}%
% {\includegraphics{landau-symbols.pdf}}
\small
\begin{description}\itemsep0pt\leftskip-0.5cm
\item[$n$:] Eingabedaten
\item[$g(n)$:] Rechenzeit
\end{description}
\end{minipage}}
\end{picture}
\vspace*{-1.5\bigskipamount}
\begin{itemize}
\item
Türme von Hanoi: $\mathcal{O}(2^n)$
\begin{onlyenv}<1>
\par\medskip
Für jede zusätzliche Scheibe\\verdoppelt sich die Rechenzeit!
% 32.712 * 2^32 / 3600 / 24 / 365.25 = 4452.08032888280477602859
\begin{itemize}
\arrowitem
$\frac{32,712\,\text{s}\,\cdot\,2^{32}}{3600\,\cdot\,24\,\cdot\,365,25} \approx 4452$
Jahre\\[\smallskipamount]
für 64 Scheiben
\end{itemize}
\end{onlyenv}
\end{itemize}
\pause
Beispiel: Sortieralgorithmen
\begin{itemize}
\item
Minimum suchen: \alt<4->{$\mathcal{O}(n)$}{$\mathcal{O}(\textbf{\color{red}?})$}
\pause
\item
\dots\ mit Schummeln: $\mathcal{O}(1)$
\pause
\pause
\begin{picture}(0,0)
\put(-4.742,-3.7){\begin{minipage}[t]{12cm}
Faustregel:\\Schachtelung der Schleifen zählen\\
$x$ Schleifen \textarrow\ $\mathcal{O}(n\w^x)$
% \pause
% \begin{tabbing}
% Verschlüsselung brechen (Primfaktorzerlegung):~\=\kill
% \textbf{\color{red}RSA}: Schlüsselerzeugung (Berechnung von $d\/$):
% \> \color{red}$\mathcal{O}\bigl((\log n)^2\bigr)$,\\[\smallskipamount]
% Ver- und Entschlüsselung (Exponentiation):
% \> \color{red}$\mathcal{O}\kern0.5pt(n\log n)$,\\[0.5\smallskipamount]
% Verschlüsselung brechen (Primfaktorzerlegung):
% \> \color{red}$\mathcal{O}\bigl(2^{\sqrt{\log n\,\cdot\,\log\log n}}\bigr)$
% \end{tabbing}
\end{minipage}}
\end{picture}
\pause
\item
Minimum an den Anfang tauschen,\\
nächstes Minimum suchen:\\
\textarrow\ Selectionsort\pause: $\mathcal{O}(n\w^2)$
% \pause
% \item
% Während Minimumsuche prüfen\\und abbrechen, falls schon sortiert\\
% \textarrow\ Bubblesort\pause: $\mathcal{O}(n)$ bis $\mathcal{O}(n\w^2)$
% \pause
% \item
% Rekursiv sortieren\\
% \textarrow\ Quicksort\pause: $\mathcal{O}(n\log n)$ bis $\mathcal{O}(n\w^2)$\hfill
\end{itemize}
\end{frame}
\end{document}
../common/landau-symbols.pdf
\ No newline at end of file
File added
File added
../common/pgscript.sty
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment