首页 > 解决方案 > MIPS 1D 整数数组,在索引处插入

问题描述

我是 MIPS 的新手,我的程序遇到了一些问题。

现在我的代码可以在指定索引处将一个整数值插入到数组中。但是目前,当插入整数时,它会替换先前存在的值。如何在所述索引之后移动所有元素,以便数组的长度增加 1 并且不会丢失任何值?

请注意,我使用一个特殊的整数 -22 来表示我的数组的结尾。

任何帮助将不胜感激!谢谢!

下面是我的代码:

.data
    beginarray: .word 1, 2, 3, 4, -22   
   array: .space 4000
   str_command: .asciiz 

   str1:    .asciiz "\nEnter an Integer:    "
str2:   .asciiz "\nAt what index:   "

    .text
    .globl main



main:
    # get length
    la $a0, beginarray
    jal length
    addi $s0, $v0, 0    # $s0 = returned length

    # copy beginarray into array
    la $a0, beginarray
    la $a1, array
    jal copyarray

    
    jal insert 
    
    # print array
    la $a0, array
    jal printarray
    

    li $v0,10
    syscall



# takes pointer to array as input
length:       addi $t0, $zero, -22      # $t0 = -22
          addi $v0, $zero, 0        # $v0 = 0   (length)
          add $t1, $a0, $zero       # $t1 = pointer to first int

calclength:   lw $t2, 0($t1)            # $t1 = value of current int

          beq $t2, $t0, returnlength    # if $t1 = -22
          
          addi $v0, $v0, 1          # increment length
          addi $t1, $t1, 4          # move pointer to next int

          j calclength          # loop

returnlength: jr $ra



# takes two pointers as input (pointer to first array and pointer to second array)
# copies contents of first array into second array
copyarray:    addi $t0, $a0, 0          # $t0 = pointer to array1
         # addi $t1, $a1, 0         # $t1 = pointer to array2
          addi $t4, $zero, -22      # $t4 = -22

calccopy:    lw $t2, 0($t0)         # $t2 = value of array1
         #lw $t3, 0($t1)            # $t3 = value of array2

          sw $t2, 0($a1)                # store int in array

          beq $t2, $t4, returncopy      # if $t2 = -22

          addi $t0, $t0, 4          # move a1 pointer to next int
          addi $a1, $a1, 4          # move a2 pointer to next int

          j calccopy

returncopy:   jr $ra



# print array
printarray: add     $t1, $a0, $zero     # $t1 = pointer to first int
        addi    $t0, $zero, 0           # set counter to zero

printloop: beq     $t0, $s0, done

    # load word from addrs and goes to the next addrs
    lw      $t2, 0($t1)
    addi    $t1, $t1, 4

    # syscall to print value
    li      $v0, 1      
    move    $a0, $t2
    syscall

    # syscall for printing space
    li      $a0, 32
    li      $v0, 11  
    syscall
    
    #increment counter
    addi    $t0, $t0, 1
    j      printloop

done: jr $ra

insert: 
    li $v0, 4           # system call code for print_str

    la $a0, str1
    syscall

        li $v0 5    # system call code for read_int
        syscall
        
        move $t3, $v0       # $t3 = integer entered

    li $v0, 4           # system call code for print_str
    la $a0, str2
    syscall

    li $v0 5    # system call code for read_int
        syscall
        
        move $t1, $v0       # $t1 = index entered

    li   $t0, 0          # $t0 = counter
        la   $t2, array      # $t2 = base address of array
for:    bge  $t0, $t1, end_for
        add  $t2, $t2, 4      # get address of next array element
        add  $t0, $t0, 1      # increment loop induction variable
        b    for
end_for:sw   $t3, ($t2)
    jr $ra

标签: arraysloopsmips

解决方案


推荐阅读