diff --git a/20200109/aufgabe-1.c b/20200109/aufgabe-1.c new file mode 100644 index 0000000000000000000000000000000000000000..cb09b0e10aade5c202ed88fddcb2e52d700d9915 --- /dev/null +++ b/20200109/aufgabe-1.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <stdint.h> + +typedef struct +{ + uint32_t a; + uint64_t b; + uint8_t c; +} three_numbers; + +int main (void) +{ + three_numbers xyz = { 1819042120, 2410670883059281007, 0 }; + printf ("%s\n", &xyz); + return 0; +} diff --git a/20200109/aufgabe-2.c b/20200109/aufgabe-2.c new file mode 100644 index 0000000000000000000000000000000000000000..33dbe39325bbfe9bf2481194b0be3420f1e3344e --- /dev/null +++ b/20200109/aufgabe-2.c @@ -0,0 +1,26 @@ +#include <stdio.h> +#include <stdint.h> + +void output (uint16_t *a) +{ + for (int i = 0; a[i]; i++) + printf (" %d", a[i]); + printf ("\n"); +} + +int main (void) +{ + uint16_t prime_numbers[] = { 2, 3, 5, 7, 11, 13, 17, 0 }; + + uint16_t *p1 = prime_numbers; + output (p1); + p1++; + output (p1); + + char *p2 = prime_numbers; + output (p2); + p2++; + output (p2); + + return 0; +} diff --git a/20200109/aufgabe-3.c b/20200109/aufgabe-3.c new file mode 100644 index 0000000000000000000000000000000000000000..47595ef0658e94d76a42263e82200f94895cdeea --- /dev/null +++ b/20200109/aufgabe-3.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <string.h> + +typedef struct +{ + char first_name[10]; + char family_name[20]; + char day, month; + int year; +} person; + +int main (void) +{ + person sls; + sls.day = 26; + sls.month = 7; + sls.year = 1951; + strcpy (sls.first_name, "Sabine"); + strcpy (sls.family_name, "Leutheusser-Schnarrenberger"); + printf ("%s %s wurde am %d.%d.%d geboren.\n", + sls.first_name, sls.family_name, sls.day, sls.month, sls.year); + return 0; +} diff --git a/20200109/hp-uebung-20200109.pdf b/20200109/hp-uebung-20200109.pdf new file mode 100644 index 0000000000000000000000000000000000000000..7894b5a858d5ce3bc4e4823d01000eb61ae25186 Binary files /dev/null and b/20200109/hp-uebung-20200109.pdf differ diff --git a/20200109/hp-uebung-20200109.tex b/20200109/hp-uebung-20200109.tex new file mode 100644 index 0000000000000000000000000000000000000000..b5030dff4269e3c426fec5ac25dfccf32218b609 --- /dev/null +++ b/20200109/hp-uebung-20200109.tex @@ -0,0 +1,265 @@ +% hp-uebung-20200109.pdf - Exercises on Low-Level Programming +% Copyright (C) 2013, 2015, 2016, 2017, 2018, 2019, 2020 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: Speicherformate von Zahlen, Zeigerarithmetik, Personen-Datenbank + +\documentclass[a4paper]{article} + +\usepackage{pgscript} + +\begin{document} + + \thispagestyle{empty} + + \section*{Hardwarenahe Programmierung\\ + Übungsaufgaben -- 9.\ Januar 2020} + + Diese Übung enthält Punkteangaben wie in einer Klausur. + Um zu "`bestehen"', müssen Sie innerhalb von 90 Minuten + unter Verwendung ausschließlich zugelassener Hilfsmittel + 15 Punkte (von insgesamt \totalpoints) erreichen. + + \exercise{Speicherformate von Zahlen} + + Wir betrachten das folgende Programm (\gitfile{hp}{20200109}{aufgabe-1.c}): + \begin{lstlisting} + #include <stdio.h> + #include <stdint.h> + + typedef struct + { + uint32_t a; + uint64_t b; + uint8_t c; + } three_numbers; + + int main (void) + { + three_numbers xyz = { 1819042120, 2410670883059281007, 0 }; + printf ("%s\n", &xyz); + return 0; + } + \end{lstlisting} + + Das Programm wird für einen 32-Bit-Rechner compiliert und ausgeführt.\\ + (Die \lstinline[style=cmd]{gcc}-Option \lstinline[style=cmd]{-m32} sorgt dafür, + daß \lstinline[style=cmd]{gcc} Code für einen 32-Bit-Prozessor erzeugt.) + + \begin{lstlisting}[style=terminal] + $ ¡gcc -Wall -m32 aufgabe-2.c -o aufgabe-2¿ + aufgabe-2.c: In function "main": + aufgabe-2.c:14:13: warning: format "%s" expects argument of type "char *", but + argument 2 has type "three_numbers * {aka struct <anonymous> *}" [-Wformat=] + printf ("%s\n", &xyz); + ^ + $ ¡./aufgabe-2¿ + Hallo, Welt! + \end{lstlisting} + + \begin{enumerate}[\quad(a)] + \item + Erklären Sie die beim Compilieren auftretende Warnung. + \points{2} + \item + Erklären Sie die Ausgabe des Programms. + \points{4} + \item + Welche Endianness hat der verwendete Rechner? + Wie sähe die Ausgabe auf einem Rechner mit entgegengesetzter Endianness aus? + \points{2} + \item + Dasselbe Programm wird nun für einen 64-Bit-Rechner compiliert und ausgeführt.\\ + (Die \lstinline[style=cmd]{gcc}-Option \lstinline[style=cmd]{-m64} sorgt dafür, + daß \lstinline[style=cmd]{gcc} Code für einen 64-Bit-Prozessor erzeugt.) + \begin{lstlisting}[style=terminal,gobble=8] + $ ¡gcc -Wall -m64 aufgabe-2.c -o aufgabe-2¿ + aufgabe-2.c: In function "main": + aufgabe-2.c:14:13: warning: format "%s" expects argument of type "char *", + but argument 2 has type "three_numbers * {aka struct <anonymous> *}" + [-Wformat=] + printf ("%s\n", &xyz); + ^ + $ ¡./aufgabe-2¿ + Hall5V + \end{lstlisting} + (Es ist möglich, daß die konkrete Ausgabe auf Ihrem Rechner anders aussieht.)\par + Erklären Sie die geänderte Ausgabe des Programms. + \points{3} + \end{enumerate} + + \clearpage + + \exercise{Zeigerarithmetik} + + Wir betrachten das folgende Programm (\gitfile{hp}{20200109}{aufgabe-2.c}): + \begin{lstlisting} + #include <stdio.h> + #include <stdint.h> + + void output (uint16_t *a) + { + for (int i = 0; a[i]; i++) + printf (" %d", a[i]); + printf ("\n"); + } + + int main (void) + { + uint16_t prime_numbers[] = { 2, 3, 5, 7, 11, 13, 17, 0 }; + + uint16_t *p1 = prime_numbers; + output (p1); + p1++; + output (p1); + + char *p2 = prime_numbers; + output (p2); + p2++; + output (p2); + + return 0; + } + \end{lstlisting} + + Das Programm wird compiliert und ausgeführt: + + \begin{lstlisting}[style=terminal] + $ ¡gcc -Wall aufgabe-2.c -o aufgabe-2¿ + aufgabe-2.c: In function 'main': + aufgabe-2.c:20:13: warning: initialization from + incompatible pointer type [enabled by default] + aufgabe-2.c:21:3: warning: passing argument 1 of 'output' from + incompatible pointer type [enabled by default] + aufgabe-2.c:4:6: note: expected 'uint16_t *' but argument is of type 'char *' + aufgabe-2.c:23:3: warning: passing argument 1 of 'output' from + incompatible pointer type [enabled by default] + aufgabe-2.c:4:6: note: expected 'uint16_t *' but argument is of type 'char *' + $ ¡./aufgabe-2¿ + 2 3 5 7 11 13 17 + 3 5 7 11 13 17 + 2 3 5 7 11 13 17 + 768 1280 1792 2816 3328 4352 + \end{lstlisting} + + \begin{enumerate}[\quad(a)] + \item + Erklären Sie die Funktionsweise der Funktion \lstinline{output ()}. + \points{2} + \item + Begründen Sie den Unterschied zwischen der ersten (\lstinline[style=terminal]{2 3 5 7 11 13 17})\\ + und der zweiten Zeile (\lstinline[style=terminal]{3 5 7 11 13 17}) der Ausgabe des Programms. + \points{2} + \item + Erklären Sie die beim Compilieren auftretenden Warnungen\\ + und die dritte Zeile (\lstinline[style=terminal]{2 3 5 7 11 13 17}) der Ausgabe des Programms. + \points{3} + \item + Erklären Sie die vierte Zeile (\lstinline[style=terminal]{768 1280 1792 2816 3328 4352}) + der Ausgabe des Programms.\\ +% Welche Endianness hat der verwendete Rechner?\\ +% Wie sähe die Ausgabezeile bei umgekehrter Endianness aus? +% +% 2 0 3 0 5 0 7 0 11 --> 2 3 5 7 11 +% 0 3 0 5 0 7 0 11 --> 768 1280 ... +% +% 0 2 0 3 0 5 0 7 0 11 --> 2 3 5 7 11 +% 2 0 3 0 5 0 7 0 11 --> 768 1280 ... +% +% --> Endianness nicht erkennbar! +% + Sie dürfen einen Little-Endian-Rechner voraussetzen. + \points{4} + \end{enumerate} + + \goodbreak + + \exercise{Personen-Datenbank} + + Wir betrachten das folgende Programm (\gitfile{hp}{20200109}{aufgabe-3.c}): + \begin{lstlisting} + #include <stdio.h> + #include <string.h> + + typedef struct + { + char first_name[10]; + char family_name[20]; + char day, month; + int year; + } person; + + int main (void) + { + person sls; + sls.day = 26; + sls.month = 7; + sls.year = 1951; + strcpy (sls.first_name, "Sabine"); + strcpy (sls.family_name, "Leutheusser-Schnarrenberger"); + printf ("%s %s wurde am %d.%d.%d geboren.\n", + sls.first_name, sls.family_name, sls.day, sls.month, sls.year); + return 0; + } + \end{lstlisting} + + Die Standard-Funktion \lstinline{strcpy()} bewirkt ein Kopieren eines Strings + von rechts nach links, hier also z.\,B.\ die Zuweisung der String-Konstanten + \lstinline{"Sabine"} an die String-Variable \lstinline{sls.first_name[]}. + + Das Programm wird für einen 32-Bit-Rechner compiliert und ausgeführt.\\ + (Die \lstinline[style=cmd]{gcc}-Option \lstinline[style=cmd]{-m32} sorgt dafür, + daß \lstinline[style=cmd]{gcc} Code für einen 32-Bit-Prozessor erzeugt.) + + \begin{lstlisting}[style=terminal] + $ ¡gcc -Wall -O -m32 aufgabe-2.c -o aufgabe-2¿ + $ ¡./aufgabe-2¿ + Sabine Leutheusser-Schnarrenberger wurde am 110.98.1701278309 geboren. + Speicherzugriffsfehler + \end{lstlisting} + + \begin{enumerate}[\quad(a)] + \item + Erklären Sie die Ausgabe des Programms einschließlich der Zahlenwerte. + \points{4} + \item + Welche Endianness hat der verwendete Rechner? + Begründen Sie Ihre Antwort. + \points{1} + \item + Wie sähe die Ausgabe auf einem Rechner mit entgegengesetzter Endianness aus? + \points{2} + \item + Erklären Sie den Speicherzugriffsfehler. + (Es kann sein, daß sich der Fehler auf Ihrem Rechner nicht bemerkbar macht. + Er ist aber trotzdem vorhanden.) + \points{2} + \end{enumerate} + + \begin{flushright} + \textit{Viel Erfolg!} + \end{flushright} + + \makeatletter + \immediate\write\@mainaux{\string\gdef\string\totalpoints{\arabic{points}}} + \makeatother + +\end{document} diff --git a/20200109/logo-hochschule-bochum-cvh-text-v2.pdf b/20200109/logo-hochschule-bochum-cvh-text-v2.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3725a72c764b4d9ab200553474e4262161f7a5b5 Binary files /dev/null and b/20200109/logo-hochschule-bochum-cvh-text-v2.pdf differ diff --git a/20200109/logo-hochschule-bochum.pdf b/20200109/logo-hochschule-bochum.pdf new file mode 100644 index 0000000000000000000000000000000000000000..8cad73dbb48a2b550bf29355b5a6ec895ce091f8 Binary files /dev/null and b/20200109/logo-hochschule-bochum.pdf differ diff --git a/20200109/pgscript.sty b/20200109/pgscript.sty new file mode 120000 index 0000000000000000000000000000000000000000..95c888478c99ea7fda0fd11ccf669ae91be7178b --- /dev/null +++ b/20200109/pgscript.sty @@ -0,0 +1 @@ +../common/pgscript.sty \ No newline at end of file diff --git a/README.md b/README.md index 752d952d68a7cbdc8537b084ae3008cdab6a66b1..70d6cf8382aeb1f24296e403a7c95de608856372 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ Vortragsfolien und Beispiele: * [05.12.2019: Löschen aus Strings, Hexdumps](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20191205/hp-uebung-20191205.pdf) * [12.12.2019: Kondensator, hüpfender Ball](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20191212/hp-uebung-20191212.pdf) * [19.12.2019: Trickprogrammierung, Thermometer-Baustein an I²C-Bus](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20191219/hp-uebung-20191219.pdf) + * [09.01.2020: Speicherformate von Zahlen, Zeigerarithmetik, Personen-Datenbank](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20200109/hp-uebung-20200109.pdf) Musterlösungen: ---------------