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