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