## LSU EE 4720
#
 ## Classroom Live Demo -- strlen (string length)
 #

        .text
strlen:
        ## Register Usage
        #
        # $a0: Address of first character of string.
        # $v0: Return value, the length of the string.
        #
        # Can modify $t0-$t9



        jr $ra
        nop



##############################################################################
#
 ## Test Code
#
#  The code below calls "strlen".

        .data
str:
        .asciiz "Per"
        .asciiz "Per aspera, ad astra!"
        .asciiz ""

msg:
        .ascii "The length of string \n   \"%/a1/s\"\nis %/v1/d.\n"
        .ascii "In strlen executed %/s4/d instructions ..\n"
        .ascii ".. computing the string length at %/v1/d / %/s4/d = %/f6/.3f char/insn ..\n"
        .asciiz ".. assuming that the string length really is %/v1/d characters.\n"


        .text
        .globl __start
__start:
        mtc0 $0, $22            # Pause tracing.
        la $a0, str             # Load address of string.

        mtc0 $v0, $22           # Resume tracing. (No effect if not stepping.)
        jal strlen              # Call strlen procedure.
        mfc0 $s5, $9            # Copy current instruction count. (Before.)
        mfc0 $s4, $9            # Copy current instruction count. (After.)
        mtc0 $0, $22            # Pause tracing.
        sub $s4, $s4, $s5       # Compute number of instructions executed.
        mtc1 $s4, $f4           # Move number of instructions ..
        mtc1 $v0, $f0           # .. and string length to FP registers and ..
        div.d $f6, $f0, $f4     # .. compute execution rate in char / insn.

        la $a1, str             # Load address of string to $a1
        addi $v1, $v0, 0        # Move length of string to $v1
        addi $v0, $0, 11        # System call code for message.
        la $a0, msg             # Address of message.
        syscall
        addi $v0, $0, 10        # System call code for exit.
        syscall