リビジョン | ae8be4c5ebe331e8782c89153137ed4095c0d505 (tree) |
---|---|
日時 | 2020-11-12 23:23:38 |
作者 | H. Turgut Uyar <uyar@teki...> |
コミッター | H. Turgut Uyar |
add transcripts and metadata to programming slides
@@ -4,5 +4,7 @@ | ||
4 | 4 | *.css |
5 | 5 | *.css.map |
6 | 6 | *.js |
7 | ||
8 | +*.zip | |
7 | 9 | |
8 | 10 | .sass-cache |
@@ -0,0 +1,6 @@ | ||
1 | +requires: | |
2 | + - processor | |
3 | + - hexadecimal-notation | |
4 | + | |
5 | +provides: | |
6 | + - programming-definition |
@@ -30,7 +30,7 @@ | ||
30 | 30 | .. |
31 | 31 | |
32 | 32 | * instructions encoded as numbers |
33 | -* `depends on the processor type <annotate://box/orange>`_ | |
33 | +* `depends on the processor <annotate://box/orange>`_ | |
34 | 34 | |
35 | 35 | ---- |
36 | 36 |
@@ -52,11 +52,14 @@ | ||
52 | 52 | |
53 | 53 | ---- |
54 | 54 | |
55 | -High-Level Languages | |
56 | -==================== | |
55 | +Source Code | |
56 | +=========== | |
57 | 57 | |
58 | 58 | * very difficult to write machine code directly |
59 | 59 | |
60 | + * what instructions are available? | |
61 | + * in which memory locations are the data? | |
62 | + | |
60 | 63 | .. |
61 | 64 | |
62 | 65 | * write program in a friendlier language |
@@ -66,14 +69,26 @@ | ||
66 | 69 | |
67 | 70 | .. class:: substep |
68 | 71 | |
69 | -* use a program to convert source code to machine code | |
72 | +* use a program to convert it to machine code | |
73 | + | |
74 | +---- | |
75 | + | |
76 | +Friendlier? | |
77 | +=========== | |
78 | + | |
79 | +* more abstract concepts | |
80 | + | |
81 | +.. | |
82 | + | |
83 | +* using names instead of numbers to refer to memory locations | |
84 | +* grouping multiple instructions into `statements <annotate://underline/orange>`_ | |
70 | 85 | |
71 | 86 | ---- |
72 | 87 | |
73 | 88 | Conversion to Machine Code |
74 | 89 | ========================== |
75 | 90 | |
76 | -* source code doesn't depend on processor type | |
91 | +* source code doesn't depend on processor | |
77 | 92 | * but machine code does |
78 | 93 | |
79 | 94 | .. |
@@ -0,0 +1,208 @@ | ||
1 | +Programming | |
2 | +=========== | |
3 | + | |
4 | +Let's look at the definition of a computer program. | |
5 | +This definition is taken from Wikipedia, | |
6 | +but you can find similar definitions in many resources. | |
7 | + | |
8 | +Let's first read the full definition and then go over it: | |
9 | + | |
10 | +"A computer program is a collection of instructions | |
11 | + that can be executed by a computer | |
12 | + to perform a specific task." | |
13 | + | |
14 | +The first part tells us that a program consists of instructions. | |
15 | +Do this, then do that, then do that, and so on. | |
16 | + | |
17 | +The second part tells that these instructions are meant to be executed | |
18 | +by a computer. | |
19 | +So they must be things that the computer "understands", | |
20 | +in other words, knows how to carry out. | |
21 | + | |
22 | +And the third part tells that the program will perform a specific task, | |
23 | +like solving a particular problem. | |
24 | + | |
25 | +[PRESS ENTER TO PROCEED TO SUBSTEP] | |
26 | + | |
27 | +And we can define programming | |
28 | +as the act of designing and implementing computer programs. | |
29 | + | |
30 | +---- | |
31 | + | |
32 | +Machine Code | |
33 | +============ | |
34 | + | |
35 | +We said that programs consist of instructions to the computer. | |
36 | +We call this collection of all instructions that make up the program | |
37 | +the "machine code" of that program. | |
38 | + | |
39 | +Since computers can basically only work with numbers, | |
40 | +everything needs to be somehow represented by numbers. | |
41 | +This includes the instructions to the computer, | |
42 | +and also all of the operands that these instructions will work on. | |
43 | + | |
44 | +The component of the computer that carries out instructions is its processor. | |
45 | +There are many different processors | |
46 | +which have been manufactured by different companies. | |
47 | +These different processors can understand different instructions. | |
48 | +Even if they have the same instruction, | |
49 | +the number they use to represent the instruction will be different. | |
50 | +Therefore the machine code will be different between different processors, | |
51 | +in other words, [CLICK ON] it depends on the processor. | |
52 | + | |
53 | +---- | |
54 | + | |
55 | +Machine Code Example | |
56 | +==================== | |
57 | + | |
58 | +Let's look at an example. | |
59 | +Say, given what year we're in and the birth year of a person, | |
60 | +we want to figure out if that person is under the age of 18 or not. | |
61 | + | |
62 | +We can express this calculation like this: | |
63 | + | |
64 | +Get the value located in memory address 47 into the processor. | |
65 | +Here we are assuming that this memory location contains the current year. | |
66 | + | |
67 | +Subtract the value located in memory address 62 from that year value. | |
68 | +We are assuming that the person's birth year is stored in that location. | |
69 | +The result of the subtraction will be the age of the person. | |
70 | + | |
71 | +Check whether the result is less than 18. | |
72 | +If it is, continue with the instruction that is located | |
73 | +4 positions ahead from the current memory location. | |
74 | + | |
75 | +Now let's see how these instructions can be represented in machine code | |
76 | +on some processor. | |
77 | + | |
78 | +[PRESS ENTER TO PROCEED TO SUBSTEP] | |
79 | + | |
80 | +Here we are using hexadecimal notation. | |
81 | +If we look closely, we can roughly understand how this code is structured. | |
82 | + | |
83 | +For example, the decimal number 47 is represented as 2F in hexadecimal | |
84 | +(2 times 16 makes 32, plus 15 makes 47). | |
85 | +And the machine code starts with A12F. | |
86 | +We can guess that "A1" (161 in decimal) is the number | |
87 | +that represents the instruction of getting | |
88 | +the value of a given memory location into the processor. | |
89 | +The following six zeroes are part of this memory address. | |
90 | + | |
91 | +The decimal number 62 is represented as 3E in hexadecimal | |
92 | +(3 times 16 makes 48, plus 14 makes 62). | |
93 | +The part that starts with "2B053E" must represent the next instruction. | |
94 | +So, "2B05" likely means | |
95 | +"subtract the given operand (in this case, 62) | |
96 | +from the value in the processor". | |
97 | +Again the address "3E" is followed by six zeroes to form a full memory address. | |
98 | + | |
99 | +Next we have the comparison operation. | |
100 | +Decimal 18 corresponds to hexadecimal 12 (16 plus 2). | |
101 | +In the code "83F812", | |
102 | +"83F8" likely means "compare the value in the processor with the given value" | |
103 | +(in this case, 18). | |
104 | + | |
105 | +And finally, in the remaining part "7C04", | |
106 | +the code "7C" must mean something like | |
107 | +"if the value in the processor is less (as a result of this comparison), | |
108 | +continue with instruction which is the given number of positions ahead". | |
109 | +And "04" is the number of positions. | |
110 | + | |
111 | +---- | |
112 | + | |
113 | +Source Code | |
114 | +=========== | |
115 | + | |
116 | +You can see that it's very difficult to write code like this. | |
117 | +We have to know what the available instructions are, | |
118 | +what operands they take, where every piece of data is, and so on. | |
119 | + | |
120 | +So, the idea is: "let's write our code in a friendlier (or easier) language". | |
121 | +We call this code the "source code" of the program. | |
122 | + | |
123 | +[PRESS ENTER TO PROCEED TO SUBSTEP] | |
124 | + | |
125 | +And then we use a program that will convert this source to machine code. | |
126 | + | |
127 | +---- | |
128 | + | |
129 | +Friendlier? | |
130 | +=========== | |
131 | + | |
132 | +But, what does "friendlier" mean? | |
133 | +What makes a language friendly? | |
134 | + | |
135 | +In this context, friendliness refers to the ability to use | |
136 | +more abstract concepts rather than the platform details. | |
137 | + | |
138 | +For example, instead of having to know at which memory location | |
139 | +some data is, we could use a name for the data | |
140 | +and the convertor would figure out the actual memory address. | |
141 | + | |
142 | +Or, instead of having to know how to carry out a subtraction operation, | |
143 | +we could express it using the familiar mathematical notation | |
144 | +and the convertor could generate the necessary instruction sequence. | |
145 | +So, one [CLICK ON] statement could correspond to multiple instructions. | |
146 | + | |
147 | +---- | |
148 | + | |
149 | +Conversion to Machine Code | |
150 | +========================== | |
151 | + | |
152 | +This approach creates another opportunity for us. | |
153 | +Since we're not dealing with what instructions the processor can understand, | |
154 | +our source code doesn't have to depend on the processor anymore. | |
155 | + | |
156 | +But -as we have discussed- the machine code does depend on the processor, | |
157 | +therefore our convertor has to take our source code | |
158 | +and generate the proper machine code [CLICK ON] for a particular processor. | |
159 | + | |
160 | +[PRESS ENTER TO PROCEED TO SUBSTEP] | |
161 | + | |
162 | +But the nice thing is, as programmers, we don't care | |
163 | +how this conversion is made. | |
164 | + | |
165 | +---- | |
166 | + | |
167 | +Portability | |
168 | +=========== | |
169 | + | |
170 | +If we sum it up, we write our processor-independent source code | |
171 | +and we use an appropriate convertor to generate machine code from it. | |
172 | + | |
173 | +If we want to generate machine code for another processor, | |
174 | +we have to find another convertor that can do this for us. | |
175 | +But ideally, we shouldn't have to change our source code. | |
176 | +If that is the case, we say that our source code is "portable". | |
177 | + | |
178 | +[PRESS ENTER TO PROCEED TO SUBSTEP] | |
179 | + | |
180 | +Unfortunately, it's not just the processor differences | |
181 | +that prevents source code from being portable. | |
182 | +Other factors, most importantly the operating system | |
183 | +on which the program will run, also play significant roles. | |
184 | +So, instead of "processor-independent", | |
185 | +we use the term "platform-independent" | |
186 | +where the term platform indicates the processsor and the operating system, | |
187 | +and possibly other factors. | |
188 | + | |
189 | +---- | |
190 | + | |
191 | +Language Levels | |
192 | +=============== | |
193 | + | |
194 | +When we've discussed the difficulties of writing our programs directly | |
195 | +in machine code, | |
196 | +we've used the term "friendlier" language. | |
197 | +This "friendliness" of a language is in fact on a scale, | |
198 | +and it's called the "level" of a language. | |
199 | + | |
200 | +Languages where the programmer has to be more familiar with the details | |
201 | +of the platform are called "lower-level languages" (so, less friendly). | |
202 | +And languages where the source code is closer to the way humans think | |
203 | +and express themselves are called "higher-level languages". | |
204 | + | |
205 | +These are loose definitions and there's no well defined scale | |
206 | +that ranks languages based on these criteria. | |
207 | +But generally, a language that is reasonably easy to understand | |
208 | +and to write source code in, is called a "high-level" language. |
@@ -0,0 +1,2 @@ | ||
1 | +requires: | |
2 | + - programming-definition |
@@ -11,9 +11,9 @@ | ||
11 | 11 | |
12 | 12 | * interpreting: |
13 | 13 | |
14 | - * convert first instruction | |
14 | + * convert first statement | |
15 | 15 | * execute it |
16 | - * convert next instruction | |
16 | + * convert next statement | |
17 | 17 | * execute it |
18 | 18 | * ... |
19 | 19 |
@@ -23,8 +23,8 @@ | ||
23 | 23 | |
24 | 24 | * | convert whole source code |
25 | 25 | | into executable code |
26 | - * execute first instruction | |
27 | - * execute next instruction | |
26 | + * execute first statement | |
27 | + * execute next statement | |
28 | 28 | * ... |
29 | 29 | |
30 | 30 | ---- |
@@ -0,0 +1,75 @@ | ||
1 | +Conversion Methods | |
2 | +================== | |
3 | + | |
4 | +There are two ways to convert and run a source code. | |
5 | + | |
6 | +In the interpretation approach, the source code is processed | |
7 | +statement by statement. | |
8 | +That means, the first statement gets read, converted, and executed. | |
9 | +And then the next statement gets read, converted, and executed. | |
10 | +And it continues like this until the program terminates. | |
11 | + | |
12 | +In the compilation approach, the source code is processed as a whole. | |
13 | +First, the entire source code is converted to machine code, | |
14 | +that is, all statements are converted at once, | |
15 | +and then they are executed in sequence. | |
16 | + | |
17 | +---- | |
18 | + | |
19 | +Interpreting | |
20 | +============ | |
21 | + | |
22 | +This diagram shows the stages of interpreted execution. | |
23 | +Both the source code and the inputs are fed into the interpreter | |
24 | +which does both the conversion and the execution, | |
25 | +and produces the outputs. | |
26 | + | |
27 | +[PRESS ENTER TO PROCEED TO SUBSTEP] | |
28 | + | |
29 | +The conversion from source code to machine code is done | |
30 | +while the program is running | |
31 | +-using the technical term, "at runtime". | |
32 | + | |
33 | +Interpretation is more flexible than compilation | |
34 | +because not everything has to be decided up front. | |
35 | + | |
36 | +---- | |
37 | + | |
38 | +Compiling | |
39 | +========= | |
40 | + | |
41 | +In the compilation method, the conversion and execution -or "running"- | |
42 | +stages are separated. | |
43 | +The source code is fed into the compiler which produces the machine code. | |
44 | +And then the machine code and inputs are fed into the executor | |
45 | +which produces the outputs. | |
46 | + | |
47 | +Here, the conversion is done at "compile time". | |
48 | + | |
49 | +Compilation is significantly faster than interpretation | |
50 | +because the conversion is already finished before the program starts running. | |
51 | + | |
52 | +---- | |
53 | + | |
54 | +Programmer's Workflow | |
55 | +===================== | |
56 | + | |
57 | +To sum up, here's how programmers develop programs: | |
58 | + | |
59 | +First, they write some source code. | |
60 | + | |
61 | +Then, if using a compiled language, they run it through the compiler | |
62 | +to generate machine code. | |
63 | + | |
64 | +If there are any errors in the code that prevent the compiler | |
65 | +from generating the machine code, they go back to editing the source code. | |
66 | + | |
67 | +If the compiler doesn't give any errors and does generate a machine code, | |
68 | +they run the machine code and check whether the program behaves the way | |
69 | +it's expected to behave or not, | |
70 | +like producing correct outputs for given inputs. | |
71 | +Note that, just because the compiler didn't give any errors | |
72 | +it doesn't mean that the program will work as intended. | |
73 | +And if the program doesn't work correctly, | |
74 | +it's time to figure out what's causing the problem | |
75 | +and go back to editing the source code. |