Question

Write a procedure, bfind( ), in C code using pointer-based and convert it into MIPS assembly...

  1. Write a procedure, bfind( ), in C code using pointer-based and convert it into MIPS assembly language with clear and necessary comments. The procedure should take a single argument that is a pointer to a null-terminated string in register $a0. The bfind procedure should locate the first character b in the string and return its address. If there are no b’s in the string, then bfind should return a pointer to the null character at the end of the string. For example, if the argument to bfind points to the string "imbibe," then the return value will be a pointer to the third character of the string. Use minimum number of MIPS instructions.

C code:

char *bfind(const char *str)

{

char *p;

for(p=str;*p;p++)

   {

    if(*p==’b’)

      return p;  

            }

return p;

}

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

PROGRAM:

.data
   string: .asciiz "imbibe"
   output: .asciiz "Character at the returned address is: "
.text
.globl main
main:
   la $a0,string       # Load the address to string to pass as argument to subroutine

   jal bfind       # Jump to subroutine

   move $t0,$v0       # Store the return value
  
   li $v0,4       # 4 is the print string syscall
   la $a0,output       # Load the address of string to be printed
   syscall           # Print the string
   li $v0,11       # 11 is print character syscall
   lb $a0,0($t0)       # load $a0 with the character to be printed
   syscall           # Print the character
  
   li $v0,10       # 10 is the exit program syscall
   syscall           # Exit the program
  
# Definition of bfind subroutine
bfind:
   # Adjust the stack pointer
   addi $sp,$sp,-8
   sw $s1,4($sp)       # Store $s1 on stack
   sw $ra,0($sp)       # Store return address on stack
  
loop:   lb $t0,0($a0)       # Load a byte from string
   beq $t0,98,return   # If byte is equal to character b, return the address
   beqz $t0,return       # If byte is equal to null return the address
   addi $a0,$a0,1       # Increment the string pointer
   b loop           # Repeat the loop
  
return:   move $v0,$a0       # Load the address to be returned into $v0
   lw $s1,4($sp)       # Restore $s1 from stack
   lw $ra,0($sp)       # Restore $ra from stack
   addi $sp,$sp,-8       # Readjust stack pointer to its original position
   jr $ra           # Jump to return address (i.e., to calling function, main)

Please refer to the following screenshot of the program for indentation of the code:

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a procedure, bfind( ), in C code using pointer-based and convert it into MIPS assembly...
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...

  • MIPS ASSEMBLY LANGUAGE Write MIPS code to convert binary to decimal: (01110001)2 to (113)10 .data   binary_digit:  ...

    MIPS ASSEMBLY LANGUAGE Write MIPS code to convert binary to decimal: (01110001)2 to (113)10 .data   binary_digit:       .word       0 1 1 1 0 0 0 1       # is 113 in decimal .globl main main: # Load arguments to argument registers # Call convert()      # Print return value # (it's in $v0, make sure to copy it before overwriting to print)        # Properly end program    convert: # This label is where conversion of the current...

  • Turn the Following c-code into MIPS assembly code. You are given the main procedure which calls...

    Turn the Following c-code into MIPS assembly code. You are given the main procedure which calls multiply. You are also given the argument registers to be used. /* C-program */ int multiply (int number, int times) { int f; f = number * times return f; } # MIPS PROGRAM # assumes we have called the program leaf_example # $a0=number, $a1=times, $s0=f main: $addi $a0,$zero,3 $addi $a1,$zero,2 jal multiply # place the address into $ra j EXIT2 EXIT2: j OS...

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

  • ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra,...

    ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra, 4($sp) # save the return address on stack beqz $a0, terminate # test for termination subu $sp, $sp, 4 # do not terminate yet sw $a0, 4($sp) # save the parameter sub $a0, $a0, 1 # will call with a smaller argument jal Factorial # after the termination condition is reached these lines # will be executed lw $t0, 4($sp) # the argument I...

  • **C** Write a C function that inputs a pointer to a string and a pointer to...

    **C** Write a C function that inputs a pointer to a string and a pointer to a buffer. The function then scans through the string removing leading and trailing whitespace and replacing any run of whitespace within the string with a single space. Your function should follow this prototype: void tighten(char *oldstring, char* newstring, int length); The first argument is a pointer to a null-terminated string sitting somewhere in memory. The second argument is a pointer to the first char...

  • Write a new subroutine in assembly to convert the upper-case letters to lower-case letter. Example of...

    Write a new subroutine in assembly to convert the upper-case letters to lower-case letter. Example of lower case to upper is provided below: Let’s look at a subroutine to capitalize all the lower-case letters in the string. We need to load each character, check to see if it is a letter, and if so, capitalize it. Each character in the string is represented with its ASCII code. For example, ‘A’ is represented with a 65 (0x41), ‘B’ with 66 (0x42),...

  • 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

  • B2. Convert the C code to MIPS assembly with only 2 efficient instructions: Register assignment: ...

    B2. Convert the C code to MIPS assembly with only 2 efficient instructions: Register assignment: timer-v0 int timer = 0x0AC8 0001; B3. Write MIPS assembly code segment for the following C code snippet for (i - 0, i < 100; i++) -array Register assignment: i-) $ao Base of array -> $s0 array [ i+1] [i] / 2; B2. Convert the C code to MIPS assembly with only 2 efficient instructions: Register assignment: timer-v0 int timer = 0x0AC8 0001; B3. Write...

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