2011-04-10 5 views
0

이 프로그램의 목적은 짝수 개의 사용자 입력 정수를 가져 와서 병합 정렬을 사용하여 정렬하는 것입니다. 정렬 된 목록을 화면에 인쇄합니다. 프로그램은 먼저 설정된 크기를 취한 다음 정수를 묻습니다. 그것들을 두개의 분리 된 세트들로 나눠서 여기로부터 오름차순으로 정렬합니다. 두 세트가 모두 정렬되면 프로그램은 두 세트를 최종 정렬 된 세트에서 함께 병합합니다. 마지막 세트가 화면에 인쇄됩니다. 출력은 0의 목록 일뿐입니다. 내 잘못이 어디에 있는지 확실하지 않아서 새로운 눈을 사용할 수 있습니다. 감사.MIPS 어셈블러의 배열로 병합

.data 
msg1: 
    .asciiz "Please Enter the the total even number of integers in the list: " 
    .align 2 
msg2: 
    .asciiz "\nPlease Enter an integer: " 
    .align 2 
msg3: 
    .asciiz "\nSorted list of integers is: " 
    .align 2 
coma: 
    .asciiz ", " 
    .align 2 
set1: 
    .space 69 
    .align 2 
set2: 
    .space 69 
    .align 2 
set3: 
    .space 138 
    .align 2 

    .text 
    .globl main 
main: 
#Get input for size of sortedSet, divide it in half to get size of two 
#final sorted sets to merge and store them into arguements 
    li $v0, 4 
    la $a0, msg1    #prints message1 to screen 
    syscall 
    li $v0, 5 
    syscall     #inputed value is number of elements 
    move $s3, $v0 
    beqz $s3, main    #check if inputed value is greater than zero 
    li $t2, 2 
    div $s3, $t2    #divides size of final array by 2 
    mflo $s4    #stores quotient as size of smaller arrays 

Enter: 
#Get input of each integer in the list, after each entered integer itterator is increased by 1. 
#input will be saved into set1,when size is equal set1size input will be saved into set2. 
    beq $t4, $s4, Enter2   #branch to set2 when set1 is full 
    li $v0, 4 
    la $a0, msg2 
    syscall     #print message2 to screen, taking input for set1 
    li $v0, 5 
    syscall     #takes inputed user value 
    move $a1, $v0    
    la $a0, set1 
    move $t2, $t4 
    sll $t2, $t2, 2    #times itterator by 4 
    add $t1, $t2, $a0 
    sw $a1, ($t1)    #stores user value as current element of set1 
    addi $t4, $t4, 1   #$t4 is itterator for entering loop. Increase by 1 to move to next space in set1 
    j Enter 
Enter2: 
    beq $t5, $s4, step   #branch to step after both sets are full 
    li $v0, 4 
    la $a0, msg2 
    syscall     #print message2 to screen, taking input for set2 
    li $v0, 5 
    syscall     #takes inputed user value 
    move $a1, $v0 
    la $a0, set2 
    move $t2, $t5 
    sll $t2, $t2, 2    #times itterator by 4 
    add $t1, $t2, $a0 
    sw $a1, ($t1)    #stores user value as current element of set2 
    addi $t5, $t5, 1   #$t5 is itterator for second entering loop. Increase by 1 to move to next spot in set2 
    j Enter2 

step: 
#resets itterators for safety/to use again 
    li $t4, 0 
    li $t5, 0 
sort: 
#sort each individual list to create two sorted list in ascending order for the final 
# merge-sort with two lists. 
#first set 
    beq $t4, $s4, next   #branch to next (second set) when itterator is equal to size of set1 
    la $a0, set1     
    move $t2, $t4    #move current spot into $t2 to preserve itterator 
    sll $t2, $t2, 2    # times by 4 
    add $t1, $t2, $a0   #get address of first element 
    lw $s1, ($t1)    #load first element into $s1 
    move $t2, $t4    
    addi $t2, $t2, 1   #add 1 to itterator to get next element in set 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    lw $s2, ($t1)    #load next element in set to $s2 
    bge $s1, $s2, switch   #if set1[$s1] >= set1[$s2] goto label switch 
    addi $t4, $t4, 1   #add 1 to itterator 
    j sort 
switch: 
    la $a0, set1    #switch set1[$s1] and set1[$s1+1] 
    move $t2, $t4 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    sw $s2, ($t1)    #store $s2 into the $s1 spot 
    move $t2, $t4    
    addi $t2, $t2, 1     
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    sw $s1, ($t1)    #store $s1 into the $s2 spot 
    addi $t4, $t4, 1   #add 1 to itterator 
    j sort 

next: 
    beq $t5, $s4, merge   #branch to merge when itterator equals set2 size 
    la $a0, set2    #load address of set2 
    move $t2, $t5    #move itterator to $t2 to preserve it 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    lw $s1, ($t1)    #load element into $s1 
    move $t2, $t5 
    addi $t2, $t2, 1   #add 1 to itterator for next element 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    lw $s2, ($t1)    #load next element into $s2 
    bge $s1, $s2, switch2   #if set2[$t5] >= set2[$t5+1] goto label switch2 
    addi $t5, $t5, 1   #add 1 to itterator 
    j next 
switch2: 
    la $a0, set2    #switch set2[$s2] and set2[$s2+1] 
    move $t2, $t5    
    addi $t2, $t2, 1   
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    sw $s1, ($t1)    #store $s1 into $s2 spot 
    move $t2, $t5 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    sw $s2, ($t1)    #store $s2 into $s1 spot 
    addi $t5, $t5, 1   #add 1 to itterator 
    j next 

merge: 
#resets itterators for safety/to use again 
    li $t4, 0    # = set1 itterator 
    li $t5, 0    # = set2 itterator 
    li $t7, 0    # = set3 itterator 
while: 
#while($s1 <= $s4 || $s2 <= $s4) do{ 
# if($s1 == $s4){ set3[$s3] = set2[$s2]; addi $s2, 1; addi $s3, 1; 
# j while} 
# if($s2 == $s4){ set3[$s3] = set1[$s1]; addi $s1, 1; addi $s3, 1; 
# j while} 
# if(set1[$s1] > set2[$s2]){ set3[$s3] = set1[$s1]; addi $s1, 1;} 
# else{ set3[$s3] = set2[$s2]; addi $s2, 1;} 
# addi $s3, 1; 
#} return 
# break 

    beq $t4, $s4, print   #1st part of while conditional, branch to print if $t4 = $s4 
here: 
    beq $t4, $s4, if1   #1st if statement, branch to if1 loop if $t4 = $s4 
    beq $t5, $s4, if2   #2nd if statement, branch to if2 loop if %t5 = $s4 

    la $a0, set1    #3rd if statement 
    move $t2, $t4 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    lw $s1, ($t1)    #load first element from set1 into $s1 
    la $a0, set2 
    move $t2, $t5 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    lw $s2, ($t1)    #load first element from set2 into $s2 
    slt $t6, $s2, $s1   #returns 1 if $s1 > $s2 
    bnez $t6, if3    #if $t6 =/= 0, branch to if3 loop 
    beqz $t6, else3    #if $t6 = 0, branch to else3 loop 

if1: 
#branch for $s1 == $s4 
# set3[$t7] = set2[$t4]; 
    la $a0, set2    #load set2 address 
    move $t2, $t4 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    lw $s1, ($t1)    #load element at $t4 in set2 to $s1 
    la $a0, set3    #load set3 
    move $t2, $t7 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    sw $s1, ($t1)    #store $s1 into $t7 position in set3 
    addi $t4, $t4, 1   #add 1 to $t4 itterator 
    addi $t7, $t7, 1   #add 1 to $t7 itterator 
    j while 


if2: 
#branch for $s2 == $s4 
# set3[$t7] = set1[$t4]; 
    la $a0, set1    #load set1 address 
    move $t2, $t4    #load itterator into $t2 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    lw $s1, ($t1)    #load element in position $t4 into $s1 
    la $a0, set3    #load set3 address 
    move $t2, $t7 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    sw $s1, ($t1)    #store $s1 into position $t7 in set3 
    addi $t4, $t4, 1   #move to next position in set1 
    addi $t7, $t7, 1   #move to next position in set3 
    j while 

if3: 
#branch for set1[$t4] > set2[$s2] 
# set3[$t7] = set1[$t4]; 
    la $a0, set1    #load set1 address 
    move $t2, $t4    #move set1 itterator 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    lw $s1, ($t1)    #load element at $t4 of set1 into $s1 
    la $a0, set3    #load set3 address 
    move $t2, $t7    #load set3 itterator 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    sw $s1, ($t1)    #store $s1 into set3 at position $t7 
    addi $t4, $t4, 1   #move to next position in set1 
    addi $t7, $t7, 1   #move to next position in set3 
    j while 

else3: 
#branch for set1[$s1] < set2[$t5] 
# set3[$t7] = set2[$t5]; 
    la $a0, set2    #load set2 address 
    move $t2, $t5    #move set2 itterator to $t2 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    lw $s2, ($t1)    #load element in position $t5 of set2 into $s2 
    la $a0, set3    #load set3 address 
    move $t2, $t7    #move set3 itterator 
    sll $t2, $t2, 2 
    add $t1, $t2, $a0 
    sw $s2, ($t1)    #store element $s2 into position $t7 in set3 
    addi $t5, $t5, 1   #move to next position in set2 
    addi $t7, $t7, 1   #move to next position in set3 
    j while 

print: 
#itterate through sortedSet and print it to console then exit 
    blt $t5, $s4, here   #go to label here if 2nd part of while conditional is not met 
    li $v0, 4 
    la $a0, msg3    #load msg3 
    syscall     #print to screen 
    la $t3, set3    #load addresss of set3 
    move $t2, $s3    #move set3 size into $t2 
    li $t7, 0    #load 0 into set3 itterator 
print2: 
    sll $t7, $t7, 2 
    add $t1, $t7, $t3 
    li $v0, 1 
    lw $a0, ($t3)    #load element at position $t2 of set3 into $a0 
    syscall     #print to screen 
    li $v0, 4 
    la $a0, coma    #load address of coma 
    syscall     #print screen 
    addi $t7, $t7, 1   #move to next element in set3 
    addi $t2, $t2, -1   #subtract 1 from set3 size 
    bnez $t2, print2   #if set3 size =/= 0, branch to print2 

    li $v0, 10 
    syscall     #exit program 

답변

0

안녕하세요. 코드와 동일한 문제가 있었지만 광산은 인쇄하기 전에 배열 간격 주소를로드하는 것과 관련이있었습니다. 나는 당신이 다시 set3을로드 할 수 있지만 아마도 문제는 실제 값을 레지스터에 저장하거나 올바른 레지스터를로드하는 것입니다.