I am trying to create a function called strlen that returns

I am trying to create a function called strlen that returns the length of an inputted string using MIPS32 Assembly Language Code.

Please let me know if you can help me fix my strlen function so it works properly? Thank you.

main:
# int len, char string[BUF_LEN]
addi $sp, $sp, -88 # Allocate local vars in stack frame

# SysPrintStr(\"Enter a string (length <= 80)? \")
addi $v0, $zero, SYS_PRINT_STR # $v0 ? SysprintStr service code
la $a0, s_prompt # $a0 ? addr of s_prompt
syscall # SysPrintStr(\"Enter a string (length <= 80)? \")

# string ? SysReadStr(string, SYS_RS_LEN)
addi $v0, $zero, SYS_READ_STR # $v0 ? SysReadStr service code
addi $a0, $sp, 4 # $a0 ? &string
addi $a1, $zero, SYS_RS_MAX # $a1 ? max num of chars to read
syscall # SysReadStr(string, SYS_RS_LEN)

# len ? strlen(string)
addi $a0, $sp, 4 # $a0 ? &string
jal strlen # $v0 ? strlen(string)
sw $v0, 0($sp) # len ? strlen(string)

# SysPrintInt(len)
addi $v0, $zero, SYS_PRINT_INT # $v0 ? SysPrintInt service code
lw $a0, 0($sp) # $a0 ? len
syscall # SysPrintInt(len)

# SysPrintChar(\'\ \')
addi $v0, $zero, SYS_PRINT_CHAR # $v0 ? SysPrintChar service code
addi $a0, $zero, \'\ \' # $a0 ? ASCII value of \'\ \' (10)
syscall # SysPrintChar(\'\ \')

# Deallocate stack frame
addi $sp, $sp, 88 # Deallocate stack frame

# SysExit()
addi $v0, $zero, SYS_EXIT # $v0 ? SysExit service code
syscall # SysExit()

#-------------------------------------------------------------------------------------------------------
# FUNCTION: int strlen(char string[])
#
# DESCRIPTION
# Computes and returns the length of the C-string \'string\'.
#
# ARGUMENTS
# $a0 - string (the address of the string)
#
# RETURNS
# $v0 - the length of string.
#
# PSEUDOCODE
# function strlen(char string[])
# local int index ? 0
# begin_loop:
# if string[index] = \'\\0\' goto end_loop
# ++index
# goto begin_loop
# end_loop:
# return index
#-------------------------------------------------------------------------------------------------------
strlen:


# int index= 0;
#addi $sp, $sp, 4 #allocate one word in stackfrace for index
# sw $a0, 0($sp) #save $a0 (index) in stack frame
# sw $zero, 0($sp) #initialize index in stack frame at 0

  
#if (string[index] == \'\\0\') goto end_loop
addi $t0, $zero, 0 # index ($t0) = 0
add $t1, $a0, $t0 # $t1= string + i = &string[i]
begin_loop:

beq $a0, $zero, end_loop #if (string[index] == \'\\0\') go to end_loop

addi $t0, $t0, 1 #++index

lw $t0, 0($sp) #$t0 = index
  
# add $t0, $t0, 1 #$t0= index+1
sw $t0, 0($sp) #++i

#goto begin_loop


j begin_loop #continue looping
  
end_loop:


addi $v0, $zero,SYS_PRINT_INT # $v0 ? SysPrintInt service code # $a0 ? len
syscall # SysPrintInt(len)

# FUNCTION: void main() # DESCRIPTION # Asks the user to enter a string, calls strlen() passing the string as the argument, and outputs the # return value. # PSEUDOCODE # global constant MAX MSG LEN 80 # global constant BUF-LEN # global constant SYSRS MAX MAX_ASG-LEN + 2 -Arg $al to SysReadStr is the max num of chars to read Max number of chars in a valid message 84 Size of the string array allocated in main() # function main() #int len, char string [BUF_LEN SysPrintStr(\"Enter a string (length

Solution

there is no fix in the strlen i,e..

# int index= 0;
#addi $sp, $sp, 4 #allocate one word in stackfrace for index
# sw $a0, 0($sp) #save $a0 (index) in stack frame
# sw $zero, 0($sp) #initialize index in stack frame at 0

  
#if (string[index] == \'\\0\') goto end_loop
addi $t0, $zero, 0 # index ($t0) = 0
add $t1, $a0, $t0 # $t1= string + i = &string[i]
begin_loop:

beq $a0, $zero, end_loop #if (string[index] == \'\\0\') go to end_loop

addi $t0, $t0, 1 #++index

lw $t0, 0($sp) #$t0 = index
  
# add $t0, $t0, 1 #$t0= index+1
sw $t0, 0($sp) #++i

#goto begin_loop


j begin_loop #continue looping
  
end_loop:


addi $v0, $zero,SYS_PRINT_INT # $v0 SysPrintInt service code # $a0 len
syscall # SysPrintInt(len)

I am trying to create a function called strlen that returns the length of an inputted string using MIPS32 Assembly Language Code. Please let me know if you can
I am trying to create a function called strlen that returns the length of an inputted string using MIPS32 Assembly Language Code. Please let me know if you can
I am trying to create a function called strlen that returns the length of an inputted string using MIPS32 Assembly Language Code. Please let me know if you can

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site