In this homework, you are supposed to write a code find the longest word in an a
ID: 3600998 • Letter: I
Question
In this homework, you are supposed to write a code find the longest word in an asciiz string. The string is a part of a Shakespeare play from http://www.william-shakespeare.info/act2-script-text-henry-vi-part1.htm.
In this homework, a word of length n is defined as n consecutive ascii codes, where each code is either between ‘a’ and ‘z’ or ‘A’ and ‘Z’, inclusive.
I have provided the skeleton of the code at the end of this description of the homework. You will need to implement 3 functions. If your functions are correct, the code will print the longest word. Basically, the main program calls the “findmax” function, which returns the longest word.
# -----------------------------------------------
# findmax (40 points)
#
# $a0: the starting address of an asciiz string
# $a1: the starting address of an allocated memory space to which the maximum length word is to
# be copied, ending with a 0 to make it an asciiz string
#
# Note: Must call the getword function to examine one word at a time
# -----------------------------------------------
# -----------------------------------------------
# getword (40 points)
#
# $a0: the starting address of an asciiz string, say, A, at this address, the content must be the
# ascii code of an English letter, either upper case of lower case
# $a1: the starting address of an allocated memory space, to which the first word in string A can
# be copied, ending with a 0 to make it an asciiz string
# $v0: the length of the word
#
# Note: Must call the isletter function
# -----------------------------------------------
# -----------------------------------------------
# isletter (20 points)
#
# $a0: a value
$ $v0: returns 1 if the value in $a0 is the ascii code of an English letter, 0 otherwise
# -----------------------------------------------
"
returnres : .space 100
.text
.globl main
main:
la $a0, dialog
la $a1, returnres
jal findmax
li $v0,4 # now print the found longest word
la $a0,returnres
syscall
done:li $v0, 10 # Exit
syscall
findmax:
jr $ra
getword:
jr $ra
isletter:
jr $ra
# -----------------------------------------------
# strcpy
#
# Note: copies the string starting at $a1 to an allocated memory space starting at $a0, the same
# as the code we wrote in the class, will be useful
# -----------------------------------------------
strcpy:
lb $t0, 0($a1)
sb $t0, 0($a0)
addi $a0, $a0, 1
addi $a1, $a1, 1
bne $t0, $0, strcpy
jr $ra
Explanation / Answer
findmax (40 points)
#
# $a0: the starting address of an asciiz string
# $a1: the starting address of an allocated memory space to which the maximum length word is to
# be copied, ending with a 0 to make it an asciiz string
#
# Note: Must call the getword function to examine one word at a time
# -----------------------------------------------
# -----------------------------------------------
# getword (40 points)
#
# $a0: the starting address of an asciiz string, say, A, at this address, the content must be the
# ascii code of an English letter, either upper case of lower case
# $a1: the starting address of an allocated memory space, to which the first word in string A can
# be copied, ending with a 0 to make it an asciiz string
# $v0: the length of the word
#
# Note: Must call the isletter function
# -----------------------------------------------
# -----------------------------------------------
# isletter (20 points)
#
# $a0: a value
$ $v0: returns 1 if the value in $a0 is the ascii code of an English letter, 0 otherwise
# -----------------------------------------------
"
returnres : .space 100
.text
.globl main
main:
la $a0, dialog
la $a1, returnres
jal findmax
li $v0,4 # now print the found longest word
la $a0,returnres
syscall
done:li $v0, 10 # Exit
syscall
findmax:
jr $ra
getword:
jr $ra
isletter:
jr $ra
# -----------------------------------------------
# strcpy
#
# Note: copies the string starting at $a1 to an allocated memory space starting at $a0, the same
# as the code we wrote in the class, will be useful
# -----------------------------------------------
strcpy:
lb $t0, 0($a1)
sb $t0, 0($a0)
addi $a0, $a0, 1
addi $a1, $a1, 1
bne $t0, $0, strcpy
jr $ra
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.