Could someone help me? I need the following code in mips assembly language. I\'m
ID: 3628728 • Letter: C
Question
Could someone help me? I need the following code in mips assembly language. I'm not expecting you to do the whole thing, just guide me in the right direction, especially with the array which is given in the data segment. I don't understand that... The first value specified in the data segment is 9. The instruction is to use that value to calculate the high, low and mid points. (The remaining 9 integers are the values in the sorted array.) and the key should be inputted by the user...Please and Thank you!
BinarySearch(array[0..N-1], key)
{
low = 0
high = N - 1
while (low <= high)
{
mid = (low + high) / 2
if (array[mid] > key)
high = mid - 1
else if (array[mid] < key)
low = mid + 1
else
output "found at index mid" and exit
}
output "not found"
}
.data
.align 0
item: .word 9
.word 5
.word 9
.word 18
.word 34
.word 53
.word 76
.word 84
.word 97
.word 100
.align 0
str: .asciiz " Enter the search value : "
strf: .asciiz " Found at index : "
strnf:.asciiz " Not Found ! "
.text
.globl main
main: ....
Explanation / Answer
BinarySearch: # With array and taget as parameters
#push base address of array
push ebp
mov ebp, esp
mov ebx, [ebp + 8]
mov ecx, [ebp + 12]
xor edx, edx
dec ecx
jmp LoopCond
LoopStart:
mov eax, edx
add eax, ecx
shr eax, 1
push ecx
mov ecx, [ebp + 16]
cmp [eax * 4 + ebx], ecx
pop ecx
je Exit
jl UpperHalf
mov ecx, eax
dec ecx
jmp LoopCond
UpperHalf:
mov edx, eax
inc edx
LoopCond:
cmp ecx, edx
jge LoopStart
mov eax, -1
Exit:
pop ebp
ret
End BinarySearch