annotate ppc/test/flip.p @ 1:b5139af1a420 tip basis

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