Question

In: Computer Science

Use MARS to write and simulate a MIPS assembly language program to implement the strlen function....

Use MARS to write and simulate a MIPS assembly language program to implement the strlen function. The program should ask for a user's input and print the length of the user's input. Write the main function to call strlen.

The main function should:
- Prompt the user for input
- Pass that input to strlen using registers $a0.
- Preserve (i.e. push onto the stack) any T registers that the main function uses. The strlen function should:
- Preserve any S registers it uses.
- Determine the length of the string.
- Return the length of the string to main using register $v0.

Solutions

Expert Solution

Code:

.data
        # reserve 100 bytes for user input
        inputString: .space 100
.text
.globl main

main:
        # load the address of the space where string is to be stored
        la $a0, inputString
        # load how many characters to read
        li $a1, 100
        # syscall code to read a string
        li $v0, 8
        syscall
        
        # load address of the string for passing to strlen function
        la $a0, inputString
        # call strlen function
        jal strlen
        
        # load the value returned by strlen from $v0 to $a0 for printing
        add $a0, $zero, $v0
        # syscall code to print an integer
        li $v0, 1
        syscall
        
        # syscall code to exit the program
        li $v0, 10
        syscall
# strlen function takes string in $a0, returns it's length in $v0
strlen:
        # initialize our counter $t0 to -1, so that null character is not counted
        li $t0, -1
        # loop through the string
        loop:
                # load the byte at address $a0 to $t1
                lb $t1, 0($a0)
                # if $t1 is '\0' i.e null character return 
                beqz $t1, return
                # else increment the address in $a0 by 1
                addi $a0, $a0, 1
                # increment the counter by 1
                addi $t0, $t0, 1
                # loop again
                j loop
        # return the value of $t0 and exit the function
        return:
                # load the value to $t0 to $v0
                add $v0, $zero, $t0
                # return back to the main
                jr $ra

                

OUTPUT:


Related Solutions

Assembly Language Coding Using MARS (MIPS) 1)Write a program that prints your name in a Triangle....
Assembly Language Coding Using MARS (MIPS) 1)Write a program that prints your name in a Triangle. (NAME = John Doe) 2)Write a Program that intializes X to 10, Y to 20 and Z to -50, adds X and Y and Z and prints the following the value of each variable, for example value of x is 10 as well as the result of the addition.
Using MARS write a MIPS assembly language program to prompt the user to input two 32-bit...
Using MARS write a MIPS assembly language program to prompt the user to input two 32-bit integers X and Y (X and Y can be prompted separately or at the same time), get them from the user then store them in memory locations labeled X and Y respectively. The program then loads X and Y from the main memory to registers, calculates the sum of them (i.e. X + Y) and store the sum into a memory location labeled S....
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 in MIPS assembly language to perform the calculation of the following equation and...
Write a program in MIPS assembly language to perform the calculation of the following equation and save the result accordingly:    f = 5x + 3y + z Assumptions: - Registers can be used to represent variables x, y, z, and f - Initialize x, y, and z to values of your choice. f can be initialized to zero. - Use comments to specify your register usage and explain your logic
In MIPS assembly language, write a function that will display the max and min value in...
In MIPS assembly language, write a function that will display the max and min value in an array. Then write a function to calculate and display the average of all values in an array. This must be done in MIPS assembly language.
In MIPS Assembly Language in Mars, define a method 1 to check if a number is...
In MIPS Assembly Language in Mars, define a method 1 to check if a number is divisible by 4. Then, define a method 2 to generate a random number, call method 1, and return result(number, yes/no) to main. Lastly, have the main method call method 2, and display the results.
Using MIPS (MARS) - Assembly Language Assignment ( PLEASE USE COMMENTS TO DESCRIBE EACH STEP )...
Using MIPS (MARS) - Assembly Language Assignment ( PLEASE USE COMMENTS TO DESCRIBE EACH STEP ) Take input of name, the input of hours worked, and input of hourly wage, and use input of hourly wage and hours worked to calculate total paycheck and print Name, and paycheck.
Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
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.
Use MIPS assembly language program to swap two of the integers in an integer array. The...
Use MIPS assembly language program to swap two of the integers in an integer array. The program should include the Swap function to swap the integers and the main function to call the Swap function. The main function should: • Pass the starting address of the array in $a0. • Pass the indices of the two elements to swap in $a1 and $a2. • Preserve (i.e. push onto the stack) any T registers that it uses. • Call the Swap...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT