annotate lab4/test/swap.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 (* Procedure to swap elements of array *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 const n = 10;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 var a: array n of integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 proc swap(i, j: integer);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 var t: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 t := a[i];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 a[i] := a[j];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 a[j] := t
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 proc main();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 var i: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 for i := 0 to n-1 do a[i] := i end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 swap(3, 6);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 for i := 0 to n-1 do print_num(a[i]) end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 main()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 0126453789
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 >>*)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 (*[[
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 @ picoPascal compiler output
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 .include "fixup.s"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 .global pmain
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 @ proc swap(i, j: integer);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 .text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 _swap:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 @ t := a[i];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 set r5, _a
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 ldr r0, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 lsl r0, r0, #2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 add r6, r5, r0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 ldr r4, [r6]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 @ a[i] := a[j];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 ldr r0, [fp, #44]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 lsl r0, r0, #2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 add r5, r5, r0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 ldr r0, [r5]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 str r0, [r6]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 @ a[j] := t
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 str r4, [r5]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 @ proc main();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 _main:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 @ for i := 0 to n-1 do a[i] := i end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 mov r4, #0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 mov r5, #9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 .L3:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 cmp r4, r5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 bgt .L4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 set r0, _a
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 lsl r1, r4, #2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 add r0, r0, r1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 str r4, [r0]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 add r4, r4, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 b .L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 .L4:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 @ swap(3, 6);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 mov r1, #6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 mov r0, #3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 bl _swap
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 @ for i := 0 to n-1 do print_num(a[i]) end;
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 mov r6, #9
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 .L5:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 cmp r4, r6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 bgt .L6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 set r0, _a
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 lsl r1, r4, #2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 add r0, r0, r1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 ldr r0, [r0]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 add r4, r4, #1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
95 b .L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96 .L6:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97 @ newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
99 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
100 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
101
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
102 pmain:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
103 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
104 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
105 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
106 @ main()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
107 bl _main
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
108 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
109 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
110
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
111 .comm _a, 40, 4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
112 @ End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
113 ]]*)