annotate lab4/test/list.p @ 1:b5139af1a420 tip basis

Fixed permissions on compile scripts
author Mike Spivey <mike@cs.ox.ac.uk>
date Fri, 13 Oct 2017 17:27:58 +0100
parents bfdcc3820b32
children
rev   line source
mike@0 1 (* Linked lists *)
mike@0 2
mike@0 3 type ptr = pointer to rec;
mike@0 4 rec = record
mike@0 5 data: integer;
mike@0 6 next: ptr;
mike@0 7 end;
mike@0 8
mike@0 9 proc sum(p: ptr): integer;
mike@0 10 var q: ptr; s: integer;
mike@0 11 begin
mike@0 12 q := p; s := 0;
mike@0 13 while q <> nil do
mike@0 14 s := s + q^.data;
mike@0 15 q := q^.next
mike@0 16 end;
mike@0 17 return s
mike@0 18 end;
mike@0 19
mike@0 20 proc main();
mike@0 21 const input = "3141592650";
mike@0 22 var i: integer; p, q: ptr;
mike@0 23 begin
mike@0 24 i := 0;
mike@0 25 while input[i] <> '0' do i := i+1 end;
mike@0 26
mike@0 27 p := nil;
mike@0 28 while i > 0 do
mike@0 29 i := i-1;
mike@0 30 q := p;
mike@0 31 new(p);
mike@0 32 p^.data := ord(input[i]) - ord('0');
mike@0 33 p^.next := q
mike@0 34 end;
mike@0 35
mike@0 36 print_num(sum(p)); newline()
mike@0 37 end;
mike@0 38
mike@0 39 begin
mike@0 40 main()
mike@0 41 end.
mike@0 42
mike@0 43 (*<<
mike@0 44 36
mike@0 45 >>*)
mike@0 46
mike@0 47 (*[[
mike@0 48 @ picoPascal compiler output
mike@0 49 .include "fixup.s"
mike@0 50 .global pmain
mike@0 51
mike@0 52 @ proc sum(p: ptr): integer;
mike@0 53 .text
mike@0 54 _sum:
mike@0 55 mov ip, sp
mike@0 56 stmfd sp!, {r0-r1}
mike@0 57 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 58 mov fp, sp
mike@0 59 @ q := p; s := 0;
mike@0 60 ldr r4, [fp, #40]
mike@0 61 mov r5, #0
mike@0 62 .L3:
mike@0 63 @ while q <> nil do
mike@0 64 cmp r4, #0
mike@0 65 beq .L5
mike@0 66 @ s := s + q^.data;
mike@0 67 ldr r0, [r4]
mike@0 68 add r5, r5, r0
mike@0 69 @ q := q^.next
mike@0 70 ldr r4, [r4, #4]
mike@0 71 b .L3
mike@0 72 .L5:
mike@0 73 @ return s
mike@0 74 mov r0, r5
mike@0 75 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 76 .ltorg
mike@0 77
mike@0 78 @ proc main();
mike@0 79 _main:
mike@0 80 mov ip, sp
mike@0 81 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 82 mov fp, sp
mike@0 83 @ i := 0;
mike@0 84 mov r4, #0
mike@0 85 .L7:
mike@0 86 @ while input[i] <> '0' do i := i+1 end;
mike@0 87 set r0, g1
mike@0 88 add r0, r0, r4
mike@0 89 ldrb r0, [r0]
mike@0 90 cmp r0, #48
mike@0 91 beq .L9
mike@0 92 add r4, r4, #1
mike@0 93 b .L7
mike@0 94 .L9:
mike@0 95 @ p := nil;
mike@0 96 mov r5, #0
mike@0 97 .L10:
mike@0 98 @ while i > 0 do
mike@0 99 cmp r4, #0
mike@0 100 ble .L12
mike@0 101 @ i := i-1;
mike@0 102 sub r4, r4, #1
mike@0 103 @ q := p;
mike@0 104 mov r6, r5
mike@0 105 @ new(p);
mike@0 106 mov r0, #8
mike@0 107 bl new
mike@0 108 mov r5, r0
mike@0 109 @ p^.data := ord(input[i]) - ord('0');
mike@0 110 set r0, g1
mike@0 111 add r0, r0, r4
mike@0 112 ldrb r0, [r0]
mike@0 113 sub r0, r0, #48
mike@0 114 str r0, [r5]
mike@0 115 @ p^.next := q
mike@0 116 str r6, [r5, #4]
mike@0 117 b .L10
mike@0 118 .L12:
mike@0 119 @ print_num(sum(p)); newline()
mike@0 120 mov r0, r5
mike@0 121 bl _sum
mike@0 122 bl print_num
mike@0 123 bl newline
mike@0 124 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 125 .ltorg
mike@0 126
mike@0 127 pmain:
mike@0 128 mov ip, sp
mike@0 129 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 130 mov fp, sp
mike@0 131 @ main()
mike@0 132 bl _main
mike@0 133 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 134 .ltorg
mike@0 135
mike@0 136 .data
mike@0 137 g1:
mike@0 138 .byte 51, 49, 52, 49, 53, 57, 50, 54, 53, 48
mike@0 139 .byte 0
mike@0 140 @ End
mike@0 141 ]]*)