1
0
Fork 0
mirror of https://github.com/notwa/lips synced 2024-04-23 14:53:22 -07:00
lips/example.asm

101 lines
3.2 KiB
NASM
Raw Normal View History

; 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:
; calculate the nth fibonacci number, memoizing results 1 to 47 in 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, +
; 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
; 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, +
; 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
; multiply by sizeof(word) which is 4, or 1 << 2.
2016-01-18 12:56:06 -08:00
sll t0, t0, 2
; load the value from the look-up table.
; pseudo-instruction utilizing addressing modes:
lw t9, fib_memo(t0)
2016-01-18 12:56:06 -08:00
; branch to return the look-up value if it's non-zero,
; meaning it has been memoized.
2016-01-18 12:56:06 -08:00
bnez t9, +
; 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
; 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
-: ; 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
; calculate the next fibonacci number.
2016-01-18 12:56:06 -08:00
addu t3, t1, t2
; push the previous values back, part 1.
2016-01-18 12:56:06 -08:00
mov t1, t2
; iterate to the next number.
2016-01-18 12:56:06 -08:00
subiu a0, a0, 1
; loop if it hasn't yet reached the nth fibonacci number.
2016-01-18 12:56:06 -08:00
bnez a0, -
; 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
; loop finished, copy the result to return.
2016-01-18 12:56:06 -08:00
mov v0, t1
; memoize the result for next time.
; pseudo-instruction not unlike the previous lw:
sw v0, fib_memo(t0)
2016-01-18 12:56:06 -08:00
; here's the + label used at the start of the routine.
2016-01-18 12:56:06 -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
; 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
; set up initial values in the look-up table.
fib_memo:
; 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