~charles/awk-riscv

ref: fa3c47fde6c5a46743f34bc972ece337a8974615 awk-riscv/fibo.asm -rw-r--r-- 925 bytes
fa3c47fdCharles Daniels assemble lw and sw 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# This program computes the value of the nth number in the fibonacci sequence,
# where n is read from memory address 0x10000. The result is written to memory
# address 0x10004.

li x5 0x10000
lw x10 0(x5)  # n <- mem[0x10000]
li x11 1      # i <- 1
li x12 0      # F[0] <- 0
li x13 1      # F[1] <- 1
li x14 0      # next <- 0

# x12 and x13 will be used to track the previous two elements of the fibonacci
# sequence as we advance through the loop

bgt x10 x13 input_is_other      # if n > 1

input_is_zero:
csrw x0 io2
csrw x0 io3
j end

input_is_one:
csrw x13 io2
csrw x13 io3
j end

input_is_other:

fibo:
add x14 x12 x13         # next <- F[i] + F[i-1]
mv x12 x13              # F[i-1] <- F[i]
mv x13 x14              # F[i] <- next
addi x11 x11 1          # i++
csrw x13 io2            # HEX <- F[i]
csrw x11 io3            # LEDs <- i
blt x11 x10 fibo        # while(i < n)

end:
sw x13 4(x5)   # mem[0x10004] <- F[n]