# A SAL program to calculate the longest, shortest,
# and average length of strings entered by the user.

		.data
str_count:	.word	0	# number of user-entered strings
sum:		.word	0	# running sum of the string lengths
ave:		.word	0	# average of the string lengths
str_length:	.word		# length of each string
shortest:	.word	1000
longest:	.word	-1
ch:		.byte		# used to read characters
newline:	.byte	'\n'

getstring_ra:	.word
calculate_ra:	.word
average_ra:	.word
printresults_ra: .word

str1:		.asciiz	"Enter a string (<CR> to stop):"
str2:		.asciiz	"The longest string entered was "
str3:		.asciiz	" characters long.\n"
str4:		.asciiz	"The shortest string entered was "
str5:		.asciiz	"The average string length was "
str6:		.asciiz " characters.\n"

		.text
__start:	la	getstring_ra,rtn1
		b	getstring
rtn1:		beqz	str_length,endwhile

		la	calculate_ra,rtn2
		b	calculate
rtn2:
		la	getstring_ra,rtn3
		b	getstring
rtn3:		b	rtn1			# end while-loop

endwhile:	blez	str_count,rtn5
		la	average_ra,rtn4
		b	average
rtn4:
		la	printresults_ra,rtn5
		b	printresults
rtn5:		done

# procedure getstring
getstring:	move	str_length,0
		puts	str1
		get	ch
while:		beq	ch,newline,getstr_rtn
		add	str_length,str_length,1
		get	ch
		b	while
getstr_rtn:	b	(getstring_ra)

# procedure calculate
calculate:	add	str_count,str_count,1
		add	sum,sum,str_length
		ble	str_length,longest,nextif
		move	longest,str_length
nextif:		bge	str_length,shortest,calc_rtn
		move	shortest,str_length
calc_rtn:	b	(calculate_ra)

# procedure average
average:	div	ave,sum,str_count
		b	(average_ra)

# procedure printresults
printresults:	puts	str2
		put	longest
		puts	str3
		puts	str4
		put	shortest
		puts	str3
		puts	str5
		put	ave
		puts	str6
		b	(printresults_ra)
