2012-02-10 99 views
0

我想在MIPS中使用冒泡排序對整數數組進行排序,但是每當我運行冒泡排序時,我都會得到一個超出範圍錯誤的地址。我一直盯着代碼幾個小時,不知道爲什麼會發生這種情況。希望「有一點非常明顯,有更多經驗的人可以看到並幫助我解決問題。 程序的要點是簡單地讀取整數和符號(股票價格和股票代碼),然後根據價格進行排序。下面是代碼:泡沫排序MIPS

.data 
welcome: .asciiz "Welcome!\n" 
prompt1: .asciiz "Enter how many stocks you have.\n >" 
prompt2: .asciiz "Enter the four character NASDAQ abbrevation and price for each stock.\n" 
prompt3p1: .asciiz "You have entered " 
prompt3p2: .asciiz " stock abbreviations and prices. How many stocks do you want to buy:\n" 
prompt4: .asciiz "How many stocks do you want to sell:\n" 
InfoPrompt: .asciiz "Here are the symbols and the corresponding numbers that were entered: \n" 
numStocks: .word 0 
stockPrice: .space 40 # this will be the list of the stock prices 
stockSymbol: .word 0:20 # this will be for the lit of stock abbrevs 
symbols: .space 12 #this should be used for a 40 character array 
inputFormat: .asciiz "\n> " 
length: .word 0 
buffer: .space 4 
.globl main 
.text 

main: # display all of the propts 

    li $v0, 4 # get ready to print welcome 
    la $a0, welcome 
    syscall  # print welcome 

    li $v0, 4 # not sure this is necessary since it was already loaded into v0 before 
    la $a0, prompt1 
    syscall  # print("enter how many stocks you have.") 

    li $v0, 5 # this will get ready to accept the number of stocks 
    syscall  # this should store the number into the $v0 register 
    sw $v0, numStocks # store in memory so we dont lose it 
    move $t0, $v0  # this will hold the num of stocks 
    #before we go into the procedure we need to declare and move the stack for the $t registers. 
    la $t1, symbols 
    la $t2, stockPrice 
    sub $sp, $sp, 8 
    sw $t1, 0($sp) 
    sw $t2, 4($sp) 
    jal getInfo 
    # now that we have all the information lets run a test to see how successful we were 
    # jal printInfo 
    li $v0, 4 #get ready to ask how many items you want to buy 
    la $a0, prompt3p1 
    syscall 
    li $v0, 1 
    lw $a0, numStocks # prints the number of stocks in the portfolio 
    syscall 
    li $v0, 4 
    la $a0, prompt3p2  
    syscall 
    # we need to now get the number of stocks the person wants to buy 
    li $v0, 4 
    la $a0, inputFormat 
    syscall  #format the next input 
    li $v0, 5 
    syscall 
    move $t3, $v0 # the number of stocks we want to buy is now stored as $t3 
    #we need to get how many they want to buy 
    li $v0, 4 
    la $a0, prompt4 
    syscall 
    li $v0, 4 
    la $a0, inputFormat 
    syscall  #format the next input 
    li $v0, 5 
    syscall 
    move $t4, $v0 # the number of stocks that you want to sell 
    # now we have to sort the list to figure out what elements we are going to sell and buy 
    la $a2, stockPrice 
    la $a0, stockPrice 
    la $a1, numStocks 
    jal buble_sort 
    jal printInfo 
    # end program 
     li  $v0,10   #load the syscall number for terminating 
     syscall     #terminate 
#################################################################### 
# This will iterate for the number of stocks 
# Only accepts the number of stocks 
#################################################################### 
getInfo:  
    sub $sp, $sp, 8 
    sw $ra, 0($sp) # store the return value 
    sw $t0, 4($sp) # Save the t registers that we will be using 
    li $v0, 4 # set up the first call to initialize the calls for the abbreviations and numbers 
    la $a0, prompt2 
    syscall 
    # we want to have a place to store the symbols 
      la $t1, symbols 
      la $t2, stockPrice 
GI_loop: 
    beq $t0, $zero, GI_loop_done # if the counter == 0 then we are done 
    li $v0, 4 
    la $a0, inputFormat 
    syscall  #format the next input 
    # ask for the string input 
    li $v0, 8 
    la $a0, 0($t1) 
    li $a1, 6 
    syscall 
    #store the value in the array 
    addi $t1, $t1, 6  #increment our "array" 
    # ask for the integer input 
    li $v0, 4 
    la $a0, inputFormat 
    syscall  #format the next input 
    li $v0, 5 
    syscall  # get the integer value that we require 
    sw $v0, 0($t2) #store the value 
    addi $t2, $t2,4 #increment our counter 

    addi $t0, $t0, -1 # decrement our counter 
    j GI_loop 
GI_loop_done: 
    lw $ra, 0($sp) 
    lw $t0, 4($sp) 
    lw $t1, 8($sp) 
    lw $t2, 12($sp) 
    add $sp, $sp, 8 
    jr $ra 
#################################################################### 
# This will go through the lists and print out what was stored 
# will go through the symbols then the numbers 
#################################################################### 
printInfo: 
    sub $sp, $sp, 12 
    sw $ra, 0($sp) 
    sw $t0, 4($sp) # this will store the number of stocks that were entered 
    sw $t1, 8($sp) 
    li $v0, 4 
    la $a0, InfoPrompt 
    syscall 
    # we know that $t0 stores the correct number that was originally enetered so we need to loop through and print all the integers 
InfoLoop: 
    beq $t0, $zero, InfoLoopDone # a basic counter check 
    #li $v0, 4 
    #la $a0, 0($t1) 
    #syscall 
    #addi $t1, $t1, 6 
    #addi $t0, $t0, -1 
    #j InfoLoop 
    ################################### INTEGER PRINT WORKING 
    li $v0, 1   # this will print out the integers 
    lw $a0, 0($t2)   # we have to load the world that is found in the address of $t2 
    syscall 
    addi $t2,$t2, 4 # this will increment the array 
    addi $t0, $t0, -1 ## this will fix our counter 
    j InfoLoop 
InfoLoopDone: 
    lw $ra, 0($sp) 
    lw $t0, 4($sp) 
    add $sp, $sp, 8 
    jr $ra 
################################ 
# BUBBLE SORT 
################################ 
buble_sort: 
#a0=address of table 
#a1=sizeof table 
add $t0,$zero,$zero #counter1(i)=0 

loop1: 
addi $t0,$t0,1 #i++ 
bgt $t0,$a1,endloop1 #if t0 > a1 break; 

add $t1,$a1,$zero #counter2=size=6 
loop2: 

bge $t0,$t1,loop1 #j < = i 

#slt $t3,$t1,$t0 
#bne $t3,$zero,loop1 

addi $t1,$t1,-1 #j-- 

mul $t4,$t1,4 #t4+a0=table[j] 
addi $t3,$t4,-4 #t3+a0=table[j-1] 
add $t7,$t4,$a2 #t7=table[j] 
add $t8,$t3,$a2 #t8=table[j-1] 
lw $t5,0($t7) 
lw $t6,0($t8) 

bgt $t5,$t6,loop2 

#switch t5,t6 
sw $t5,0($t8) 
sw $t6,0($t7) 
j loop2 

endloop1: 
jr $ra 

回答

4

您的問題是在聲明中呼籲您的冒泡排序程序之前:

la $a1, numStocks 

它將載入其中保存的股票數量的地址,而不是股票的數量本身。

你應該

lw $a1, numStocks 
改變