Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

I need a flowchart/pseudocode for the code below which is a program that accepts a line of text from the user and counts the number

I need a flowchart/pseudocode for the code below which is a program that accepts a line of text from the user and counts the number of characters and the number of spaces in the string (not including the and null character) and prints the results.

main:

# Get the string from user li $v0, 4 # Load 4=print_string into $v0 la $a0, prompt # Load address of prompt into $a0 syscall # Output the prompt la $a0, prompt # Load address of prompt into $a0 li $v0, 8 # Load 8=read_string into $v0 la $a0, str # $a0=address of str li $a1, 1024 # $a1= max str length syscall # read string

# Compute string length

la $s0, str # $s0 contains base address of str add $s2, $0, $0 # $s2 = 0 addi $s3, $0, ' ' # $s2 = ' ' loop: lb $s1, 0($s0) # load character into $s0 beq $s1, $s3, end # Break if byte is newline addi $s2, $s2, 1 # increment counter addi $s0, $s0, 1 # increment str address j loop end: sb $0, 0($s0) #replace newline with 0

# Output

li $v0, 4 # Load 4=print_string into $v0 la $a0, r1 # Load address of newline into $a0 syscall # Output the newline li $v0, 4 # Load 4=print_string into $v0 la $a0, str # Load address of newline into $a0 syscall # Output the newline li $v0, 4 # Load 4=print_string into $v0 la $a0, r2 # Load address of newline into $a0 syscall # Output the newline li $v0, 1 # Load 1=print_int into $v0 add $a0, $s2, $0 # Load first number into $a0 syscall # Output the prompt via syscall li $v0, 4 # Load 4=print_string into $v0 la $a0, r3 # Load address of newline into $a0 syscall # Output the newline

# Exit

li $v0, 10 syscall

.data prompt: .asciiz "Hi, please enter a string of characters: " str: .space 1024 # Allocate 1024 bytes for the input string r1: .asciiz "The string \"" r2: .asciiz "\" has " r3: .asciiz " characters."

newline: .asciiz " "

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions