Question

**Please use MIPS assembly language** int strlen(char* str): Parameter str is passed in a register. Assume...

**Please use MIPS assembly language**

int strlen(char* str): Parameter str is passed in a register. Assume that str points to a nulterminated string. Returns the length of the string in a register

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

Solution: The MIPS code for returning the length of string in a register is given below:

.text
main:
li $t0, 0 # Here we are initializing the  loop-counter
la $a0, message #load the address of the 1st byte

loop:
lb $t1, ($a0) # load the content of the address stored in $a0
beq $t1, $zero, exit   
# exit the program if $t0 == null

addi $t0, $t0, 1 # increment the loop counter
addi $a0, $a0, 1

j loop

exit:
move $a0, $t0 # prepare to print the integer
li $v0, 1 # integer syscall
syscall

li $v0, 10
syscall

Add a comment
Know the answer?
Add Answer to:
**Please use MIPS assembly language** int strlen(char* str): Parameter str is passed in a register. Assume...
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
  • ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and...

    ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...

  • 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...

  • MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4,...

    MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4, 5, 6); int main) int num, position; scanf("%d",&num) ; position search(array, printf("The position is: num, 5); %d\n",positio int search(int array, int num, int size int position =-1; for(int i-0;i<size; i++) if(array [i]=num) { position-i; break; return position; Register map $s1: position $a0: array address $a1: num . $a2: size . $VO: return value

  • Given a String str, and a char c return the number of times that c appears...

    Given a String str, and a char c return the number of times that c appears inside str. countChar("Abcdefg", 'a') returns 1 countChar("xxXx", 'x') returns 4 countChar("", 'q'') returns 0 ----------------- Please use java public class Count { public int countProblem(String str, char c) { int res = 0; //Your work is here return result; } }

  • Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will...

    Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will read in a base (as an integer) and a value (nonnegative integer but as an ASCII string) in that base and print out the decimal value; you must implement a function (which accepts a base and an address for a string as parameters, and returns the value) and call the function from the main program. The base will be given in decimal and will...

  • Write a function that can return the length of a string in C language. Please leave...

    Write a function that can return the length of a string in C language. Please leave comments to explain the code and verify that it runs properly... Example: Input String: China The length of the string is 5. #include <stdio.h> int length(char * p) {     /write your code here } main(){     int len;     char str[20];     printf("Input string:");     scanf("%s", str);     len = length(str);     printf("The length of string is %d. ", len);     getchar();    ...

  • This is a function. PLEASE ANSWER IN MIPS Basic assembly language. No psuedo like li, la,...

    This is a function. PLEASE ANSWER IN MIPS Basic assembly language. No psuedo like li, la, move, etc. Thank you. 2. int readData (&array): The starting address of an array is passed to the function as a parameter using Sa0. The function must prompt for and read and store integers in the array until either a zero is entered or 10 numbers are read. Once the tenth integer is entered, your program must stop reading. The function must return (using...

  • Write the corresponding MIPS assembly equivalent of the given Ccode. Assume that register $s0 contains the...

    Write the corresponding MIPS assembly equivalent of the given Ccode. Assume that register $s0 contains the base address for array arr. Use a register for each of the variables. (e.g. use $sl for total, etc.) and indicate them as a comment line in your code. int main() { inti, s = 10; int total, ps, ng int arr[10] = {3, -21, 18,0, 16, 85,-44, 23, 100, -20}; total = 0; ps = 0; ng = 0; for (i = 0;...

  • Convert the following C fragment to equivalent MIPS assembly language. Assume that the variables a, b,...

    Convert the following C fragment to equivalent MIPS assembly language. Assume that the variables a, b, c, d, i and x are assigened to registers $t1, $t2, $t3, $t4, $s0 and $s1 respectively. Assume that the base address of the array A and B is in register $a0 and $a1 respectively. if (a > 0)            b = a + 10;            else                b = a - 10;

  • Can anyone help to solve this MIPS assembly language problem? Please help. Translate the fowlowwing C...

    Can anyone help to solve this MIPS assembly language problem? Please help. Translate the fowlowwing C code to MIPS assembly language. void main() { int i, sum, begin, end; for(i=0; i < 5; i++){ scanf(“%d, %d”, &begin, &end); sum = accum_range(begin, end); printf(“sum[%d: %d] = %d\n”, begin, end, sum); } int accum_range(int a, int b){ return accum (b) - accum(a); } int accum(int final) { int sum = 0; for (int I = 1; I <= final; I = I...

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