MIPS 中的冒泡排序

发布于 2025-01-04 10:22:14 字数 6163 浏览 1 评论 0原文

我正在尝试使用冒泡排序对 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

I am trying to sort an array of integers in MIPS using bubble sort but every time that I run bubble sort I get an address out of range error. I have been staring at the code for hours and have no idea why this is happening. Hopefully' it is something really obvious that someone with more experience can see and help me fix.
The point of the program is simply to read in integers and symbols (price of stock, and stock symbol) and then sort those based on the prices. Here is the code:

.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间☆小暴躁 2025-01-11 10:22:14

您的问题出在调用冒泡排序例程之前的声明中:

    la $a1, numStocks

它将加载保存股票数量的地址,而不是股票数量本身。

你应该改变它

    lw $a1, numStocks

Your problem is in the statement before calling your bubble sort routine:

    la $a1, numStocks

It will load the address where the number of stocks are saved, not the number of stocks itself.

You should change it with

    lw $a1, numStocks
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文