annotate ppc/test/list.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 type ptr = pointer to rec;
mike@0 2 rec = record
mike@0 3 data: integer;
mike@0 4 next: ptr;
mike@0 5 end;
mike@0 6
mike@0 7 proc sum(p: ptr): integer;
mike@0 8 var q: ptr; s: integer;
mike@0 9 begin
mike@0 10 q := p; s := 0;
mike@0 11 while q <> nil do
mike@0 12 s := s + q^.data;
mike@0 13 q := q^.next
mike@0 14 end;
mike@0 15 return s
mike@0 16 end;
mike@0 17
mike@0 18 proc main();
mike@0 19 const input = "3141592650";
mike@0 20 var i: integer; p, q: ptr;
mike@0 21 begin
mike@0 22 i := 0;
mike@0 23 while input[i] <> '0' do i := i+1 end;
mike@0 24
mike@0 25 p := nil;
mike@0 26 while i > 0 do
mike@0 27 i := i-1;
mike@0 28 q := p;
mike@0 29 new(p);
mike@0 30 p^.data := ord(input[i]) - ord('0');
mike@0 31 p^.next := q
mike@0 32 end;
mike@0 33
mike@0 34 print_num(sum(p)); newline()
mike@0 35 end;
mike@0 36
mike@0 37 begin
mike@0 38 main()
mike@0 39 end.
mike@0 40
mike@0 41 (*<<
mike@0 42 36
mike@0 43 >>*)
mike@0 44
mike@0 45 (*[[
mike@0 46 MODULE Main 0 0
mike@0 47 IMPORT Lib 0
mike@0 48 ENDHDR
mike@0 49
mike@0 50 PROC _sum 8 0 0
mike@0 51 ! q := p; s := 0;
mike@0 52 LDLW 16
mike@0 53 STLW -4
mike@0 54 CONST 0
mike@0 55 STLW -8
mike@0 56 ! while q <> nil do
mike@0 57 JUMP L3
mike@0 58 LABEL L2
mike@0 59 ! s := s + q^.data;
mike@0 60 LDLW -8
mike@0 61 LDLW -4
mike@0 62 LOADW
mike@0 63 PLUS
mike@0 64 STLW -8
mike@0 65 ! q := q^.next
mike@0 66 LDLW -4
mike@0 67 LDNW 4
mike@0 68 STLW -4
mike@0 69 LABEL L3
mike@0 70 LDLW -4
mike@0 71 JNEQZ L2
mike@0 72 ! return s
mike@0 73 LDLW -8
mike@0 74 RETURNW
mike@0 75 END
mike@0 76
mike@0 77 PROC _main 12 0 0
mike@0 78 ! i := 0;
mike@0 79 CONST 0
mike@0 80 STLW -4
mike@0 81 ! while input[i] <> '0' do i := i+1 end;
mike@0 82 JUMP L6
mike@0 83 LABEL L5
mike@0 84 LDLW -4
mike@0 85 CONST 1
mike@0 86 PLUS
mike@0 87 STLW -4
mike@0 88 LABEL L6
mike@0 89 GLOBAL g1
mike@0 90 LDLW -4
mike@0 91 LDIC
mike@0 92 CONST 48
mike@0 93 JNEQ L5
mike@0 94 ! p := nil;
mike@0 95 CONST 0
mike@0 96 STLW -8
mike@0 97 ! while i > 0 do
mike@0 98 JUMP L9
mike@0 99 LABEL L8
mike@0 100 ! i := i-1;
mike@0 101 LDLW -4
mike@0 102 CONST 1
mike@0 103 MINUS
mike@0 104 STLW -4
mike@0 105 ! q := p;
mike@0 106 LDLW -8
mike@0 107 STLW -12
mike@0 108 ! new(p);
mike@0 109 CONST 8
mike@0 110 CONST 0
mike@0 111 GLOBAL lib.new
mike@0 112 PCALLW 1
mike@0 113 STLW -8
mike@0 114 ! p^.data := ord(input[i]) - ord('0');
mike@0 115 GLOBAL g1
mike@0 116 LDLW -4
mike@0 117 LDIC
mike@0 118 CONST 48
mike@0 119 MINUS
mike@0 120 LDLW -8
mike@0 121 STOREW
mike@0 122 ! p^.next := q
mike@0 123 LDLW -12
mike@0 124 LDLW -8
mike@0 125 STNW 4
mike@0 126 LABEL L9
mike@0 127 LDLW -4
mike@0 128 JGTZ L8
mike@0 129 ! print_num(sum(p)); newline()
mike@0 130 LDLW -8
mike@0 131 CONST 0
mike@0 132 GLOBAL _sum
mike@0 133 PCALLW 1
mike@0 134 CONST 0
mike@0 135 GLOBAL lib.print_num
mike@0 136 PCALL 1
mike@0 137 CONST 0
mike@0 138 GLOBAL lib.newline
mike@0 139 PCALL 0
mike@0 140 RETURN
mike@0 141 END
mike@0 142
mike@0 143 PROC MAIN 0 0 0
mike@0 144 ! main()
mike@0 145 CONST 0
mike@0 146 GLOBAL _main
mike@0 147 PCALL 0
mike@0 148 RETURN
mike@0 149 END
mike@0 150
mike@0 151 ! String "3141592650"
mike@0 152 DEFINE g1
mike@0 153 STRING 3331343135393236353000
mike@0 154
mike@0 155 ! End
mike@0 156 ]]*)