# MIPS assembler code to initialize the elements of a 1-d array
#  with the value stored in location val

.data
ar1:        .word   0:20    # array of 20 integers (4 bytes each)
val:        .float  1.e-3   # value to store in array elements
veclen:     .word   20      # vector length (number of elements)
size:       .word   4       # size of an array element, in bytes

.text
__start:
        la      $a0,ar1             # First argument: a pointer to ar1[0]
        lw      $a1,size            # Second argument: value of size
        lw      $a2,veclen          # Third argument: value of vector length
        lw      $a3,val             # Fourth argument: value to be stored
        jal     initv
        ori     $v0,$0,10           # These two instructions are equivalent
        syscall                     #   to the SAL command "done"

initv:  nop                         # Entry point to procedure initv
        blez    $a2,beamup          # If veclen<=0, I'm outta here  '
		or		$t0,$0,$a0          # Reg. t0 points to the array element
		or		$t1,$0,$a2			# Reg. t1 is a counter
loop:     sw    $a3,0($t0)          # Store the value into the array element
          add   $t0,$a1,$t0         # Increment the pointer by the value of size
          addi  $t1,-1              # Decrement the counter
          bgtz  $t1, loop           # branch back to loop if counter >= 0
                                    #  (since we store at the head of the
                                    #   loop, we compute one more address 
                                    #   than necessary just to reduce 
                                    #   the number of compares & branches)
beamup: jr $ra                      # Beam me up....

