Merge sort in MIPS Assembly help?
-
I have a program in MIPS assembly that is supposed to run a merge sort on a fixed list of integers. I seem to have hit a bug. My end list isn't correctly sorted and there are some 0s that are messing up the program. I am using a stack and am putting saved values into that stack. My main program creates a stack. [code] main: li $s0, 0 addi $s0, $s0, 0 # $s0=lo addi $s1, $s1, 19 # $s1=hi la $a0, list addi $s2, $s2, 0 addi $sp, $sp, -20 sw $ra, 16($sp) sw $a0, 12($sp) sw $s2, 8($sp) # m sw $s1, 4($sp) # hi sw $s0, 0($sp) # lo jal merge_sort merge_sort: slt $t0, $s0, $s1 # $t0=1 if lo<hi bne $t0, $zero, L1 # if lo<hi branch to L1 jr $ra L1: add $t0, $s0, $s1 # t0 = lo+hi div $s2, $t0, 2 # m=lo+hi/2 addi $sp, $sp, -20 sw $ra, 16($sp) sw $a0, 12($sp) sw $s2, 8($sp) # m sw $s1, 4($sp) # hi sw $s0, 0($sp) # lo add $s1, $s2, $zero # hi=m jal merge_sort # merge_sort(a(a0), ?, $s0, $ L2: addi $sp, $sp, -20 sw $ra, 16($sp) sw $a0, 12($sp) sw $s2, 8($sp) # m sw $s1, 4($sp) # hi sw $s0, 0($sp) # lo addi $s0, $s2, 1 # lo=m+1 jal merge_sort addi $sp, $sp, 20 ##pop stack ###load values for merge and jal to merge## /// I think I might be having a problem here//// lw $s0, 0($sp) lw $s1, 4($sp) lw $s2, 8($sp) lw $a0, 12($sp) jal merge /////Or Problem here/////// ####return from merge load values, pop and return to previous address (L2 most likely)### lw $s0, 0($sp) lw $s1, 4($sp) lw $s2, 8($sp) lw $a0, 12($sp) lw $ra, 16($sp) addi $sp, $sp, 20 jr $ra ######################################... Merge ######################################... merge: li $t2, 0 # i li $t3, 0 # j li $t4, 0 # k move $t2, $s0 # i=lo ##FOR LOOP FROM MERGE### ######################## for: slt $t1, $s1, $t2 # $t1=1;hi<i bne $t1, $zero, next #if i>hi next loop la $a0, list # array list into $a0 move $t5, $t2 # save iterator sll $t5, $t5, 2 # shift by 4 add $t6, $t5, $a0 # $t6=a[i] lw $t1, 0($t6) la $a0, b_array move $t7, $t2 sll $t7, $t7, 2 # shift 4 add $t7, $t7, $a0 sw $t1, 0($t7) # $b[i]=a[i] addi $t2, $t2, 1 j for ###START OF WHILE LOOP 1#### next: move $t2, $s0 # i=lo addi $t3, $s2, 1 # j=m+1 (Hopefully) move $t4, $s0 # k=lo while1: slt $t1, $s2, $t2 #$t1=1;m<i bne $t1, $zero, while2 #ifm<i branch to while2 slt $t1, $s1, $t3 #$t1=1;hi<j bne $t1, $zero, while2 #if j>hi branch to while2 ##### getting b[i] & storing into $s3 ####### move $t5, $t2 #preserve i into $t5 la $a0, b_array sll $t5, $t5, 2 add $t6, $t5, $a0 #load b[i] into $t6 lw $s3, 0
-
Answer:
I see 3 problems: 1.) You're not initializing the arguments registers that you probably need to pass to the merge function. 2.) In your merge function, you're calling lw when you should be calling sw to preserve your registers 3.) In your merge function, you don't have a line for storing your $ra register.
brian at Yahoo! Answers Visit the source
Other answers
I see 3 problems: 1.) You're not initializing the arguments registers that you probably need to pass to the merge function. 2.) In your merge function, you're calling lw when you should be calling sw to preserve your registers 3.) In your merge function, you don't have a line for storing your $ra register.
godfathe...
1)Where's the algorithm? 2)Where's the pseudocode? 3) CPU? 4) Which Assembler? for example, on x86 MASM 6 is different than TASM using IDEAL mode.
Therese
Related Q & A:
- What Is The Prediction Of Up Assembly Election 2012?Best solution by afternoondc.in
- How to download programming tools for Assembly?Best solution by assembly-language-programming.winsite.com
- What's the relationship between .NET assembly file size and memory usage?Best solution by Programmers
- Why is learning assembly language valuable to a company?Best solution by codeproject.com
- What sort of opportunities are there to help people in Africa or places where people need help?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.