# delay.s - Simple program to illustrate calling a function # This program consists of a main program and a sleep function. # Every time we call the sleep function, the machine will count some "sheep" # before returning. The more sheep to count, the longer it is asleep. .data dot: .asciiz "." nl: .asciiz "\n" # Register allocation # $a0 - Argument passed to function, how high to count up to. # $s0 - Number of outer loop iterations, number of lines of output. # $s1 - Number of inner loop iterations, number of dots to print per line. # $s2 - Outer loop counter, i.e. which line we are on. # This number will also influence how long we sleep. # $s3 - Inner loop counter, i.e. which dot we are printing on the line. # $s4 - Amount of delay .text main: li $s0, 5 # Arrange for 5 outer iterations. li $s1, 10 # Arrange for 10 inner iterations. li $s2, 1 outer: li $s3, 1 mul $s4, $s2, 50000 # Determine delay amount. inner: li $v0, 4 # Print one period. la $a0, dot syscall move $a0, $s4 # Go to sleep jal sleep addi $s3, $s3, 1 ble $s3, $s1, inner li $v0, 4 # Print newline after all dots on the line. la $a0, nl syscall addi $s2, $s2, 1 ble $s2, $s0, outer li $v0, 10 # end of program syscall # ------------------------------------------------------------------- # This is the function called sleep. # $t0 = loop bound # $t1 = loop counter sleep: move $t0, $a0 # Keep track of how high to count li $t1, 1 loop: addi $t1, $t1, 1 ble $t1, $t0, loop jr $ra