MIPS Assembly programming

posted by .

Hi there,
I need to implement in assembly mips an iterative binary search method.
Here's my code:
# this program implements binary search

# the equivalent pseudo code is the following:

# first = 0
# last = size -1
# while (last - first > 1) {
# mid = (last-first)/2 + first
# if A[mid] == val
# break;
# if A[mid] > val
# last = mid;
# continue
# else
# first = mid;
# continue;
# }

#-----------------------------------------------------

.data 0x10000000

size: .word 0x0000000a # array size

.data 0x10000004

search: .word 0x0000000d # search term

.data 0x10000008

result: .word 0xffffffff # result = -1

.data 0x10000100

array: .word 0x00000001 # the array
.word 0x00000005
.word 0x00000007
.word 0x00000009
.word 0x0000000b
.word 0x0000000d
.word 0x00000010
.word 0x00004000
.word 0x00050000
.word 0x00700000

.text 0x0400000

main:
sw $t0, 0 # $t0 = 0, that's our "first" pointer
sw $t1, size # $t1 - size
addi $t1, $t1, -1 # $t1 = size - 1, our "last" pointer
j condition # goto condition
nop

condition:
sub $t2, $t1, $t0 # $t2 = last - first
bgt $t2, 1, while # if ($t2 > 1) goto while
nop
j exit # if not, goto exit
nop

while:
div $t3, $t2, 2 # $t3 = (last - first) / 2
add $t3, $t3, $t0 # $t3 = t3 + first
lw $t5, array($t3) # $t5 = array($t3)
lw $t6, result # $t6 = result
beq $t6, $t5, found # if value found, goto found
nop
bgt $t5, $t6, isGreater # if array[$t3] > result, goto isGreater
nop
addi $t0, $t3, 0 # else, first = mid
j condition # check the condition and start over the loop

found:
sw $t3, result # result = $t3
j exit # goto exit
nop

isGreater:
addi $t1, $t3, 0 # else, last = mid
j condition # check the condition and start over the loop

exit: sw $t4, 4($t6) # result = $t4
j end # jump to end
nop

end: j end # internal loop to end the program

however when I try to run it I get errors of bad data.
Any ideas?

  • MIPS Assembly programming -

    dumb

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. math 12

    How many different binary numbers can be present by a string of binary code with 5 digits if the first digit is 1 and the last two digits are 0?
  2. math

    the binary system is used for computer programming. a binary number consists of a string of digits that are either 0s or 1s a. if a string of binary code is 5 digits long, how many binary numbers are possible if the first digit is …
  3. programming 2

    Write a program that inputs a word representing a binary number (0s and 1s). First, your program should verify that it is indeed a binary number, that is, the number contains only 0s and 1s. if that is not the case, your program should …
  4. Programming

    Here is the question that I need help to write the code for the following program in Visual Basic 2010. I am trying to write the code but it has a run time error. If your could helps me to write the code it would be greatly appreciated. …
  5. programming MIPS ask

    Write a MIPS assembly language program that will cover the following steps: Prompt the user to enter a first integer named i1 Prompt the user to enter a second integer named i2 Compute i1+i2-200 Print the value of the result Repeat …
  6. programming MIPS ask 3

    Write a MIPS assembly language program for the following for loop: Pseudocode: int x; # for(x=10; x>0; x--){ # printf("x=%d", 4x); #} In the program use the slt or slti instruction and sll
  7. Computer Literary

    Which of the following is Not an example of debugging A)Looking for errors in programming code B)Fixing misspelled commands in programming code C)Solving faulty logic in programming code D)Upgrading to improve the programming code
  8. programming ( qbasic)

    i was given this program by my teacher. it converts decimal number to binary number.. Why is that FIX kept in front of (n/2) ...6th line of the program. why is that FIX written and why?
  9. cs203

    Write a MIPS assembly language program that displays the binary string of the content of register $t0.
  10. Assembly language

    For this project, you will write a MIPS assembly language program to calculate periodically compounding interest. Your program should prompt the user to enter the necessary parameters for the calculation and continue to loop until …

More Similar Questions