地址超出范围 - MIPS

时间:2015-11-19 23:18:51

标签: assembly runtime mips

我看过那些在堆栈溢出时提出这个问题的人,但是我似乎无法从对它们的回复中把任何东西拼凑起来,所以我只是问它特别关注我的代码。

我有一块(破碎的)MIPS代码应该是一个合并分区。但是,当我尝试运行它时,我收到此错误:路径错误\ Desktop \ mergesort.asm第8行:0x00003160的运行时异常:地址超出范围0xfffffffc

我无法弄清楚为什么保存返回地址会导致问题。任何帮助将不胜感激

#main method for mergesort

.data 0x0
.globl array
.globl b_array

num_of_el:  .word 4
array:      .space  3200
b_array:    .space  3200

#strings to be printed

comm_space: .asciiz ", "

string_1:   .asciiz "\nPlease enter the number of elements you'd like to sort (No more than 100).\n"
string_2:   .asciiz "\nPlease enter "
string_2b:  .asciiz " integers to be mergesorted, one per line:\n"
string_3:   .asciiz "\nHere is the sorted data:\n"

#start of code
.text 0x3000
.globl main

main:

    li  $v0, 4          #system call code to print
    la  $a0, string_1       #load address of first string
    syscall

    li  $v0, 5          #syscall to read int
    syscall
    move    $s0, $v0        #move from $t0 to $s0
    sw  $s0, num_of_el

    li  $v0, 4          #system call code to print
    la  $a0, string_2       #load address of second string part one
    syscall

    li  $v0, 1          #system call code to print int
    lw  $a0, num_of_el      #load word - number of elements
    syscall

    li  $v0, 4          #system call code to print
    la  $a0, string_2b      #load address of second string part two
    syscall

fill_arr:

    addi    $t0, $0, 0      #initialize array pointer

fill_array_loop:

    li  $v0, 5          #syscall to read int
    syscall
    move    $s1, $v0        #move from t0 to s0
    sw  $s1, array($t0)     #store into array at pointer

    addiu   $t0, $t0, 4     #increment counter
    lw  $t1, num_of_el      #load number of elements into t1
    add $t1, $t1, $t1       #double value of t1
    add     $t1, $t1, $t1       #quadruple value of t1 to compare to array pointer
    bne $t1, $t0, fill_array_loop

    addi    $a0, $0, 0      #array pointer low (0)
    lw  $t2, num_of_el      #load size of array
    sll $t2, $t2, 2     #multiply by 4 to get 
    addi    $a1, $t2, -4        #end address -4

    jal mergesort

    li  $v0, 4          #system call code to print
    la  $a0, string_3       #load address of second string part two
    syscall

print_array:

    addi    $t0, $0, 0      #initialize array pointer

print_array_loop:

    li  $v0, 1          #system call code to print int
    lw  $a0, b_array($t0)   #load word - element of array
    syscall

    li  $v0, 4          #system call code to print
    la  $a0, comm_space     #load address of second string part two
    syscall

    addiu   $t0, $t0, 4     #increment counter

    lw  $t1, num_of_el      #load number of elements into t1
    add $t1, $t1, $t1       #double value of t1
    add     $t1, $t1, $t1       #quadruple value of t1 to compare to array pointer
    bne $t1, $t0, print_array_loop

end_main:

    li  $v0, 10         #terminate programm
    syscall

第二个程序:

.text
.globl mergesort

mergesort:

    addi    $sp, $sp, -12           # Make room on stack for saving $ra and $fp
    sw      $ra, 0($sp)             # Save $ra
    sw  $a0, 4($sp)     # store low
    sw  $a1, 8($sp)     # store high

    beq $a0, $a1 end_mergesort  #only execute if low < high

    add $s0, $a1, $a0       #sum of low and high
    div $s0, $s0, 2     #divide by 2 (this is mid)

    add $a1, $s0, $0        #make argument2 the mid for mergesort call

    jal     mergesort

    lw  $a1, 8($sp)     # Load high
    add $s0, $a1, $a0       #sum of low and high
    div $s0, $s0, 2     #divide by 2 (this is mid)

    add $a0, $s0, 4     #make argument1 mid +4

    jal mergesort       # Call recursively on the second half of the array

    lw  $a0, 4($sp)     # Load low
    lw  $a1, 8($sp)     # Load high

    add $s0, $a1, $a0       #sum of low and high
    div $s0, $s0, 2     #divide by 2 (this is mid)

    add $a2, $0, $a1        #put high in argument3
    add $a1, $s0, $0        #put mid in argument 2

    jal merge           # Merge the two array halves

end_mergesort:

    lw  $ra, 0($sp)     # Load the return address from the stack
    lw  $a0, 4($sp)     # store low
    lw  $a1, 8($sp)     # store high
    addi    $sp, $sp, 12        # Adjust the stack pointer
    jr  $ra         # Return 

第三程序:

.text
.globl  merge

merge:
    addi    $sp, $sp, -4        # Adjust the stack pointer
    sw  $ra, 0($sp)     # Store the return address on the stack

    add $t0, $a0, $0        #i is low($a0)
    add $t1, $a1, 4     #j is mid + 4
    add $t2, $0, $0     #k points to b_array

while1:

    bgt $a1, $t0, while2    #exit loop if m>i
    bgt $t2, $a2, while2    #exit loop if j>h
    lw  $t3, array($t0)     #load array[i] into $t3
    lw  $t4, array($t1)     #load array[j] into $t4
    ble $t3, $t4, if        #if array[i] <= array[j]
    beq $0, $0, else        #jump to else

if:
    sw  $t3, b_array($t2)       #store array[i] into b_array[k]
    add $t0, $t0, 4         #increment i
    add $t2, $t2, 4     #increment k
    j   while1

else:
    sw  $t4, b_array($t2)       #store array[j] into b_array[k]
    add $t1, $t1, 4         #increment j
    add $t2, $t2, 4     #increment k
    j   while1

while2:

    bgt $a1, $t0, while3    #exit loop if m>i
    lw  $t3, array($t0)     #load array[i] into $t3
    sw  $t3, array($t2)     #store array[i] into b_array[k]
    add $t0, $t0, 4         #increment i
    add $t2, $t2, 4     #increment k
    j   while2

while3:

    bgt $t2, $a2, merge_end #exit loop if j>h
    lw  $t4, array($t1)     #load array[j] into $t4
    sw  $t4, b_array($t2)       #store array[j] into b_array[k]
    add $t1, $t1, 4         #increment j
    add $t2, $t2, 4     #increment k
    j   while1


merge_end:

    lw  $ra, 0($sp)     # Store the return address on the stack
    addi    $sp, $sp, 4     # Adjust the stack pointer

    jr  $ra         #return

0 个答案:

没有答案