## LSU EE 4720 Computer Architecture Spring 2010 # # Classroom example, covered 22, 25 January 2010 # :Example: # # Procedure to determine the length of a C-style string. Includes # code to call the procedure. .data str: .asciiz "I Love NO" msg: .asciiz "The length of string \n \"%/a1/s\"\nis %/v1/d.\n" .text .globl __start __start: la $a0, str # Load address of string (for strlen routine). jal strlen # Call strlen procedure. nop la $a1, str # Load address of string (for message). 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 ################################################################################ ## strlen: Return length of string. ## ## Register Usage # # $a0: CALL VALUE: Address of the first character of the string. # $v0: RETURN VALUE: The length of the string. strlen: addi $v0, $a0, 1 LOOP: lb $t0, 0($a0) bne $t0, $0 LOOP addi $a0, $a0, 1 jr $ra sub $v0, $a0, $v0 ####