2016-01-24 20:18:52 -08:00
|
|
|
; lips example code: fibonacci numbers
|
|
|
|
; this isn't a particularily useful or realistic example,
|
|
|
|
; but it demonstrates syntax and various features in lips.
|
2016-01-18 12:56:06 -08:00
|
|
|
|
|
|
|
[max_n]: 47
|
|
|
|
|
|
|
|
fib:
|
2016-01-24 20:18:52 -08:00
|
|
|
; calculate the nth fibonacci number, caching results 1 to 47 to a table
|
|
|
|
; only valid for values of n between 0 and 47 inclusive.
|
|
|
|
; a0: n
|
|
|
|
; v0: Fn
|
|
|
|
|
|
|
|
; branch to return 0 if a0 <= 0.
|
|
|
|
; the + refers to the next + label, relative to here.
|
|
|
|
; ++ would refer to the + label after that, and so on.
|
2016-01-18 12:56:06 -08:00
|
|
|
blez a0, +
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; note that this executes even if the branch is taken,
|
|
|
|
; due to the single delay slot of this MIPS CPU.
|
|
|
|
; pseudo-instruction clears (sets to 0) the 32-bit value of a register:
|
2016-01-18 12:56:06 -08:00
|
|
|
cl v0
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; check if the input is within the bounds specified earlier.
|
|
|
|
; pseudo-instruction to branch if register > immediate:
|
2016-01-18 12:56:06 -08:00
|
|
|
bgti a0, @max_n, +
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; offset the input for use with the look-up table.
|
|
|
|
; note that this executes even if the branch is taken,
|
|
|
|
; but won't break the functionality of the routine either way.
|
|
|
|
; pseudo-instruction translates into an addiu with a negated immediate:
|
2016-01-18 12:56:06 -08:00
|
|
|
subiu t0, a0, 1
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; multiply by sizeof(word) which is 4, or 1 << 2.
|
2016-01-18 12:56:06 -08:00
|
|
|
sll t0, t0, 2
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; load the value from the look-up table.
|
|
|
|
; pseudo-instruction utilizing addressing modes:
|
2016-01-18 12:56:06 -08:00
|
|
|
lw t9, fib_cache(t0)
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; branch to return the look-up value if it's non-zero, meaning it has been cached.
|
2016-01-18 12:56:06 -08:00
|
|
|
bnez t9, +
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; once again, note that this is the delay slot of the branch instruction.
|
|
|
|
; pseudo-instruction to copy the 32-bit value of one register to another:
|
2016-01-18 12:56:06 -08:00
|
|
|
mov v0, t9
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; set up the following loop to calculate the fibonacci number.
|
|
|
|
; pseudo-instruction to load a 32-bit value into a register:
|
|
|
|
li t1, 0 ; F(0)
|
|
|
|
li t2, 1 ; F(1)
|
2016-01-18 12:56:06 -08:00
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
-: ; here's a - label referred to later.
|
|
|
|
; - labels are like + labels, except
|
|
|
|
; they look upwards in the file instead of downwards.
|
2016-01-18 12:56:06 -08:00
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; calculate the next fibonacci number.
|
2016-01-18 12:56:06 -08:00
|
|
|
addu t3, t1, t2
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; push the previous values back, part 1.
|
2016-01-18 12:56:06 -08:00
|
|
|
mov t1, t2
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; iterate to the next number.
|
2016-01-18 12:56:06 -08:00
|
|
|
subiu a0, a0, 1
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; loop if it hasn't yet reached the nth fibonacci number.
|
2016-01-18 12:56:06 -08:00
|
|
|
bnez a0, -
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; push the previous values back, part 2.
|
|
|
|
; this is put in the branch delay as a simple optimization.
|
2016-01-18 12:56:06 -08:00
|
|
|
mov t2, t3
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; loop finished, copy the result to return.
|
2016-01-18 12:56:06 -08:00
|
|
|
mov v0, t1
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; cache the result for next time.
|
|
|
|
; pseudo-instruction not unlike the previous lw:
|
2016-01-18 12:56:06 -08:00
|
|
|
sw v0, fib_cache(t0)
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; here's the + label used at the start of the routine.
|
2016-01-18 12:56:06 -08:00
|
|
|
+:
|
2016-01-24 20:18:52 -08:00
|
|
|
; return to the function that called this routine.
|
|
|
|
; when jr is given without any arguments, `jr ra` is implied.
|
2016-01-18 12:56:06 -08:00
|
|
|
jr
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; there's nothing to do in the delay slot, so don't do anything.
|
|
|
|
; this is necessary, otherwise the next instruction or data
|
|
|
|
; following the routine would be executed.
|
|
|
|
; pseudo-instruction to do nothing:
|
2016-01-18 12:56:06 -08:00
|
|
|
nop
|
|
|
|
|
2016-01-24 20:18:52 -08:00
|
|
|
; set up initial values in the look-up table.
|
2016-01-18 12:56:06 -08:00
|
|
|
fib_cache:
|
2016-01-24 20:18:52 -08:00
|
|
|
; lips doesn't yet have a way to specify "x, n times",
|
|
|
|
; so this will do for now.
|
2016-01-18 12:56:06 -08:00
|
|
|
.word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
.word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
.word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
.word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
.word 0, 0, 0, 0, 0, 0, 0
|