Question

In: Computer Science

How can I convert the following subroutine into MIPS assembly: search(Array, start, end, searchValue) { if...

How can I convert the following subroutine into MIPS assembly:

search(Array, start, end, searchValue)

{

if (start > end) return false

mid = start + (end - start)/2;

if (array[mid] == searchValue) return true;

if (array[mid] > searchValue) return search(array, start, mid-1, searchValue);

return search(array, mid+1, end, searchValue);

}

Solutions

Expert Solution

search(int*, int, int, int):
addiu $sp,$sp,-40
sw $31,36($sp)
sw $fp,32($sp)
move $fp,$sp
sw $4,40($fp)
sw $5,44($fp)
sw $6,48($fp)
sw $7,52($fp)
lw $3,44($fp)
lw $2,48($fp)
nop
slt $2,$2,$3
beq $2,$0,$L2
nop

move $2,$0
b $L3
nop

$L2:
lw $3,48($fp)
lw $2,44($fp)
nop
subu $2,$3,$2
srl $3,$2,31
addu $2,$3,$2
sra $2,$2,1
move $3,$2
lw $2,44($fp)
nop
addu $2,$3,$2
sw $2,24($fp)
lw $2,24($fp)
nop
sll $2,$2,2
lw $3,40($fp)
nop
addu $2,$3,$2
lw $3,0($2)
lw $2,52($fp)
nop
bne $3,$2,$L4
nop

li $2,1 # 0x1
b $L3
nop

$L4:
lw $2,24($fp)
nop
sll $2,$2,2
lw $3,40($fp)
nop
addu $2,$3,$2
lw $3,0($2)
lw $2,52($fp)
nop
slt $2,$2,$3
beq $2,$0,$L5
nop

lw $2,24($fp)
nop
addiu $2,$2,-1
lw $7,52($fp)
move $6,$2
lw $5,44($fp)
lw $4,40($fp)
jal search(int*, int, int, int)
nop

b $L3
nop

$L5:
lw $2,24($fp)
nop
addiu $2,$2,1
lw $7,52($fp)
lw $6,48($fp)
move $5,$2
lw $4,40($fp)
jal search(int*, int, int, int)
nop

$L3:
move $sp,$fp
lw $31,36($sp)
lw $fp,32($sp)
addiu $sp,$sp,40
j $31
nop


Related Solutions

convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:(a) Prompt the user to enter all the 10 integers in the array.(b) Prompt the user to enter the number to be searched.(c) Reads the integer values and makes sure it is a positive integer.(d) Prints the index of the integer. If the input is not available in...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
binarySearchLengths(String[] inArray, String search, int start, int end) This method will take in a array of...
binarySearchLengths(String[] inArray, String search, int start, int end) This method will take in a array of strings that have been sorted in order of increasing length, for ties in the string lengths they will be in alphabetical order. The method also takes in a search string and range of indexes (start, end). The method will return a String formatted with the path of search ranges followed by a decision (true or false), see the examples below. Example 1: binarySearchLengths({"a","aaa","aaaaa"},"bb",0,2) would...
Write MIPS assembly code for the following C code. for (i = 10; i < 30;...
Write MIPS assembly code for the following C code. for (i = 10; i < 30; i ++) { if ((ar[i] > b) || (ar[i] <= c)) ar[i] = 0; else ar[i] = a; }
Convert the following C++ code into MIPS assembely. For testing I will be change the values...
Convert the following C++ code into MIPS assembely. For testing I will be change the values for q,y,x with few different values. //q -> $s0 //y -> $s1 //x -> $s2 int main(){ int q = 5; int y = 17; int x = 77; if ( q < 10){ cout << "inside if"; } elseif ( x > 0 || y < 10) { cout << "inside elseif"; } else { cout << "inside else"; } }
Hello I am needing an example of how to write an assembly (MIPS) code that with...
Hello I am needing an example of how to write an assembly (MIPS) code that with will ask the user for two numbers then for addition or multiplication by typing in + or * into the command prompt. For example if I type in the number 2 and 5 then + The code should add the sum between the two numbers like 2 + 3 + 4 + 5 = 14. If multiplication is implemented then it will do the...
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...
Write a program in MIPS assembly language to convert an ASCII number string containing positive and...
Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a nullterminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register $v0. If a non-digit character appears anywhere in the string, your program should stop with...
Compile the following C code to MIPS assembly. a. Assume that i and j are stored...
Compile the following C code to MIPS assembly. a. Assume that i and j are stored in register $s1 and $s2, respectively. i = i – 2 + j; b. Assume base address of Array B and the variable i are stored in registers $s3 and $s1, respectively. B[1] = (B[0] x 4) - I; Explain each step in detail! my professor has the following answer: 1) addi $t0, $s1, -2 add $s1, $$t0, $s2 2) lw $t0, 0 ($s3)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT