annotate ppc/test/foo.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 (* foo.p *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 proc gcd(u, v: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 var x, y: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 x := u; y := v;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 while x <> y do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 if x < y then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 y := y - x
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 else
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 x := x - y
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 return x
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 var z: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 z := gcd(3*37, 5*37);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 print_string("The final answer is calculated as ");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 print_num(z); newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 The final answer is calculated as 37
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 MODULE Main 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 IMPORT Lib 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 ENDHDR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 PROC _gcd 8 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 ! x := u; y := v;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 STLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 LDLW 20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 STLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 ! while x <> y do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 JUMP L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 LABEL L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 ! if x < y then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 JGEQ L6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 ! y := y - x
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 MINUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 STLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 JUMP L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 LABEL L6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 ! x := x - y
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 MINUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 STLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 LABEL L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 LDLW -8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 JNEQ L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 ! return x
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 LDLW -4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 PROC MAIN 0 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 ! z := gcd(3*37, 5*37);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 CONST 185
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 CONST 111
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 GLOBAL _gcd
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 PCALLW 2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 STGW _z
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 ! print_string("The final answer is calculated as ");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 CONST 34
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 GLOBAL g1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 GLOBAL lib.print_string
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 PCALL 2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 ! print_num(z); newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 LDGW _z
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 GLOBAL lib.print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 PCALL 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 GLOBAL lib.newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 PCALL 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 RETURN
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 GLOVAR _z 4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 ! String "The final answer is calculated as "
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 DEFINE g1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
95 STRING 5468652066696E616C20616E737765722069732063616C63756C617465642061
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96 STRING 732000
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98 ! End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
99 ]]*)