# A simple program to demonstrate loops, and array accessing # Written by Scott D. Anderson, 2/2/98 .text .globl main main: nop nop # # This code corresponds roughly to the following C code: # int array = { 16, 32, 64, 128, 256, 512, 1024, 2048 } # int x=0xFFFFFFFF # x=0; # for( int i=0; i<8; i++ ) x += array[i] # array[5] = x # .data x: .word 0xFFFFFFFF Array: .word 16 .word 32 .word 64 .word 128 .word 256 .word 512 .word 1024 .word 2048 .text sw $0,x # initialize x to zero add $s0, $0, $0 # Use $s0 as i, initialize it to zero addi $s1, $0, 8 # Use $s1 for the upper bound of the loop # init it to the constant 8 add $s2, $0, $0 # Use $s2 for the running sum, init to zero la $s3, Array # Use $s3 as pointer into the array. loop1: slt $t0, $s0, $s1 # is i < 8? beq $t0, $0, done1 # done if i isn't < 8, when $t0 will be zero lw $t1,0($s3) # get some array data into temporary add $s2,$s2,$t1 # add it to running sum sw $s2,x # store into x--is this really necessary? addi $s0,$s0,1 # add 1 to $s0. addi $s3,$s3,4 # add 4 to $s2. Why 4? j loop1 done1: la $s3, Array # reload $s3 with address of array sw $s2,20($s3) # why 20? sw $0,0x14($s3) # what does this instruction do? # # Done !!! # jr $ra # Return from main