annotate ppc/test/fac.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
mike@0 1 (* fac.p *)
mike@0 2
mike@0 3 proc fac(n: integer): integer;
mike@0 4 begin
mike@0 5 if n = 0 then
mike@0 6 return 1
mike@0 7 else
mike@0 8 return n * fac(n-1)
mike@0 9 end
mike@0 10 end;
mike@0 11
mike@0 12 var f: integer;
mike@0 13
mike@0 14 begin
mike@0 15 f := fac(10);
mike@0 16 print_num(f);
mike@0 17 newline()
mike@0 18 end.
mike@0 19
mike@0 20 (*<<
mike@0 21 3628800
mike@0 22 >>*)
mike@0 23
mike@0 24 (*[[
mike@0 25 MODULE Main 0 0
mike@0 26 IMPORT Lib 0
mike@0 27 ENDHDR
mike@0 28
mike@0 29 PROC _fac 0 0 0
mike@0 30 ! if n = 0 then
mike@0 31 LDLW 16
mike@0 32 JEQZ L1
mike@0 33 JUMP L2
mike@0 34 LABEL L1
mike@0 35 ! return 1
mike@0 36 CONST 1
mike@0 37 RETURNW
mike@0 38 LABEL L2
mike@0 39 ! return n * fac(n-1)
mike@0 40 LDLW 16
mike@0 41 LDLW 16
mike@0 42 CONST 1
mike@0 43 MINUS
mike@0 44 CONST 0
mike@0 45 GLOBAL _fac
mike@0 46 PCALLW 1
mike@0 47 TIMES
mike@0 48 RETURNW
mike@0 49 END
mike@0 50
mike@0 51 PROC MAIN 0 0 0
mike@0 52 ! f := fac(10);
mike@0 53 CONST 10
mike@0 54 CONST 0
mike@0 55 GLOBAL _fac
mike@0 56 PCALLW 1
mike@0 57 STGW _f
mike@0 58 ! print_num(f);
mike@0 59 LDGW _f
mike@0 60 CONST 0
mike@0 61 GLOBAL lib.print_num
mike@0 62 PCALL 1
mike@0 63 ! newline()
mike@0 64 CONST 0
mike@0 65 GLOBAL lib.newline
mike@0 66 PCALL 0
mike@0 67 RETURN
mike@0 68 END
mike@0 69
mike@0 70 GLOVAR _f 4
mike@0 71 ! End
mike@0 72 ]]*)