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