Skip to content

Commit 5e4b361

Browse files
committed
Add arrays
1 parent ec29261 commit 5e4b361

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

src/arrays.wat

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
(module
2+
(memory $memory 1)
3+
4+
;; void triple(int array[], int len) {
5+
;; for (int i = 0; i < len; i++)
6+
;; array[i] *= 3;
7+
;; }
8+
(func $triple (param $array i32) (param $len i32)
9+
(block $B0
10+
(br_if $B0
11+
(i32.lt_s
12+
(get_local $len)
13+
(i32.const 1)))
14+
(loop $L1
15+
(i32.store
16+
(get_local $array)
17+
(i32.mul
18+
(i32.load
19+
(get_local $array))
20+
(i32.const 3)))
21+
(set_local $array
22+
(i32.add
23+
(get_local $array)
24+
(i32.const 4)))
25+
(br_if $L1
26+
(tee_local $len
27+
(i32.add
28+
(get_local $len)
29+
(i32.const -1)))))))
30+
31+
;; void strcpy1(char dest[], const char src[]) {
32+
;; int i = 0;
33+
;; while((dest[i] = src[i]))
34+
;; i++;
35+
;; }
36+
(func $strcpy1 (param $dest i32) (param $src i32)
37+
(local $c i32)
38+
(loop $L0
39+
(i32.store8
40+
(get_local $dest)
41+
(tee_local $c
42+
(i32.load8_u
43+
(get_local $src))))
44+
(set_local $src
45+
(i32.add
46+
(get_local $src)
47+
(i32.const 1)))
48+
(set_local $dest
49+
(i32.add
50+
(get_local $dest)
51+
(i32.const 1)))
52+
(br_if $L0
53+
(get_local $c))))
54+
55+
;; char* strcpy2(char* dest, const char* src) {
56+
;; char* saved = dest;
57+
;; while ((*dest++ = *src++))
58+
;; ;
59+
;; return saved;
60+
;; }
61+
(func $strcpy2 (param $dest i32) (param $src i32) (result i32)
62+
(local $i i32) (local $c i32)
63+
(set_local $i
64+
(i32.const 0))
65+
(loop $L0
66+
(i32.store8
67+
(i32.add
68+
(get_local $dest)
69+
(get_local $i))
70+
(tee_local $c
71+
(i32.load8_u
72+
(i32.add
73+
(get_local $src)
74+
(get_local $i)))))
75+
(set_local $i
76+
(i32.add
77+
(get_local $i)
78+
(i32.const 1)))
79+
(br_if $L0
80+
(get_local $c)))
81+
(get_local $dest))
82+
)

0 commit comments

Comments
 (0)