-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexercise.txt
385 lines (343 loc) · 10.3 KB
/
exercise.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
Exercises for yourforth.
We follow the convention of Knuth in TAO:
Exercises are numbered per section.
Each exercise has a rating 0..50, where 40 is research project and
under 10 an easy question that you can asnwer inyour head.
At first, just skip the hard exercises.
\ -------------------- 0 Introduction --------------------------
0.0 If you're new at Forth you must read the Chapter "gentle introduction"
of yourforth.pdf.
Was it painful?
Answer:
0.1 Use as a calculator.
Go to a system with a decent keyboard and numeric keypad and
switch it to numeric. Now type the following into <your>forth,
ending each line with the <ENTER> key from the keypad.
"
123
.
123
123
123
+
+
123
3
*
-
.
"
Now repeat the experiment, with the normal keyboard typing
.S after each word.
What are the first and second results?
Answer:
0.2 Regression test.
There is a test supplied called tsuiteyour.frt.
Run it through yourforth as follows:
yourforth <tsuiteyour.frt | more
This is a regression test, familiarize yourself with the output.
We must know how this test signals defects.
Remove the word MIN from yourforth, i.e. its header line and
lines following up till MAX.
Try to run the test again. Why doesn't this work?
Answer:
Now keep the word, but change its Forth name MIN to APE.
What number has the error revealed by tsuiteyour.frt?
Answer:
Look the error up in yourforth.pdf.
What is the name of the chapter it is in?
Answer:
What are the three words where this error is detected?
Hint: all errors are signalled by ?ERROR. Use the source!
Answer:
In yourforth.fas remove the content of COUNT , except for the closing EXIT
and run the test again. What error message do you get?
Answer:
\ ---------------------- 1 memory and adressing ---------------------------------
1.1 Memory size.
Type WORDS to verify that BM and EM are indeed Forth words.
How many items are left by BM? Hint use .S .
Answer:
What do you expect as a result of `` EM BM - . ''? Now try it.
Answer:
1.2 Usable size.
Find the place where forthsize is defined:
forthsize equ 0x100000 ; 1 Mbyte
Reassemble with 2 Mbyte and check that the resulting Forth
works the same. Hint: use the regression test.
How are you going to verify the size change from within <your>forth?
Answer:
1.3 Insight in memory layout.
Which RB (reserve byte) statement is responsible for allocating the bulk of the
working space for <your>forth?
Answer:
There are only four labels in the high uninitialized part of the memory.
Name them.
Answer:
(30) 1.4. memory layout and Forth.
Where are they used? What is the type/class of those words?
Answer:
\ ---------------------- 2 input and output ------------
2.1 Just a bunch of characters.
Define a word as with
CREATE x[32myourforth
Show that it is in the list using WORDS.
Now do
LATEST >NFA @ $@
This leaves the name of that definition as you can see with
2DUP TYPE
Now do
DROP HEX 1B SWAP C!
This changes the first letter (x) into an escape.
What do you see after the following?
LATEST >NFA @ $@ TYPE
Answer:
2.2 Interfacing with Linux
Just do
1 XOS
What happens?
Answer:
------------------------
Define a word as per
"APE" (CREATE)
and verify that is is in the dictionary list.
What happens if you type APE?
Answer:
To make APE behave, we try
20 ALLOT
'NOOP 5 CELLS + 'APE 5 CELLS + 20 MOVE
Is this sufficient? Why or why not?
Answer:
Where is the ALLOT for?
Answer:
Define a test to show that the ALLOT is needed. i.e. that fails
if you leave out `` 20 ALLOT ''. State it!
Answer:
'APE >NFA @ $@ TYPE
You've earned your badge, from now on you may do
'APE ID.
Try out the following (don't worry if you don't quite understand it)
: APE "Kiss my ape" TYPE CR ;
: NOTE "Crack note" TYPE CR ;
: AN APE NOTE ;
AN
WORDS
"MISS" $, 'APE >NFA !
AN
WORDS
What is this code doing to the word APE?
What do you see in WORDS?
Answer:
FIXME : intended for a non-trivial program.
INCLUDE sieve.frt
SIEVE \ 2000 primes
WORDS
: erase-links 'SIEVE BEGIN >LFA DUP @ WHILE DUP @ >R 0 SWAP ! R> >LFA REPEAT ;
SIEVE
WORDS
erase-links
SIEVE
WORDS
What does sieve do? What does erase-links do?
Answer:
Does SIEVE still work after erase-links? And WORDS?
Answer:
: APE "APE" TYPE CR ;
: NOTE "NOTE" TYPE CR ;
'APE DUP . >FFA @ .
'APE HIDDEN
WORDS
'APE >FFA @
Why doesn't this work?
Answer:
Find another way to inspect APE 's flagfield. State it.
Answer:
17 CONSTANT APE
What is contained in APE 's datafield. State the code to inspect it.
"NOTE" (CREATE)
What is contained in NOTE's datafield.
Answer:
"APE" (CREATE)
1789172 'APE >CFA !
APE
"NOTE" (CREATE)
HEX
51 C, 52 C, 52 C, AD C, FF C,
NOTE
Put some values on the stack and try to find out what NOTE does.
What happens if you type APE? What does NOTE?
Answer:
define 3 words A B C as per
"A" (CREATE)
"B" (CREATE)
"C" (CREATE)
WORDS
You can manipulate the link fields such that the word
B disappears from the dictionary list.
Which link field must be changed, A's or C's ?
Answer:
Now do it and check with WORDS. State how!
Answer:
Now *on a virgin system* put 1000,000 on the stack and type
INCLUDE examples/grow.frt
You have a new file in your directory, now run it.
How large is the size of this new forth in megabytes?
Hint: use `` EM BM - 1000000 / ''
Answer:
What is the difference in size of this new forth's binary file with yourforth?
Answer:
Again do
1000000000 INCLUDE examples/grow.frt
and check the size.
Now that we have 1 Gbyte, do
INCLUDE examples/sieve.frt .
The last number printed is the number of primes under 1000,000,000,
what is it?
Now read the file grow.frt and try to understand what is going on.
Answer:
Try to get the working space at around 3 Gbyte.
What happens?
Find the error detecting line in grow.frt and comment it out.
Does it work?
Why does
EM BM - 1000000 / .
indicate that you have minus thousand Megabytes?
Find a Forth word to print `` EM BM - '' correctly.
Answer:
Exercise: Create two words red and green that appear in color if you
type WORDS.
Try this
'FORTH CONTEXT !
Which words remain in the dictionary?
Choose your own way to find it out. State which one you used.
1 experimentally
2. Find a way to get a wordlist of the state of the dictionary as per WORDS.
3. code inspection
Answer:
What with numbers?
Answer:
Study the code (in the assembler listing) that is executed if you type
FORTH
and explain the difference with 'FORTH CONTEXT !
Answer:
Can you recover to a full Forth after 'FORTH CONTEXT !
Answer:
(advanced: How can you do it if we give you EXECUTE ?
Answer:
Verify this by making a <your>forth that has
EXECUTE still available after
'FORTH CONTEXT ! )
State what you did.
Answer:
Try this
INCLUDE assembler.frt
CODE NOOP2 NEXT, END-CODE
Verify that NOOP2 is a no operation, i.e. it does nothing.
Do WORDS and remark that a lot if new words have appeared.
Get rid of the assembler words in the dictionary list while
keeping NOOP2. State how!
Answer:
Do
objdump -x yourforth
Now compare it with some other compilers, one or more of
objdump -x `which jonesforth`
objdump -x `which gcc`
objdump -x `which gforth`
objdump -x `which java`
objdump -x `which python`
objdump -x `which lina`
What is your impression?
Answer:
[
(advance) Assuming that fasm does all the right things with elf headers,
find out what fields in the elf header change if forthsize is changed.
Now define a word GROW that reads a <your>forth binary and writes
a new binary with an enlarged, then a hugely enlarged space.
How far can you push it?
]
Find a document describing ELF headers.
How many pages is it? How many pages in your estimation would you
have to study to do the above experiment without help of fasm?
Make a crude estimate of the time it would take.
Answer:
Switch to HEX.
Verify that the character at BM-0x54 is 0x7F.
(If not skip the remainder of the exercise)
What is the result of
BM 53 - 3 TYPE ?
Answer:
What doethis mean?
Answer:
Do the next experiment with an absolute virgin
<your>forth.
Save the current forth in a file as per
BM 54 - HERE OVER - "yourforth2" PUT-FILE
Try to execute yourforth2. What do you find?
Answer:
Add a definition to <your>forth
: A ;
and repeat the experiment.
Try to execute yourforth2. What do you find?
Why would that be?
Answer:
Write a noop, a machine code definition that does nothing.
Define a word H. that prints a cell as 8 hex digits with a comma for
readability like so 0xDEAD,BEEF
Hints: don't bother with restoring BASE yet, and use a phrase like &x HOLD .
Answer:
Now try it on a small number like 17
Redefine H. inorder to restore BASE to what it was before,
in this manner
: H. ... H. ... ;
Answer:
With this try
HEX DEADBEAF DECIMAL DUP . H.
also try
HEX DEADBEAF DECIMAL DUP H. .
What do you see?
Answer:
Advanced: Change the source of yourforth to print hex numbers in lower case
like so 1eadbeef.
Can you think of a disadvantage?
Answer:
Advanced: Modify yourforth to add a prefix 0x such that it understand hex numbers like
0xDEADBEEF (no comma's and no lower case). State the changes!
Hint: look at + for an example and make sure that you restore base like
in a previous example.
Answer:
Now make a file c_compatible_0x.frt that can be loaded to do the
same thing. State the content!
Answer:
Try it out on some of your favorite numbers.
Does -0x12 work, or 0x-12 ? Your opinion about this?
Answer:
make a yourforth with the code and data field Interchanged
the code field second
and the data field first.
How many headers do you have to change?
Answer:
At how many other places are there changes?
Answer:
Would this have been easier if 1*cw and 0*cw where appropriate had been changed
to D_HOFFSET and C_HOFFSET?
Answer:
Would it have been worthwhile?
Answer:
2. That change would've been about equal to this assignment.
(All reasoning accepted.)
Make a file with a simple test like:
"hello there" TYPE CR
"yes! what?" TYPE CR
Now pass it to yourforth using the redirection mechanism.
Now remove the test for end of pipe in QUIT and repeat the test.
What do you see?
Answer:
What modification to the test file can solve this?
Answer:
Some Linux commands take 5 parameters instead of 3.
Add a word XOS5 to <your>forth similiar to XOS that takes 5 parameters
and a command number, knowing that the 4-th parameter goes into
SI and the 5-th goes into DI . Remember what we said about SI.
Find a way to solve the problem (hint return stack).
Now define BYE using XOS5. State the definition!
Answer: