author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Wed, 10 Dec 2014 23:50:35 +0000 | |
changeset 350 | 54d6fc856950 |
parent 347 | efad8155513f |
child 351 | cd8d18f7b7ac |
permissions | -rw-r--r-- |
276
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\usepackage{../style} |
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
\usepackage{../langs} |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
4 |
\usepackage{../graphics} |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
5 |
\usepackage{../grammar} |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
6 |
\usepackage{multicol} |
276
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
|
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
\begin{document} |
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
335
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
10 |
\section*{Handout 9 (Static Analysis)} |
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
11 |
|
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
12 |
If we want to improve the safety and security of our programs, |
337
92a718b88e14
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
335
diff
changeset
|
13 |
we need a more principled approach to programming. Testing is |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
14 |
good, but as Dijkstra famously said: |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
15 |
|
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
16 |
\begin{quote}\it |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
17 |
``Program testing can be a very effective way to show the |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
18 |
\underline{\smash{presence}} of bugs, but it is hopelessly |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
19 |
inadequate for showing their \underline{\smash{absence}}.'' |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
20 |
\end{quote} |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
21 |
|
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
22 |
\noindent While such a more principled approach has been the |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
23 |
subject of intense study for a long, long time, only in the |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
24 |
past few years some impressive results have been achieved. One |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
25 |
is the complete formalisation and (mathematical) verification |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
26 |
of a microkernel operating system called seL4. |
337
92a718b88e14
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
335
diff
changeset
|
27 |
|
92a718b88e14
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
335
diff
changeset
|
28 |
\begin{center} |
92a718b88e14
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
335
diff
changeset
|
29 |
\url{http://sel4.systems} |
92a718b88e14
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
335
diff
changeset
|
30 |
\end{center} |
92a718b88e14
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
335
diff
changeset
|
31 |
|
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
32 |
\noindent In 2011 this work was included in the MIT Technology |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
33 |
Review in the annual list of the world’s ten most important |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
34 |
emerging |
337
92a718b88e14
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
335
diff
changeset
|
35 |
technologies.\footnote{\url{http://www2.technologyreview.com/tr10/?year=2011}} |
338
f1491e0d7be0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
337
diff
changeset
|
36 |
While this work is impressive, its technical details are too |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
37 |
enormous for an explanation here. Therefore let us look at |
337
92a718b88e14
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
335
diff
changeset
|
38 |
something much simpler, namely finding out properties about |
338
f1491e0d7be0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
337
diff
changeset
|
39 |
programs using \emph{static analysis}. |
335
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
40 |
|
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
41 |
Static analysis is a technique that checks properties of a |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
42 |
program without actually running the program. This should |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
43 |
raise alarm bells with you---because almost all interesting |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
44 |
properties about programs are equivalent to the halting |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
45 |
problem, which we know is undecidable. For example estimating |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
46 |
the memory consumption of programs is in general undecidable, |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
47 |
just like the halting problem. Static analysis circumvents |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
48 |
this undecidability-problem by essentially allowing answers |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
49 |
\emph{yes} and \emph{no}, but also \emph{don't know}. With |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
50 |
this ``trick'' even the halting problem becomes |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
51 |
decidable\ldots{}for example we could always say \emph{don't |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
52 |
know}. Of course this would be silly. The point is that we |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
53 |
should be striving for a method that answers as often as |
347
efad8155513f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
346
diff
changeset
|
54 |
possible either \emph{yes} or \emph{no}---just in cases when |
efad8155513f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
346
diff
changeset
|
55 |
it is too difficult we fall back on the |
efad8155513f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
346
diff
changeset
|
56 |
\emph{don't-know}-answer. This might sound all like abstract |
efad8155513f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
346
diff
changeset
|
57 |
nonsense. Therefore let us look at a concrete example. |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
58 |
|
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
59 |
|
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
60 |
\subsubsection*{A Simple, Idealised Programming Language} |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
61 |
|
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
62 |
Our starting point is a small, idealised programming language. |
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
63 |
This language, amongst other things, contains variables |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
64 |
holding integers. We want to find out what the sign of these |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
65 |
integers will be when the program runs. This sign-analysis |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
66 |
seems like a very simple problem, but it will turn out even |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
67 |
such simple problems, if approached naively, are in general |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
68 |
undecidable, just like Turing's halting problem. I let you |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
69 |
think why? |
347
efad8155513f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
346
diff
changeset
|
70 |
|
efad8155513f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
346
diff
changeset
|
71 |
|
efad8155513f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
346
diff
changeset
|
72 |
Is sign-analysis of variables an interesting problem? Well, |
efad8155513f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
346
diff
changeset
|
73 |
yes---if a compiler can find out that for example a variable |
efad8155513f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
346
diff
changeset
|
74 |
will never be negative and this variable is used as an index |
efad8155513f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
346
diff
changeset
|
75 |
for an array, then the compiler does not need to generate code |
efad8155513f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
346
diff
changeset
|
76 |
for an underflow-test. Remember some languages are immune to |
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
77 |
buffer-overflow attacks because they add underflow and |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
78 |
overflow checks everywhere. If the compiler can omit the |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
79 |
underflow test, for example, then this can potentially |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
80 |
drastically speed up the generated code. |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
81 |
|
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
82 |
What do programs in our programming language look like? The |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
83 |
following grammar gives a first specification: |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
84 |
|
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
85 |
\begin{multicols}{2} |
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
86 |
\begin{plstx}[rhs style=,one per line,left margin=9mm] |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
87 |
: \meta{Stmt} ::= \meta{label} \texttt{:} |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
88 |
| \meta{var} \texttt{:=} \meta{Exp} |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
89 |
| \texttt{jmp?} \meta{Exp} \meta{label} |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
90 |
| \texttt{goto} \meta{label}\\ |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
91 |
: \meta{Prog} ::= \meta{Stmt} \ldots{} \meta{Stmt}\\ |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
92 |
\end{plstx} |
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
93 |
\columnbreak |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
94 |
\begin{plstx}[rhs style=,one per line] : \meta{Exp} ::= \meta{Exp} \texttt{+} \meta{Exp} |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
95 |
| \meta{Exp} \texttt{*} \meta{Exp} |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
96 |
| \meta{Exp} \texttt{=} \meta{Exp} |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
97 |
| \meta{num} |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
98 |
| \meta{var}\\ |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
99 |
\end{plstx} |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
100 |
\end{multicols} |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
101 |
|
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
102 |
\noindent I assume you are familiar with such |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
103 |
grammars.\footnote{\url{http://en.wikipedia.org/wiki/Backus–Naur_Form}} |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
104 |
There are three main syntactic categories: \emph{statments} |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
105 |
and \emph{expressions} as well as \emph{programs}, which are |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
106 |
sequences of statements. Statements are either labels, |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
107 |
variable assignments, conditional jumps (\pcode{jmp?}) and |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
108 |
unconditional jumps (\pcode{goto}). Labels are just strings, |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
109 |
which can be used as the target of a jump. The conditional |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
110 |
jumps and variable assignments involve (arithmetic) |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
111 |
expressions. Expressions are either numbers, variables or |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
112 |
compound expressions built up from \pcode{+}, \pcode{*} and |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
113 |
\emph{=} (for simplicity reasons we do not consider any other |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
114 |
operations). We assume we have negative and positive numbers, |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
115 |
\ldots \pcode{-2}, \pcode{-1}, \pcode{0}, \pcode{1}, |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
116 |
\pcode{2}\ldots{} An example program that calculates the |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
117 |
factorial of 5 is as follows: |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
118 |
|
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
119 |
\begin{lstlisting}[language={},xleftmargin=10mm] |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
120 |
a := 1 |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
121 |
n := 5 |
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
122 |
top: |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
123 |
jmp? n = 0 done |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
124 |
a := a * n |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
125 |
n := n + -1 |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
126 |
goto top |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
127 |
done: |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
128 |
\end{lstlisting} |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
129 |
|
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
130 |
\noindent Each line of the program contains a statement. In |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
131 |
the first two lines we assign values to the variables |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
132 |
\pcode{a} and \pcode{n}. In line 4 we test whether \pcode{n} |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
133 |
is zero, in which case we jump to the end of the program |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
134 |
marked with the label \pcode{done}. If \pcode{n} is not zero, |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
135 |
we multiply the content of \pcode{a} by \pcode{n}, decrease |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
136 |
\pcode{n} by one and jump back to the beginning of the loop, |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
137 |
marked with the label \pcode{top}. |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
138 |
|
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
139 |
\begin{figure}[t] |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
140 |
\begin{lstlisting}[numbers=none, |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
141 |
language={},xleftmargin=10mm] |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
142 |
n := 6 |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
143 |
m1 := 0 |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
144 |
m2 := 1 |
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
145 |
loop: |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
146 |
jmp? n = 0 done |
346
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
147 |
tmp := m2 |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
148 |
m2 := m1 + m2 |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
149 |
m1 := tmp |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
150 |
n := n + -1 |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
151 |
goto top |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
152 |
done: |
5a6e8b7d20f7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
338
diff
changeset
|
153 |
\end{lstlisting} |
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
154 |
\label{A mystery program in our idealised programming language.} |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
155 |
\end{figure} |
338
f1491e0d7be0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
337
diff
changeset
|
156 |
|
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
157 |
Even if our language is rather small, it is still Turing |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
158 |
complete---so rather powerful. However, discussing this more |
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
159 |
would lead us to far astray. |
276
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
|
350
54d6fc856950
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
347
diff
changeset
|
161 |
What would be missing in comparison with real |
335
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
162 |
(low-level machine) code? Well, the numbers we assume to be |
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
163 |
arbitrary precision, which is not the case in real code. There |
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
164 |
basic number formats have a rang and might over-run or |
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
165 |
under-run from this range. Our assumption about variables, |
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
166 |
does not correspond to actual registers, which are only |
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
167 |
limited on real hardware. Obviously, real code has richer |
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
168 |
operations than just addition, multiplication and equality. |
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
169 |
But this are not really essential limitations of our simple |
06d5fc15594d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
276
diff
changeset
|
170 |
examples. |
276
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
171 |
|
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
172 |
\end{document} |
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
173 |
|
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
174 |
%%% Local Variables: |
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
175 |
%%% mode: latex |
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
176 |
%%% TeX-master: t |
d7109c6e721d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
177 |
%%% End: |