为什么这些装配线会导致QtSpim崩溃?

时间:2018-04-27 21:51:58

标签: assembly mips qtspim

这是我必须描述子程序的代码块。打开文件时QtSpim崩溃了。我没有机会去运行它。

###########################################################
# Read Structure
# # #
# 1. Iterate through the array and read each element from user input
# 2. Each element should be a [double, integer] pair 
#       - Double: “price” 
#       - Integer: “count”
###########################################################

如果它有帮助,这是我未完成的程序的其余部分。在使用print语句隔离部分并删除块然后重新运行/崩溃QtSpim后,我才发现出了什么问题。

###########################################################
# Program 5 - Data Structure
# # #
# 1. Dynamically Allocate and fill Array of Data Structures 
# 2. Print Out Array
# 4. Iterate Array and calculate sum and average
#       - Value of each element = (price x count)
#       - Array Average = (total value / total count)
# 6. Iterate again to find the min and max values
# 7. Print sum, average, min and max value.

# #  Main's Order  # #
# Allocate Structure #
# Read Structure     #
# Print Structure    #
# Sum Average        #
# Get Min/Max        #
# # # # # # # # # # # 
###########################################################
#       Register Usage
#   $t0 
#   $t1
#   $t2 
#   $t3 
#   $t4 
#   $t5
#   $t6
#   $t7
#   $t8
#   $t9 Temp
###########################################################
        .data
# "sv" - Static Variable

ArrayAddress_sv:    .word   0   
ArraySize_sv:       .word   0   

###########################################################
        .text
main:

    addi $sp, $sp, -4   # Allocate space for $ra
    sw $ra, 0($sp)      # Store $ra onto stack

    addi $sp, $sp, -8   # Allocate space for |2-Arg's OUT(Array Base Address/Array Length)|

    jal allocate_structure

    lw $t0, 0($sp)      # Loading Array Address($sp+0) to $t0
    lw $t1, 4($sp)      # Loading Array Size($sp+4) to $t1

    addi $sp, $sp, 8    # De-Allocating 2 Arguments

    lw $ra, 0($sp)              # load $ra from stack
    addi $sp, $sp, 4            # deallocate word for $ra

    la $t9, ArrayAddress_sv     # Loading Address of Static Variable
    sw $t0, 0($t9)              # Storing Array Address($t0) into Static Variable(ArrayAddress_sv)

    la $t9, ArraySize_sv        # Loading Address of Static Variable
    sw $t1, 0($t9)              # Storing Array Size($t1) into Static Variable(ArraySize_sv)

# # # # # # # # # # # # # #
# Allocate Structure Done #
# # # # # # # # # # # # # #

    la $t9, ArrayAddress_sv # Loading Address of Static Variable
    lw $t0, 0($t9)          # Storing Variable(Array Address) into $t0
                                # # # RESTORING VARIABLES # # #
    la $t9, ArraySize_sv    # Loading Address of Static Variable
    lw $t1, 0($t9)          # Storing Variable(Array Length) into $t1 

    addi $sp, $sp, -4   # Allocate space for $ra
    sw $ra, 0($sp)      # Store $ra onto stack

    addi $sp, $sp, -8   # Allocate space for |2-Arg's IN(Array Base Address/Array Length)|

    sw $t0, 0($sp)      # Storing Array Address into $sp+0
    sw $t1, 4($sp)      # Storing Array Length into $sp+4

    jal read_structure

    addi $sp, $sp, 8    # De-Allocate space for |2-Arg's IN(Array Base Address/Array Length)|

    lw $ra, 0($sp)              # load $ra from stack
    addi $sp, $sp, 4            # deallocate word for $ra

# # # # # # # # # # # # 
# Read Structure Done #
# # # # # # # # # # # #

    la $t9, ArrayAddress_sv # Loading Address of Static Variable
    lw $t0, 0($t9)          # Storing Variable(Array Address) into $t0
                                # # # RESTORING VARIABLES # # #
    la $t9, ArraySize_sv    # Loading Address of Static Variable
    lw $t1, 0($t9)          # Storing Variable(Array Length) into $t1 

    addi $sp, $sp, -4           # allocate one word for $ra
    sw $ra, 0($sp)              # store $ra on stack

    addi $sp, $sp, -8   # Allocate space for |2-Arg's IN(Array Base Address/Array Length)|

    sw $t0, 0($sp)      # Storing Array Address into $sp+0
    sw $t1, 4($sp)      # Storing Array Length into $sp+4

    jal print_structure

    addi $sp, $sp, 8    # De-Allocate space for |2-Arg's IN(Array Base Address/Array Length)|

    lw $ra, 0($sp)              # load $ra from stack
    addi $sp, $sp, 4            # deallocate word for $ra

# # # # # # # # # # # # 
# Print Structure Done #
# # # # # # # # # # # #


    addi $sp, $sp, 4    # De-Allocating $ra

    li $v0, 10      #End Program
    syscall
###########################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
###########################################################
# Allocate Structure
# # #
# 1. Prompt user for an array size 
# 2. Dynamically allocate an array of data structures
# 3. Return array base address and array size to main
###########################################################
# Arguments In and Out of subprogram
#   $sp+0 Array Base Address(OUT)
#   $sp+4 Array Size(OUT)
###########################################################
# Register Usage
#   $t0 Array Size
#   $t1 12
###########################################################
        .data

InvalidSize_p:  .asciiz "\nThe size you entered was invalid."
ArraySize_p:    .asciiz "\nEnter size of the array: "
###########################################################
        .text
allocate_structure:

    li $v0, 4               # # #
    la $a0, ArraySize_p     # Print "Enter an array length: "
    syscall                 # # #

    li $v0, 5
    syscall

    blez $v0, invalid_size  # If entered length <= 0 display error

    move $t0, $v0

    li $t1, 12

    li $v0, 9
    mul $a0, $t0, $t1
    syscall

    sw $v0, 0($sp) # Saving $v0(Array Address) on stack
    sw $t0, 4($sp) # Saving $t0(Array Size) on stack

    jr $ra  #return to main

invalid_size:

    li $v0, 4               # # #
    la $a0, InvalidSize_p   # Print "\nThe size you entered was invalid."
    syscall                 # # #

    b allocate_structure
###########################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
###########################################################
# Read Structure
# # #
# 1. Iterate through the array and read each element from user input
# 2. Each element should be a [double, integer] pair 
#       - Double: “price” 
#       - Integer: “count”
###########################################################
# Arguments In and Out of subprogram
#   $sp+0 Array Base Address(IN)
#   $sp+4 Array Size(IN)
###########################################################
# Register Usage
#   $t0 Array Address
#   $t1 Array Size
#   $t2 Counter
###########################################################
        .data
Price_p:    .asciiz "\n Enter Price(Double) for Item "
Count_p:    .asciiz "\n Enter Count(Integer) for Item "
Input_p:    .asciiz " : "
InvalidRead_p:  .asciiz "\n The number you entered is invalid"
###########################################################
        .text
read_structure:

    lw $t0, 0($sp)      # Loading Array Address($sp+0) to $t0
    lw $t1, 4($sp)      # Loading Array Size($sp+4) to $t1

    li $t2, 1

read_structure_loop:

    li $v0, 4           # # #
    la $a0, Price_p     # Print "\n Enter Price(Double) for Item "
    syscall             # # #

    li $v0, 1           # # #
    move $a0, $t2       # Print Counter Integer
    syscall             # # #

    li $v0, 4           # # #
    la $a0, Input_p     # Print " : "
    syscall             # # #


    li $v0, 7   # Read Double
    syscall

    #c.le.d $f0, $zero  # Double is <= Zero
    #bc1t invalid_read  # # #

    li $v0, 4           # # #
    la $a0, Price_p     # Print "\n Enter Count(Integer) for Item "
    syscall             # # #

    li $v0, 1           # # #
    move $a0, $t2       # Print Counter Integer
    syscall             # # #

    li $v0, 4           # # #
    la $a0, Input_p     # Print " : "
    syscall             # # #

    li $v0, 5           # Read Int
    syscall

    blez $v0, invalid_read  # Int is <= zero

    s.d $f0, 0($t0)     # Store Double into array

    sw $v0, 8($t0)      # Store Int into Array

    addi $t0, $t0, 12   # Move Pointer 
    addi $t2, $t2, 1    # Increment Counter

    blt $t2, $t1, read_structure_loop # If counter < array size, then loop again

    jr $ra  # Return to main

invalid_read:

    li $v0, 4               # # #
    la $a0, InvalidRead_p   # Print "\n The number you entered is invalid"
    syscall                 # # #

    b read_structure_loop
###########################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
###########################################################
# Print Structure
# # #
# 1. Iterate the array and print the content of each element "[double, integer]
###########################################################
# Arguments In and Out of subprogram
#   $sp+0 Array Base Address(IN)
#   $sp+4 Array Size(IN)
###########################################################
# Register Usage
###########################################################
        .data
###########################################################
        .text
print_structure:

    jr $ra  #return to 
###########################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
###########################################################
# Sum Average 
# # #
# 1. Iterate the array and calculate the sum and average
# 2. Value is (price x count)
# 3. Average is (total value / total count)
###########################################################
# Arguments In and Out of subprogram
#   $sp+0  Array Base Address(IN)
#   $sp+4  Array Size(IN)
#   $sp+8  Sum Calculated Sums in data structure array(OUT)
#   $sp+16 Average of calculated sums in data structure array(OUT)
###########################################################
# Register Usage
###########################################################
        .data
###########################################################
        .text
sum_average:

    jr $ra  #return to main
###########################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
###########################################################
# Get Min/Max
# # #
# 1. Iterate the array and find minimum value and maximum value
#       - Total Value = (price x count)
###########################################################
# Arguments In and Out of subprogram
#   $sp+0  Array Base Address(IN)
#   $sp+4  Array Size(IN)
#   $sp+8  Min Value(OUT)
#   $sp+16 Max Value(OUT)
###########################################################
# Register Usage
###########################################################
        .data
###########################################################
        .text
get_min_max:

    jr $ra  #return to main
###########################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
###########################################################

0 个答案:

没有答案