annotate ppc/test/flip.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 proc flip(x: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 proc flop(y: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 if y = 0 then return 1 else return flip(y-1) + x end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 if x = 0 then return 1 else return 2 * flop(x-1) end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 print_num(flip(5));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 (* flip(5) = 2 * flop(4) = 2 * (flip(3) + 5)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 = 4 * flop(2) + 10 = 4 * (flip(1) + 3) + 10
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 = 8 * flop(0) + 22 = 30 *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 30
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 MODULE Main 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 IMPORT Lib 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 ENDHDR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 PROC _flip 0 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 ! if x = 0 then return 1 else return 2 * flop(x-1) end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 JEQZ L1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 JUMP L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 LABEL L1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 LABEL L2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 CONST 2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 MINUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 LOCAL 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 GLOBAL _flop
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 PCALLW 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 TIMES
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 PROC _flop 0 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 ! if y = 0 then return 1 else return flip(y-1) + x end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 JEQZ L4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 JUMP L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 LABEL L4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 LABEL L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 LDLW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 CONST 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 MINUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 GLOBAL _flip
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 PCALLW 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 LDLW 12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 LDNW 16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 PLUS
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 RETURNW
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 PROC MAIN 0 0 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 ! print_num(flip(5));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 CONST 5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 GLOBAL _flip
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 PCALLW 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 GLOBAL lib.print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 PCALL 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 ! newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 CONST 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 GLOBAL lib.newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 PCALL 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 RETURN
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 END
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 ! End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 ]]*)