         .data                           # declare data segment
str:     .asciiz         "EE2310"        # an arbitrary null-terminated string
#
# Beginning of text segment
#
         .text                           # declare code segment
__start:                                 # needed so SPIM knows entry point
         add             $t1,$0,$0       # initialize pointer (index)
# The following SPIM code implements a C while loop
#
# Equivalent C code:
#   int t1;
#   t1=0;
#   while (str[t1] != '\0')
#      t1++;
#   return t1;
#
# In this SPIM loop, the value in $t1 takes the place of i in the C code
again:   lb              $t5,str($t1)    # load byte (read one character)
         beq             $t5,$0,finish   # done if sentinel character ('\0')
         addi            $t1,$t1,1       # increment offset (by 1, not 4; why?)
         j               again           # repeat
                                         # The "j" instruction is an
                                         #  unconditional jump, like a
                                         #  "goto" in other languages
                                         # We have to use "j" here, because
                                         #  the string length is unknown
finish:  addi            $v0,$0,1        # $v0=1 (to print integer in $a0) 
         add             $a0,$t1,$0      # copy from t1 to a0
         syscall                         # call O/S to print to console
         addi            $v0,$0,10       # $v0=10 (to return control to SPIM)
         syscall

