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