-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzbuzz.asm
72 lines (67 loc) · 1.63 KB
/
fizzbuzz.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
.data
Fizz: .asciiz "Fizz"
Buzz: .asciiz "Buzz"
Newline: .asciiz "\n"
.text
main:
# Store 3 in register s0
li $s0, 3
# Store 5 in register s1
li $s1, 5
loop:
# Exit when t0 > 99 at this point, which will be when t0 > 100 at end of loop
bgt $t0,99,exit
# Print newline every iteration but the first
bne $t0, $zero, newline
# After printing a newline, return here
newline_return:
# Store 0 in register a0
li $a0, 0
# Add 1 to t0
addi $t0,$t0,1
# Divide t0 by s0
div $t0, $s0
# Store the remainder in t1
mfhi $t1
# Divide t0 by s1
div $t0, $s1
# Store the remainder in t2
mfhi $t2
# If t0 % 3 == 0, print Fizz
beq $t1, $zero, print_fizz
# After printing Fizz, return here
fizz_return:
# If t0 % 5 == 0, print Buzz
beq $t2, $zero, print_buzz
# Printing Fizz and/or Buzz has the side-effect of storing something besides 0 in register a0; if zero is still in a0, nothing but a newline was printed yet
beq $a0, $zero, print_number
# Loop
j loop
# Print Fizz, then return to the previous point in the loop
print_fizz:
la $a0, Fizz
li $v0, 4
syscall
j fizz_return
# Print Buzz, then loop
print_buzz:
la $a0, Buzz
li $v0, 4
syscall
j loop
# Print the number in t0, then loop
print_number:
move $a0, $t0
li $v0, 1
syscall
j loop
# Print a newline, then return to the previous point in the loop
newline:
la $a0, Newline
li $v0, 4
syscall
j newline_return
# Exit the program
exit:
li $v0, 10
syscall