Tuesday, March 24, 2020

Fibonacci sequence in MIPS

This is a sample MIPS assembler code to calculate the fibonacci sequence of the first 20 numbers, store them in an array of words and, finally, print them. The code is fully commented.

Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS, the MIPS assembler editor & simulator. Assemble, Run.

# ---------------------------------------------------------------- #
# The Fibonacci sequence is the sequence of numbers given
# by an+2 = an+1 + an ie 1, 1, 2, 3, 5, 8, 13, 21,...

# ---------------------------------------------------------------- #

.data

# .... array of words to hold the first 20 Fibonacci numbers ..... #

Array: .word 0,0,0,0,0,0,0,0,0,0
       .word 0,0,0,0,0,0,0,0,0,0

szComma:  .asciiz ", "

# ---------------------------------------------------------------- #

.text
main:

    ori $t6, $zero, 1       # set t6 to 1

    la $t0, Array           # $t0 holds the memory address
                            # of the 1st array element
    
    sw $t6, ($t0)           # set the 1st term to 1
    sw $t6, 4($t0)          # set the 2nd term to 1

    addiu $t6, $t0, 80      # $t6 now holds the address after
                            # the 20th array element!

    addiu $t0, $t0, 8       # $t0 now holds the address of
                            # the 3rd array element

# ................................................................ #

loop:
    addi  $t4, $t0, -4      # $t4 holds the address of the 
                            # last array element SO FAR

    addi  $t3, $t0, -8      # $t4 holds the address of the array
                            # element before last element SO FAR



    lw $t2, ($t4)           # get the last element

    lw $t1, ($t3)           # get the element before the
                            # last element


    add $t5, $t1, $t2       # Add the two last elements together...

    sw $t5, ($t0)           # store the result AFTER the currently
                            # last element


    addi $t0, $t0, 4        # move to next element of the Array

    slt $at, $t0, $t6       # Remember: $t6 holds the address after
                            # the 30th array element!

    bne $at, $0, loop       # If not past the end of Array, repeat

# ................................................................ #

# print the first 20 Fibonacci numbers stored in the array         #

    la $t0, Array           # $t0 holds the memory address
                            # of the 1st array element
    move $t1, $zero         # $t1 = 0 (counter)

next:
    lw $a0, ($t0)           # load 1 element in $a0
    li $v0, 1               # syscall to print integer
    syscall                 # print it!

    
    la $a0, szComma         # load address of ", "
    li $v0, 4               # syscall to print string
    syscall                 # print it!

    addiu $t0, $t0, 4       # $t0 = address of next array element

    addiu $t1, $t1, 1       # counter++
    slti $at, $t1, 20
    bne $at, $zero, next    # If not past the end of Array, repeat

# ---------------------------------------------------------------- #

Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment