comparison lab4/test/foo.p @ 0:bfdcc3820b32

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