fibmmem: str@hi/lo missing
[calu.git] / progs / fibmmem.s
1 #include "dt_inc.s"
2
3 .data
4 .org 0x0
5 list:
6         .fill 0x1 ;1. element = 1 ;0x10
7         .fill 0x1 ;2. element = 1 ;0x14
8         .fill 0x10, 0x0 ;nullen..
9 str:
10         .ascii "hier das ergebnis: "
11 .text
12 .org 0x0
13 start:
14         br+ main
15         br+ main
16         ret
17 main:
18         call+ u_init
19         call+ u_recv_byte
20         call u_send_newline
21
22         ldi r1, 9;
23         ldi r10, list@lo ; global pointer
24         ldih r10, list@hi
25         call+ fibcall;
26
27         push r0
28         call u_init ; weil r10 ueberschrieben wird
29         ldis r1, str@lo
30         ldih r1, str@hi
31         ldis r2, 19
32         call u_send_string
33         
34         pop r1
35         call+ u_send_uint
36         call u_send_newline
37
38 hang:
39         br hang
40
41         ; fib(n) {
42         ;   if (list[n] > 0) {
43         ;       return list[n]
44         ;   }
45         ;   a = fib(n-1)
46         ;   list[n] = a + list[n-2]
47         ;   return list[n]
48         ; }
49 fibcall:
50         ;update counter for aligned access
51         lls r1, r1, 2 ; *4
52         ;calculate adress of top element
53         add r2, r10, r1
54 fibmem:
55         ;load top element
56         ldw r0, 0(r2)
57         ;compare if set
58         cmpi r0, 0
59         ;return if set
60         retnz-
61         ;decrement adress for next lopp
62         subi r2, r2, 4
63         ;iterative call for n-1 element
64         call+ fibmem
65         ;load n-2 element
66         ldw r3, 0-4(r2)
67         ;add n-1 and n-2 element
68         add r0, r3, r0
69         ;increment address for n element
70         ;is needed because after return
71         ;we need r2 to be set to the address
72         ;of element n
73         addi r2, r2, 4
74         ;store fib n
75         stw r0, 0(r2)
76         ret+