MIPS FizzBuzz


I know that this may be taking it a little far, but here is a MIPS version of FizzBuzz. You will require SPIM to run this.

I apologize in advance for my lack of comments.

Code:

        .data
space:          .asciiz         " "
fizz:           .asciiz         "Fizz"
buzz:           .asciiz         "Buzz"
fizzbuzz:       .asciiz         "FizzBuzz"
i:              .word 0
        .text
main:
li      $v0,    4
la      $a0,    space
syscall
lw      $t0,    i
beq     $t0,    100,    endl
add     $t0,    1
sw      $t0,    i
li      $t1,    15
div     $t0,    $t1
mfhi    $t0
bne     $t0,    0,      next1
li      $v0,    4
la      $a0,    fizzbuzz
syscall
j       main
next1:
lw      $t0,    i
li      $t1,    3
div     $t0,    $t1
mfhi    $t0
bne     $t0,    0,      next2
li      $v0,    4
la      $a0,    fizz
syscall
j       main
next2:
lw      $t0,    i
li      $t1,    5
div     $t0,    $t1
mfhi    $t0
bne     $t0,    0,      next3
li      $v0,    4
la      $a0,    buzz
syscall
j       main
next3:
li      $v0,    1
lw      $a0,    i
syscall
j       main
endl:
li      $v0,    10
syscall

Output:

# spim fizbuzz.asm
SPIM Version 7.3. of August 28, 2006
Copyright 1990-2004 by James R. Larus (larus@cs.wisc.edu).
All Rights Reserved.
See the file README for a full copyright notice.
Loaded: /usr/lib/spim/exceptions.s
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz

Leave a Reply