Question

MIPS programming question Problem 1: Write a program that asks the user to input a string...

MIPS programming question

Problem 1: Write a program that asks the user to input a string (or no more than 50 characters). Your program should then output the length of the string. The string length should be determined using a separate function strlen that will accept the address of the string and return its length. For the purposes of this exercise, the length of a string will be defined as the number of non-null and non-newline characters until either the null character (ASCII 0) or the newline character (ASCII 10) is encountered.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program screen shot:

Sample output:

Program code to copy:

;Declare the program

section   .data

   ; Input the string from the user

   prompt: .asciiz "Enter a string: "

   str: .space 50

  

   ; Display the length of the string

   msg: .asciiz "The length of the string is "

; Declare the text

.text

; THe main function of the program

main:

   ; Prompt the user string

   la $a0, prompt

   li $v0, 4

   syscall

    

   ;Read out the input string

   la $a0, str

   li $a1, 50

   li $v0, 8

   syscall

   ; Find out the string length

   #call the function strlen, address of string is already in $a0, lenght returned in $t1

   jal strlen

   ;Display function is used to display the result of the string

   li $v0, 4  

   la $a0, msg

   syscall

   li $v0, 1

   move $a0, $t1

   syscall

   #exit

   li $v0, 10

   syscall

; create a string length function method

strlen:

; Initiliaze the counter with t1 to zero.

   move $t1, $zero

  

; create a loop

strlen_loop:

   ; got the result character into t0

   lb $t0, ($a0)  

  

   ; Verify that is null character or not

   beq $t0, 0, strlen_end

  

   ; THe new line verify

   beq $t0, '\n', strlen_end

  

   ; Increment the counter one by one

   addi $t1, $t1, 1

   addi $a0, $a0, 1

  

   ; end of the loop

   b strlen_loop

  

; end of the string length

strlen_end:

; return to the register

   jr $ra

Add a comment
Know the answer?
Add Answer to:
MIPS programming question Problem 1: Write a program that asks the user to input a string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MIPS ASSEMBLY PROGRAM: PLEASE Write in MIPS Assembly language. Take strings as input and calculate and...

    MIPS ASSEMBLY PROGRAM: PLEASE Write in MIPS Assembly language. Take strings as input and calculate and print a simple checksum for each string. Make your string long enough to hold 50 characters. Don't forget to leave space for the null byte. Our checksum algorithm will produce a value which you can print with the syscall for printing a character. Stop reading strings when the user enters ".". The syscall to read a string (sycall code 8) adds a newline to...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Write a program that asks the user to enter a string and then asks the user...

    Write a program that asks the user to enter a string and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. python programming

  • 2. Searching a String: Write a MIPS assembly language program to do the following: Read a...

    2. Searching a String: Write a MIPS assembly language program to do the following: Read a string and store it in memory. Limit the string length to 100 characters. Then, ask the user to enter a character. Search and count the number of occurrences of the character in the string. The search is not case sensitive. Lowercase and uppercase letters should be equal. Then ask the user to enter a string of two characters. Search and count the number of...

  • Prompt the user and read in a string. Make your string large enough to hold 100...

    Prompt the user and read in a string. Make your string large enough to hold 100 characters. 2. Count the number of words in the string. A word is one or more non-blank characters separated by one or more blanks. My suggestion is to use a flag (a boolean variable) to indicate whether or not you are in a word. Then you know you have found a word when the flag indicates that you are in a word and the...

  • Create a program that performs the following operations: Prompt for and accept a string of up...

    Create a program that performs the following operations: Prompt for and accept a string of up to 80 characters from the user. • The memory buffer for this string is created by: buffer: .space 80 # create space for string input • The syscall to place input into the buffer looks like: li $v0, 8 # code for syscall read_string la $a0, buffer # tell syscall where the buffer is li $a1, 80 # tell syscall how big the buffer...

  • 21 Write a program that asks the user to input the length and breadth of a...

    21 Write a program that asks the user to input the length and breadth of a soccer field, and then computes and returns the number of square meters of grass required to cover the field The formula for the area is the product of length and breadth (5) 22 The following program uses the break statement to terminate an infinite while loop to print 5 numbers Rewrite the program to use a while loop to display numbers from 1 to...

  • In this part of the assignment, you will write MIPS assembly code to replace characters in...

    In this part of the assignment, you will write MIPS assembly code to replace characters in a string. This program asks the user for a string named string and two characters orig and new. It then replaces each instance of the character orig found in string with the character new. It outputs the resulting string and the number of substitutions that were made. For example, if string were "wow", orig were 'w', and new were 'b', the program would output...

  • Java Question, I need a program that asks a user to input a string && then...

    Java Question, I need a program that asks a user to input a string && then asks the user to type in an index value(integer). You will use the charAt( ) method from the string class to find and output the character referenced by that index. Allow the user to repeat these actions by placing this in a loop until the user gives you an empty string. Now realize that If we call the charAt method with a bad value...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT