about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--transect/ex3.k222
-rw-r--r--transect/ex4.k234
-rw-r--r--transect/ex5.k230
-rw-r--r--transect/ex6.k223
-rw-r--r--transect/ex7.k264
-rw-r--r--transect/ex8.k236
-rw-r--r--transect/factorial.k216
7 files changed, 225 insertions, 0 deletions
diff --git a/transect/ex3.k2 b/transect/ex3.k2
new file mode 100644
index 00000000..63151396
--- /dev/null
+++ b/transect/ex3.k2
@@ -0,0 +1,22 @@
+# add the first 10 numbers, and return the result in the exit code
+
+fn main [
+  var result/EBX : int
+  result/EBX <- copy 0
+  var counter/ECX : int
+  counter/ECX <- copy 1
+  {
+    compare counter/ECX, 10
+    break-if >
+    result/EBX <- add counter/ECX
+    counter/ECX <- add 1
+    loop
+  }
+  call exit, 1
+]
+
+fn exit x : int [
+  code/EBX <- copy x
+  code/EAX <- copy 1/exit
+  syscall
+]
diff --git a/transect/ex4.k2 b/transect/ex4.k2
new file mode 100644
index 00000000..9a7ddee7
--- /dev/null
+++ b/transect/ex4.k2
@@ -0,0 +1,34 @@
+## read a character from stdin, save it to a global, write it to stdout
+
+# variables are always references
+#   read their address with their names: x (can't write to their address)
+#   read/write their contents with a lookup: *x
+var x : char
+
+fn main [
+  call read 0/stdin, x, 1/size
+  result/EAX <- call write 1/stdout, x, 1/size
+  call exit, result/EAX
+]
+
+fn read fd : int, x : (address array byte), size : int [
+  EBX <- copy fd
+  ECX <- copy x
+  EDX <- copy size
+  EAX <- copy 3/read
+  syscall
+]
+
+fn write fd : int, x : (address array byte), size : int [
+  EBX <- copy fd
+  ECX <- copy x
+  EDX <- copy size
+  EAX <- copy 4/write
+  syscall
+]
+
+fn exit x : int [
+  code/EBX <- copy x
+  code/EAX <- copy 1/exit
+  syscall
+]
diff --git a/transect/ex5.k2 b/transect/ex5.k2
new file mode 100644
index 00000000..0d7148fe
--- /dev/null
+++ b/transect/ex5.k2
@@ -0,0 +1,30 @@
+# read a character from stdin, save it to a local on the stack, write it to stdout
+
+fn main [
+  var x : char
+  call read 0/stdin, x, 1/size
+  result/EBX <- call write 1/stdout, x, 1/size
+  call exit-EBX
+]
+
+fn read fd : int, x : (address array byte), size : int [
+  EBX <- copy fd
+  ECX <- copy x
+  EDX <- copy size
+  EAX <- copy 3/read
+  syscall
+]
+
+fn write fd : int, x : (address array byte), size : int [
+  EBX <- copy fd
+  ECX <- copy x
+  EDX <- copy size
+  EAX <- copy 4/write
+  syscall
+]
+
+# like exit, but assumes the code is already in EBX
+fn exit-EBX [
+  code/EAX <- copy 1/exit
+  syscall
+]
diff --git a/transect/ex6.k2 b/transect/ex6.k2
new file mode 100644
index 00000000..cd68f861
--- /dev/null
+++ b/transect/ex6.k2
@@ -0,0 +1,23 @@
+## print out a (global variable) string to stdout
+
+var size : int = 14
+var x : (array character) = "hello, world!"
+
+fn main [
+  call write 1/stdout, x, size
+  call exit, 0
+]
+
+fn write fd : int, x : (address array byte), size : int [
+  EBX <- copy fd
+  ECX <- copy x
+  EDX <- copy size
+  EAX <- copy 4/write
+  syscall
+]
+
+fn exit x : int [
+  code/EBX <- copy x
+  code/EAX <- copy 1/exit
+  syscall
+]
diff --git a/transect/ex7.k2 b/transect/ex7.k2
new file mode 100644
index 00000000..3ede2c5e
--- /dev/null
+++ b/transect/ex7.k2
@@ -0,0 +1,64 @@
+# example showing file syscalls
+#
+# Create a file, open it for writing, write a character to it, close it, open
+# it for reading, read a character from it, close it, delete it, and return
+# the character read.
+
+var stream : int = 0
+var a : char = 97
+var b : char = 0
+var filename : (array char) = ".foo"
+
+fn main [
+  call create, filename
+  *stream <- call open, filename, 1/wronly
+  call write, *stream, a, 1
+  call close, *stream
+  stream <- call open, filename, 0/rdonly
+  call read, *stream, b, 1
+  call close, *stream
+  call unlink, filename
+  var result/EBX : char
+  result/EBX <- copy b  # TODO: copy char to int?
+  call exit-EBX
+]
+
+fn read fd : int, x : (address array byte), size : int [
+  EBX <- copy fd
+  ECX <- copy x
+  EDX <- copy size
+  EAX <- copy 3/read
+  syscall
+]
+
+fn write fd : int, x : (address array byte), size : int [
+  EBX <- copy fd
+  ECX <- copy x
+  EDX <- copy size
+  EAX <- copy 4/write
+  syscall
+]
+
+fn open name : (address array byte) [
+  EBX <- copy name
+  EAX <- copy 5/open
+  syscall
+]
+
+fn close name : (address array byte) [
+  EBX <- copy name
+  EAX <- copy 6/close
+  syscall
+]
+
+fn unlink name : (address array byte) [
+  EBX <- copy name
+  EAX <- copy 10/unlink
+  syscall
+]
+
+# like exit, but assumes the code is already in EBX
+fn exit-EBX [
+  code/EAX <- copy 1/exit
+  syscall
+]
diff --git a/transect/ex8.k2 b/transect/ex8.k2
new file mode 100644
index 00000000..dfef03b0
--- /dev/null
+++ b/transect/ex8.k2
@@ -0,0 +1,36 @@
+# Example reading commandline arguments: compute length of first arg.
+
+fn main argc : int, argv : (array (ref array char)) -> [
+  var tmp : (index char)
+  tmp <- index 1, %size(ref array char)
+  var tmp2 : (address (ref array char))
+  tmp2 <- advance argv, tmp
+  var s/EBX : (ref array char)
+  s/EBX <- copy *tmp2
+  var result/EAX : int
+  result/EAX <- ascii_length s/EBX
+  call exit, result/EAX
+]
+
+fn ascii_length s : (ref array char) -> result : int [
+  var result/EBX : int
+  result/EBX <- copy 0
+  {
+    var tmp0/EDI : (offset char)
+    tmp0/EDI <- index result/EBX, %size(char)
+    var tmp/EDX : (address char)
+    tmp/EDX <- advance *s, tmp0/EDI
+    var c/ECX : char
+    c/ECX <- copy *tmp
+    compare c/ECX, 0
+    break-if-equal
+    loop
+  }
+  return result/EBX
+]
+
+fn exit x : int [
+  code/EBX <- copy x
+  code/EAX <- copy 1/exit
+  syscall
+]
diff --git a/transect/factorial.k2 b/transect/factorial.k2
new file mode 100644
index 00000000..79269773
--- /dev/null
+++ b/transect/factorial.k2
@@ -0,0 +1,16 @@
+# compute the factorial of 5, and return the result in the exit code
+
+fn factorial n : int -> result/EAX : int [
+  result/EAX <- copy 1
+  {
+    compare n, 1
+    break-if <=
+    var tmp/EBX : int
+    tmp/EBX <- copy n
+    tmp/EBX <- subtract 1
+    var tmp2/EAX : int
+    tmp2/EAX <- call factorial, tmp/EBX
+    result/EAX <- multiply tmp2/EAX, n
+  }
+  return result/EAX
+]
01:38:47 -0700 4014 - core skeleton for x86 interpreter' href='/akkartik/mu/commit/subx/010core.cc?h=hlt&id=f8831a023d91556eb105edc27e7cbb61bde573ca'>f8831a02 ^
630433cd ^
0474ec86 ^
4a99a6e0 ^


630433cd ^
ed79099b ^

630433cd ^



cfaac2a8 ^
4a99a6e0 ^
630433cd ^







cfaac2a8 ^
9ee351f3 ^
cfaac2a8 ^

cfaac2a8 ^
bfe9fa11 ^



cfaac2a8 ^




630433cd ^


df39c0c8 ^


630433cd ^
630433cd ^


df39c0c8 ^
















630433cd ^




f8831a02 ^
630433cd ^

f8831a02 ^
763396e5 ^
a893d3b6 ^
665a4d70 ^



630433cd ^

665a4d70 ^

880b8e91 ^
665a4d70 ^

630433cd ^
880b8e91 ^
665a4d70 ^

880b8e91 ^
665a4d70 ^


16db2a2f ^







fb2ffbd0 ^
90e6596f ^
c2a74205 ^
c2a74205 ^
fb2ffbd0 ^
16db2a2f ^
665a4d70 ^

880b8e91 ^
665a4d70 ^

16db2a2f ^








90e6596f ^
16db2a2f ^
c2a74205 ^
16db2a2f ^

665a4d70 ^

630433cd ^
665a4d70 ^
b396b7b4 ^
54ed56f2 ^
880b8e91 ^
b396b7b4 ^
54ed56f2 ^

7163541b ^
54ed56f2 ^



665a4d70 ^

880b8e91 ^

665a4d70 ^

880b8e91 ^

665a4d70 ^

880b8e91 ^

665a4d70 ^

880b8e91 ^

665a4d70 ^
f8831a02 ^
630433cd ^
16db2a2f ^








630433cd ^

f8831a02 ^

f8831a02 ^
f8831a02 ^


8950915a ^
90e6596f ^
33ad0851 ^
e5998f74 ^
33ad0851 ^
c442a5ad ^
dadae338 ^
02684e8d ^
b359facb ^
a893d3b6 ^


f8831a02 ^


e307a807 ^
f8831a02 ^
a7b2a5de ^
f8831a02 ^


f2889b00 ^







f2889b00 ^




f2889b00 ^


f8831a02 ^

e307a807 ^
f8831a02 ^

e307a807 ^
f8831a02 ^
a7b2a5de ^
f8831a02 ^



a7b2a5de ^
f8831a02 ^


f8831a02 ^
a7b2a5de ^
f8831a02 ^



2af68cc8 ^
665a4d70 ^
f8831a02 ^

62c6d163 ^
e5998f74 ^
47a15082 ^
62c6d163 ^
c69f3314 ^
c357b103 ^
62c6d163 ^
8c1a6908 ^
c442a5ad ^
62c6d163 ^

62197fd5 ^

0f851e48 ^



62197fd5 ^



0f851e48 ^

62197fd5 ^




0f851e48 ^
3f4bbe9e ^
0f851e48 ^
3f4bbe9e ^
0f851e48 ^
3f4bbe9e ^
0f851e48 ^
3f4bbe9e ^
9e5e87ca ^
116e7730 ^
dc559a00 ^

62197fd5 ^




c442a5ad ^

















f8831a02 ^



a7b2a5de ^

edd2253b ^
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
386
387
388
389
390
391
392
393
394
395
396
397