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 function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
what is an exception and how is it similar to a subroutine call? assembly 68000
what is an exception and how is it similar to a subroutine call? assembly 68000
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT