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