annotate lab4/test/foo.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 (* Euclid's algorithm as a subroutine *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 proc gcd(u, v: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 var x, y: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 x := u; y := v;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 while x <> y do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 if x < y then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 y := y - x
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 else
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 x := x - y
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 end
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 return x
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 var z: integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 z := gcd(3*37, 5*37);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 print_string("The final answer is calculated as ");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 print_num(z); 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 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 The final answer is calculated as 37
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 >>*)
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 @ picoPascal compiler output
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 .include "fixup.s"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 .global pmain
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 @ proc gcd(u, v: integer): integer;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 .text
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 _gcd:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 stmfd sp!, {r0-r1}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 @ x := u; y := v;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 ldr r4, [fp, #40]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 ldr r5, [fp, #44]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 .L3:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 @ while x <> y do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 cmp r4, r5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 beq .L5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 @ if x < y then
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 cmp r4, r5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 bge .L7
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 @ y := y - x
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 sub r5, r5, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 b .L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 .L7:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 @ x := x - y
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 sub r4, r4, r5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 b .L3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 .L5:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 @ return x
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 mov r0, r4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 pmain:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 mov ip, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 stmfd sp!, {r4-r10, fp, ip, lr}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 mov fp, sp
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 @ z := gcd(3*37, 5*37);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 mov r1, #185
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 mov r0, #111
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 bl _gcd
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 set r1, _z
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 str r0, [r1]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 @ print_string("The final answer is calculated as ");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 mov r1, #34
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 set r0, g1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 bl print_string
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 @ print_num(z); newline()
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 set r0, _z
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 ldr r0, [r0]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 bl print_num
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 bl newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 ldmfd fp, {r4-r10, fp, sp, pc}
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 .ltorg
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 .comm _z, 4, 4
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 .data
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 g1:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 .byte 84, 104, 101, 32, 102, 105, 110, 97, 108, 32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 .byte 97, 110, 115, 119, 101, 114, 32, 105, 115, 32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 .byte 99, 97, 108, 99, 117, 108, 97, 116, 101, 100
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 .byte 32, 97, 115, 32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 .byte 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 @ End
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 ]]*)