annotate ppc/test/list.p @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 type ptr = pointer to rec;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 rec = record
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 data: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 next: ptr;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 proc sum(p: ptr): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 var q: ptr; s: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 q := p; s := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 while q <> nil do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 s := s + q^.data;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 q := q^.next
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 return s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 proc main();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 const input = "3141592650";
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 var i: integer; p, q: ptr;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 i := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 while input[i] <> '0' do i := i+1 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 p := nil;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 while i > 0 do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 i := i-1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 q := p;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 new(p);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 p^.data := ord(input[i]) - ord('0');
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 p^.next := q
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 print_num(sum(p)); newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 main()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 36
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 MODULE Main 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 IMPORT Lib 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 ENDHDR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 PROC _sum 8 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 ! q := p; s := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 STLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 STLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 ! while q <> nil do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 JUMP L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 LABEL L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 ! s := s + q^.data;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 LOADW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 PLUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 STLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 ! q := q^.next
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 LDNW 4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 STLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 LABEL L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 JNEQZ L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 ! return s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 PROC _main 12 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 ! i := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 STLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 ! while input[i] <> '0' do i := i+1 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 JUMP L6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 LABEL L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 PLUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 STLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 LABEL L6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 GLOBAL g1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 LDIC
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 CONST 48
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 JNEQ L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 ! p := nil;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
95 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96 STLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97 ! while i > 0 do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98 JUMP L9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
99 LABEL L8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
100 ! i := i-1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
101 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
102 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
103 MINUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
104 STLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
105 ! q := p;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
106 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
107 STLW -12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
108 ! new(p);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
109 CONST 8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
110 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
111 GLOBAL lib.new
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
112 PCALLW 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
113 STLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
114 ! p^.data := ord(input[i]) - ord('0');
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
115 GLOBAL g1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
116 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
117 LDIC
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
118 CONST 48
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
119 MINUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
120 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
121 STOREW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
122 ! p^.next := q
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
123 LDLW -12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
124 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
125 STNW 4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
126 LABEL L9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
127 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
128 JGTZ L8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
129 ! print_num(sum(p)); newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
130 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
131 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
132 GLOBAL _sum
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
133 PCALLW 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
134 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
135 GLOBAL lib.print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
136 PCALL 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
137 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
138 GLOBAL lib.newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
139 PCALL 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
140 RETURN
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
141 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
142
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
143 PROC MAIN 0 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
144 ! main()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
145 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
146 GLOBAL _main
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
147 PCALL 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
148 RETURN
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
149 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
150
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
151 ! String "3141592650"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
152 DEFINE g1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
153 STRING 3331343135393236353000
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
154
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
155 ! End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
156 ]]*)