## 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.
        #

        sw $s0, 16($sp)

        addi $t1, $a0, 1

WHEREWESTARTED:
        lb $s0, 0($a0)
        bne $s0, $0, WHEREWESTARTED
        addi $a0, $a0, 1

WHEREDONE:


        lw $s0, 16($sp)

        jr $ra
        sub $v0, $a0, $t1



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

        .data
str:
        .asciiz "Per"
        .asciiz "Per aspera, ad astra!"
msg:
        .asciiz "The length of string \n   \"%/a1/s\"\nis %/v1/d.\n"


        .text
        .globl __start
__start:
        la $a0, str             # Load address of string.


        # Caller save
        sw $t0, 0($sp)

        jal strlen              # Call strlen procedure.
        nop

        lw $t0, 0($sp)

        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