annotate lab4/test/pascal.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 (* Pascal's triangle with a 2-dim array *)
mike@0 2
mike@0 3 const n = 10;
mike@0 4
mike@0 5 proc pascal2();
mike@0 6 var i, j: integer;
mike@0 7 var a: array n of array n+1 of integer;
mike@0 8 begin
mike@0 9 i := 0;
mike@0 10 while i < n do
mike@0 11 a[i][0] := 1; j := 1;
mike@0 12 print_num(a[i][0]);
mike@0 13 while j <= i do
mike@0 14 a[i][j] := a[i-1][j-1] + a[i-1][j];
mike@0 15 print_char(' '); print_num(a[i][j]);
mike@0 16 j := j+1
mike@0 17 end;
mike@0 18 a[i][i+1] := 0;
mike@0 19 newline();
mike@0 20 i := i+1
mike@0 21 end
mike@0 22 end;
mike@0 23
mike@0 24 begin
mike@0 25 pascal2()
mike@0 26 end.
mike@0 27
mike@0 28 (*<<
mike@0 29 1
mike@0 30 1 1
mike@0 31 1 2 1
mike@0 32 1 3 3 1
mike@0 33 1 4 6 4 1
mike@0 34 1 5 10 10 5 1
mike@0 35 1 6 15 20 15 6 1
mike@0 36 1 7 21 35 35 21 7 1
mike@0 37 1 8 28 56 70 56 28 8 1
mike@0 38 1 9 36 84 126 126 84 36 9 1
mike@0 39 >>*)
mike@0 40
mike@0 41 (*[[
mike@0 42 @ picoPascal compiler output
mike@0 43 .include "fixup.s"
mike@0 44 .global pmain
mike@0 45
mike@0 46 @ proc pascal2();
mike@0 47 .text
mike@0 48 _pascal2:
mike@0 49 mov ip, sp
mike@0 50 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 51 mov fp, sp
mike@0 52 sub sp, sp, #440
mike@0 53 @ i := 0;
mike@0 54 mov r4, #0
mike@0 55 .L2:
mike@0 56 @ while i < n do
mike@0 57 cmp r4, #10
mike@0 58 bge .L1
mike@0 59 @ a[i][0] := 1; j := 1;
mike@0 60 mov r6, #1
mike@0 61 add r0, fp, #-440
mike@0 62 mov r1, #44
mike@0 63 mul r1, r4, r1
mike@0 64 add r0, r0, r1
mike@0 65 str r6, [r0]
mike@0 66 mov r5, #1
mike@0 67 @ print_num(a[i][0]);
mike@0 68 mov r0, r6
mike@0 69 bl print_num
mike@0 70 .L5:
mike@0 71 @ while j <= i do
mike@0 72 cmp r5, r4
mike@0 73 bgt .L7
mike@0 74 @ a[i][j] := a[i-1][j-1] + a[i-1][j];
mike@0 75 add r0, fp, #-440
mike@0 76 mov r1, #44
mike@0 77 mul r1, r4, r1
mike@0 78 add r6, r0, r1
mike@0 79 lsl r7, r5, #2
mike@0 80 add r0, r6, #-44
mike@0 81 add r8, r0, r7
mike@0 82 ldr r0, [r8, #-4]
mike@0 83 ldr r1, [r8]
mike@0 84 add r0, r0, r1
mike@0 85 add r1, r6, r7
mike@0 86 str r0, [r1]
mike@0 87 @ print_char(' '); print_num(a[i][j]);
mike@0 88 mov r0, #32
mike@0 89 bl print_char
mike@0 90 add r0, fp, #-440
mike@0 91 mov r1, #44
mike@0 92 mul r1, r4, r1
mike@0 93 add r0, r0, r1
mike@0 94 lsl r1, r5, #2
mike@0 95 add r0, r0, r1
mike@0 96 ldr r0, [r0]
mike@0 97 bl print_num
mike@0 98 @ j := j+1
mike@0 99 add r5, r5, #1
mike@0 100 b .L5
mike@0 101 .L7:
mike@0 102 @ a[i][i+1] := 0;
mike@0 103 mov r0, #0
mike@0 104 add r1, fp, #-440
mike@0 105 mov r2, #44
mike@0 106 mul r2, r4, r2
mike@0 107 add r1, r1, r2
mike@0 108 lsl r2, r4, #2
mike@0 109 add r1, r1, r2
mike@0 110 str r0, [r1, #4]
mike@0 111 @ newline();
mike@0 112 bl newline
mike@0 113 @ i := i+1
mike@0 114 add r4, r4, #1
mike@0 115 b .L2
mike@0 116 .L1:
mike@0 117 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 118 .ltorg
mike@0 119
mike@0 120 pmain:
mike@0 121 mov ip, sp
mike@0 122 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 123 mov fp, sp
mike@0 124 @ pascal2()
mike@0 125 bl _pascal2
mike@0 126 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 127 .ltorg
mike@0 128
mike@0 129 @ End
mike@0 130 ]]*)