Computer Laboratory

Bluepsec Examples

Example Program


This package contains a simple assembler program that calculates Fibonacci numbers in an iterative manner.

The code

import CyanTypes::*;
import CyanAsm::*;
import List::*;

// function returning a program as a list of function calls which code the instructions
function List#(AsmLineT) fib_program(function ImmT findaddr(String label)) =
   cons(asm("",       OpORI, Rg1, RgZero, RgZero, 13), // r1 = 13
   cons(asm("",       OpJSR, RgZero, RgZero, RgZero, findaddr("fib")), // call fib function
   cons(asm("",       OpSTORE, RgZero, RgZero, Rg1, unpack(extend(pack(Address_magic_output)))), // output result
   cons(asm("end",    OpBEQ, RgZero, RgZero, RgZero, findaddr("end")), // loop for ever to stop
   // start of fub function - paramenter passed in r1
   cons(asm("fib",    OpORI, Rg2, RgZero, RgZero, 2), // r2 = 2
   cons(asm("",       OpBLT, RgZero, Rg1, Rg2, findaddr("finish")), // if(r1<r2) goto finish
   cons(asm("",       OpOR,  Rg4, Rg1, RgZero, 0), // r4 = r1  (a)
   cons(asm("",       OpORI, Rg1, RgZero, RgZero, 1), // r1 = 1  (a)
   cons(asm("",       OpORI, Rg2, RgZero, RgZero, 0), // r2 = 0  (b)
   cons(asm("loop",   OpADD, Rg1, Rg1, Rg2, 0),  // r1=r1+r2, i.e. a=a+b
   cons(asm("",       OpSUB, Rg2, Rg1, Rg2, 0),  // r2=r1-r2, i.e. b=a-b
   cons(asm("",       OpADDI,Rg4, Rg4, RgZero, -1),  // r4=r4-1, i.e. dec loop counter
   cons(asm("",       OpBNE, RgZero, RgZero, Rg4, findaddr("loop")), // if(r4!=0) goto loop
   cons(asm("finish", OpJR,  RgZero, Rgra, RgZero, 0), // jump ra (return from subroutine)
   tagged Nil)))))))))))))); // end of list

Link to the FibProg.bsv source