# spin.s -- Show a spinner on the screen, using the characters # | / - and \ to give an illusion of a rotating spinner. # # This program consists of a main procedure and a sleep procedure. # The program also uses a nested loop, although the fact that the # two loops are nested is not obvious because they are in different # procedures. # # outer loop in main -- decides how long to spin # inner loop in sleep -- keep CPU busy by having it count before # rotating the spinner to its next position # # At the beginning of the program we initialize the backspace and # left strings properly. For some reason, SPIM does not allow us # to initialize a string in the data segment to "\010" to represent # a backspace or "\\" for a backslash. # # Purpose of registers: # $t0 -- hold an ASCII value to be stored in a string # $t1 -- hold the base address of a string # $s0 -- outer loop counter (number of spin rotations) # $s1 -- inner loop counter (how much to count) # $v0 -- used to specify which I/O function to perform with syscall # $a0 -- used to hold base address of string when using syscall .data blanks: .asciiz "\n\n\n\n\n\t\t\t\t" vertical: .asciiz "|" backspace: .asciiz "0" # we'll change this soon right: .asciiz "/" left: .asciiz "0" # we'll change this soon horizontal: .asciiz "-" .text main: # Put backspace character in the backspace string. # note -- The ASCII code for the backspace character is 8. li $t0, 8 la $t1, backspace sb $t0, 0($t1) # Put backslash character in the left string. # note -- The ASCII code for backslash is 92. li $t0, 92 la $t1, left sb $t0, 0($t1) # Begin by printing some blank lines, and some tabs so we # can more easily see the spinner. li $v0, 4 la $a0, blanks syscall li $s0, 50 # set the number of iterations loop: jal sleep li $v0, 4 la $a0, vertical # print a | character syscall jal sleep li $v0, 4 la $a0, right # print a / character syscall jal sleep li $v0, 4 la $a0, horizontal # print a - character syscall jal sleep li $v0, 4 la $a0, left # print a \ character syscall addi $s0, $s0, -1 # decrement loop counter bnez $s0, loop # quit loop when loop counter reaches 0 li $v0, 10 syscall #-------------------------------------------------------------------------- # sleep -- A procedure that causes the machine to count down from a large # number down to zero. Then it prints a backspace character to erase the # previous position of the spinner. sleep: li $s1, 110000 sleep_loop: addi $s1, $s1, -1 bnez $s1, sleep_loop li $v0, 4 la $a0, backspace syscall jr $ra