annotate lab4/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
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* Linked lists *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 type ptr = pointer to rec;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 rec = record
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 data: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 next: ptr;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 proc sum(p: ptr): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 var q: ptr; s: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 q := p; s := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 while q <> nil do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 s := s + q^.data;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 q := q^.next
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 return s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 proc main();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 const input = "3141592650";
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 var i: integer; p, q: ptr;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 i := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 while input[i] <> '0' do i := i+1 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 p := nil;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 while i > 0 do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 i := i-1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 q := p;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 new(p);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 p^.data := ord(input[i]) - ord('0');
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 p^.next := q
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 print_num(sum(p)); newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 main()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 36
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 @ picoPascal compiler output
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 .include "fixup.s"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 .global pmain
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 @ proc sum(p: ptr): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 .text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 _sum:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 @ q := p; s := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 ldr r4, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 mov r5, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 .L3:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 @ while q <> nil do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 cmp r4, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 beq .L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 @ s := s + q^.data;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 ldr r0, [r4]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 add r5, r5, r0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 @ q := q^.next
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 ldr r4, [r4, #4]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 b .L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 .L5:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 @ return s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 mov r0, r5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 @ proc main();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 _main:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 @ i := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 mov r4, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 .L7:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 @ while input[i] <> '0' do i := i+1 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 set r0, g1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 add r0, r0, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 ldrb r0, [r0]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 cmp r0, #48
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 beq .L9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 add r4, r4, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 b .L7
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 .L9:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
95 @ p := nil;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96 mov r5, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97 .L10:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98 @ while i > 0 do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
99 cmp r4, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
100 ble .L12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
101 @ i := i-1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
102 sub r4, r4, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
103 @ q := p;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
104 mov r6, r5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
105 @ new(p);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
106 mov r0, #8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
107 bl new
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
108 mov r5, r0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
109 @ p^.data := ord(input[i]) - ord('0');
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
110 set r0, g1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
111 add r0, r0, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
112 ldrb r0, [r0]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
113 sub r0, r0, #48
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
114 str r0, [r5]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
115 @ p^.next := q
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
116 str r6, [r5, #4]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
117 b .L10
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
118 .L12:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
119 @ print_num(sum(p)); newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
120 mov r0, r5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
121 bl _sum
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
122 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
123 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
124 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
125 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
126
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
127 pmain:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
128 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
129 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
130 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
131 @ main()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
132 bl _main
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
133 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
134 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
135
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
136 .data
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
137 g1:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
138 .byte 51, 49, 52, 49, 53, 57, 50, 54, 53, 48
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
139 .byte 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
140 @ End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
141 ]]*)