Question

In: Computer Science

Write a simple MIPS program that asks the user to input a string and then a...

Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.

Solutions

Expert Solution

.data 
    command1:   .asciiz "Please enter a sentence "
    command2:   .asciiz "Please enter a character "
    count:      .word 10
    sentence:   .space 100
    character:  .space 4
    command3:   .asciiz " Character "
    command4:   .asciiz " appears in the sentence "
    command5:   .asciiz " times. "

.text
.globl main
main:
    li $t0, 100 #end var for loop
    li $t1, 0   #start var for loop
    li $t2, 0   #number of occurences

    la $a0, command1    #print 'please enter sentence'
    li $v0, 4
    syscall


    la $a0, sentence    #input sentence
    li $a1, 100
    li $v0, 8
    syscall

    la $a0, command2    #print 'please enter char'
    li $v0, 4
    syscall

    la $a0, character   #input character
    li $a1, 4
    li $v0, 8
    syscall

    la $s0, character
    lb $s1, ($s0)
    la $t3, sentence
    lb $a2, ($t3) #gets first char of string

loop:
    beq $a2, $zero, end     #once reach end of char array, prints result
    beq $a2, $s1, something #if the char within sentence == comparing char
    addi $t3, $t3, 1
    lb $a2,($t3)        
    j loop

something:              
    addi $t2, $t2, 1        #increments number of occurences of char 
end:
    la $a0, command3
    li $v0, 4
    syscall

    la $a0, character
    li $v0, 11
    syscall

    la $a0, command4
    li $v0, 4
    syscall

    la $a0 sentence
    li $v0, 4
    syscall

    #la $a0, $t2
    li $v0, 1
    move $a0, $t2
    syscall

    la $a0, command5
    li $v0, 4
    syscall

Hope this helps you. Please give an upvote. Thank you.


Related Solutions

Write a MIPS assembly program that prompts the user first for a string, then for a...
Write a MIPS assembly program that prompts the user first for a string, then for a character. The program should then search the string for the character and print out the number of times the character appears in the string. You can use as many restrictions as you want, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
Write a mips program that would function in QTSPIM that asks the user to enter a...
Write a mips program that would function in QTSPIM that asks the user to enter a list of integer values, one per line, with the last value being a negative. The program should read in these integers until a negative number is entered. Afterwards the program should print 1) sum, 2) minimum value, 3) max value, 4) mean and 5) variance value. of the numbers that were entered.
Write a MIPS program that asks the user for 2 numbers. Output the sum of the...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the 2 numbers. The difference between the 2 numbers (num1-num2) and (num2-num1) The value that is 305 more than the 1st number. The value that is 305 less than the 2nd number
Write a mips assembly language program that asks the user to enter an unsigned number and...
Write a mips assembly language program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6.
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user toinput an...
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user to input an integer and then prints out a string that shows how that integer should be encoded using 16 bits. Your program should handle both positive and negative valued inputs. Your program should also print out an error message if the given input cannot be expressed as a 16 bit signed integer.As an example, if the input is 12, your program should output “0000000000001100”. If the input...
Write a program, ArrayRange, that asks the user to input integers and that displays the difference...
Write a program, ArrayRange, that asks the user to input integers and that displays the difference between the largest and the smallest. You should ask the user the number of integers s/he would like to enter (this will allow you to set the length of the array). Allow the user to input all the numbers and then store them in the array. Search the array for the largest number, then search for the smallest, and finally compute the calculation. Display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT