Arrays 复制数组进行排序在MIPS程序中无法正常工作?

Arrays 复制数组进行排序在MIPS程序中无法正常工作?,arrays,mips,mars-simulator,Arrays,Mips,Mars Simulator,我正在开发一个排序程序,对一个整数数组进行排序,我将用户输入的数组复制到一个新数组中进行变异。然而,我不确定我是否做得正确 .globl main .data input: .asciiz "Enter the size of the array: \n" entries: .asciiz "Enter the elements of the array, one line at a time: \n" output: .asciiz "Original array and then

我正在开发一个排序程序,对一个整数数组进行排序,我将用户输入的数组复制到一个新数组中进行变异。然而,我不确定我是否做得正确

.globl main

.data 

input: .asciiz "Enter the size of the array: \n"

entries: .asciiz "Enter the elements of the array, one line at a time: \n"

output: .asciiz "Original array and then sorted array: \n"

space: .asciiz " "


.text

main:

   subi $sp, $sp 32

   sw $ra, 0($sp)

   sw $t0, 4($sp) # the size of the array

   sw $t4, 8($sp) # the number 4

   sw $t1, 12($sp) # temporary

   sw $t2, 16($sp) # array original

   sw $t3, 20($sp) # specific element

   sw $s1, 24($sp) # copied array

   sw $t5, 28($sp) # number to copy

   la $a0, input

   li $v0, 4

   syscall

   # get the size

   li $v0, 5
   syscall
   move $t0, $v0

  # allocate space for the array on the heap
  li $t4, 4
  mul $t1, $t0, $t4
  li $v0, 9
  move $a0, $t1
  syscall
  move $t2, $v0

  li $s0, 0
  la $a0, entries
  li $v0, 4
  syscall

read_array:
  # read element
  li $v0, 5
  syscall
  move $t3, $v0

  # place in right address
  mul $t1, $s0, $t4
  add $t1, $t2, $t1
  sw $t3, 0($t2)

  addi $s0, $s0, 1
  blt $s0, $t0, read_array

  li $s0, 0

gnome_sort: 
  # allocate space on heap for copy
  mul $t1, $t0, $t4
  li $v0, 9
  move $a0, $t1
  syscall
  move $s1, $v0

  mul $s2, $t4, $t0
  add $s3, $s1, $s2

copy_array:
  lw $t5, 0($t2)
  sw $t5, 0($s1)

  add $t2, $t2, $t4
  add $s1, $s1, $t4

  blt $s1, $s3, copy_array

  li $s0, 0
  while_loop:
  bgt $s0, $t0, finish_sort
  beq $s0, $zero, increase_i
  sw $s4, 0($s1)
  sw $s5, -4($s1)
  bge $s4, $s5, increase_i
  j swap_elements

  increase_i:
  addi $s0, $s0, 1
  j while_loop              

  swap_elements:
  la $a0, input
  li $v0, 4
  syscall

  sw $t6, 0($s1)
  sw $t7, -4($s1)
  lw $t7, 0($s1)    
  lw $t6, -4($s1)
  subi $s0, $s0, 1

  j while_loop

  finish_sort:
  li $s0, 0

  la $a0, output
  li $v0, 4
  syscall
  j print_original

print_original:

  bge $s0, $t0, print_sorted
  lw $s6, 0($t2)

  li $v0, 1
  move $a0, $s6
  syscall

  la $a0, space
  li $v0, 4
  syscall

  addi $s0, $s0, 1
  j print_original

print_sorted:
  li $s0, 0
  loop:
  bge $s0, $t0, finish
  lw $s6, 0($s1)

  li $v0, 1
  move $a0, $s6
  syscall

  la $a0, space
  li $v0, 4
  syscall

  addi $s0, $s0, 1
  j loop
finish:
  li $v0, 10
  syscall       

在QTSpim中测试之后,您似乎在
sw$s4,0($s1)
while\u循环中
。这是因为此时
$s1
已超过您分配的第二个数组的末尾。如果要使用该内存,必须对其进行
syscall

使用
lw
sw
复制阵列的方法是正确的。

Syscall的确切用途是什么?Sbrk?是的,但我认为您只需要重置
$s1
以指向该数组的开头。您通常只需使用
移动
。诀窍是将指向数组开头的指针保存在一个寄存器中,然后将指针(使用
move
)复制到另一个寄存器中。这样,您总是有一个头指针和一个当前指针。头指针可以始终停留在数组的头上,而当前指针可以遍历数组。当您需要再次遍历阵列时,可以
从磁头寄存器移回当前寄存器。这就像在高级语言中迭代链表一样,指针只是一个32位数字(表示内存地址)。每当您执行诸如
lw$r10($r2)
之类的内存操作时,您都在将数据从内存地址
$r2
读取到寄存器
$r1
$r2
在这种情况下保存指针。在循环之前执行0($s1)是否可以访问头指针?还是有点迷路。