8

我正在尝试将用户输入存储到一个数组中,但是当我使用 sw 时,我收到一个错误“存储地址未与字绑定对齐”。我的目标是从数组中读取 10 个整数,但是在输入第一个数字后,我在 sw 命令处收到错误。我不知道我做错了什么我花了几个小时试图弄清楚。任何帮助将不胜感激并标记为有用。

        .data 

mess: .asciiz " Enter 10 numbers to be stored in the array. "
array: .space 40    #10 element integer array
    .globl main
    .text 
main:
    jal read
    b done
read:
    la $t0, 0   #count variable
    b readLoop
    jr $ra

readLoop:
    beq $t0, 40, read   #branch if equal to 40, 10 items
    li $v0, 4       #Print string
    la $a0, mess        #load prompt
    syscall
    li $v0, 5       #read int
    syscall 
    sw $v0, array       #store input in array ERROR HERE
    addi  $t0, $t0, 4   #add by 4 to count
    b readLoop
print:

done:

这对我有用。我不知道为什么它在上面不起作用

    .data 
list:  .space 16
.globl main
.text

main:

    li $v0, 5
    syscall
    sw $v0, list

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

5 回答 5

8

在为数据段中的字符串分配空间之前,尝试为数组分配空间:

  array: .space 40    #10 element integer array
  mess: .asciiz " Enter 10 numbers to be stored in the array. "

如果首先分配字符串,则数组可能从不能被 4 整除的地址开始并导致字对齐错误

于 2014-10-28T08:00:09.427 回答
2
  • 商店应该是

    sw $v0, 数组($t0)

  • 替换la $t0, 0li $t0, 0

  • 设置上面的数组mess

此外,当您达到 10 个项目时,您重新开始读取并覆盖以前的值。

于 2013-04-15T14:39:32.290 回答
2

正确的数组输入代码

.data
    myarray:.space 40    
    st:.asciiz "Enter the 10 Elements"

.text    
    li $v0,4
    la $a0,st
    syscall
    jal fun
    li $v0,10
    syscall

fun:        
    li $v0,5
    syscall
    beq $t0,40,exit
    sw $v0,myarray($t0)
    add $t0,$t0,4
    j fun

exit:
    jr $ra
于 2018-01-09T18:11:00.827 回答
1

试试这个:

...
.p2align 2
array: .space 40    #10 element integer array
...
于 2013-04-14T06:03:45.173 回答
1

下面的代码将从用户的输入中读取他们输入的数量,如果他们想输入 10 个整数,初始输入必须是 10。然后它会提示用户一次填写一个整数。将打印初始数组之后。

我的一个项目要求我具有排序功能,以便在交换代码的第二部分中实现以实现升序排序。任何澄清与我联系。

.data 
    inputLabel: .asciiz "Enter the amount of integers in the array: "
        myArray: .space 100
    
    arrayToBeSorted: .asciiz "Enter the array to be sorted one at a time: "
    nextLine: .asciiz "\n"
    original: .asciiz "You have entered: "
    result: .asciiz "Here is the sorted list in ascending order: "

.text 

.globl main

main: 
        #print input label
    li $v0, 4
    la $a0, inputLabel
    syscall 

        #read amount of integers to be inputed.
    li $v0, 5
    syscall
    addi $t0, $v0, 0

        #put input into $t0, number of integers in array
    
    addi $t7, $t0, 0
    li $t4, 4
    #mul $t5, $t0, $t4

        #ask user to input numbers of the arrayToBeSorted
    li $v0, 4
    la $a0, arrayToBeSorted
    syscall

    li $t6, 0 
    #used to index array at insertion
    
    j loop0

        #read the numbers 
    loop0:         #loop successfully reads in n integers and stores them in a list

            #check if 0 == input;
        beq $t7, 0, next
            #take in user input
        li $v0, 5
        syscall
            #store user input into list
        sw $v0, myArray($t6)
            #add 4 to the index of the list
        addi $t6, $t6, 4
            #sub 1 from the number of items to add to list
        addi $t7, $t7, -1
            #loop
        j loop0

    next: #success print
        move $t0, $t6
        li $v0, 4
        la $a0, original
        syscall
        j while 

    while: #printer success

        #print new line
        li $v0, 4
        la $a0, nextLine
        syscall

        beq $t6, $zero, swap 
            #dif i is 0 then exit
        lw $t1, myArray($t3)
            #load in my array at index i
        
        #printing int at myArray[i]
        li $v0, 1
        move $a0, $t1
        syscall  

        addi $t3, $t3, 4
        addi $t6, $t6, -4

        j while 

swap:

    la $t4, myArray 
    #loads A[0] myArray to $t4

    la $t1, myArray 
    #loads A[1] array to $t1
    
    addi $t1,$t1,4 
    #add 4 to $t1, save to $t1
    
    la $t8,myArray 
    #loads A[n] array to $t8
    
    add $t8,$t0,$t8 
    #add $t8 to $t0, save to $t8
    
    la $t9,myArray
    
    add $t9,$t0,$t9 
    #add $t9 to $t0, save to $t9
    
    addi $t9,$t9,-4 
    #subtracts 4 from $t9, save to $t9 A[n-1]

loop:

    lw $t2,($t4)
    #load A[0] input into $t2

    lw $t3,($t1) 
    #load A[1] input into $t3

    blt $t2,$t3,loop1 
    # dif $t2 < $t3, A[0]<A[1]go to loops

    sw $t3,($t4) 
    #store $t3 in $t4 A[1] = A[0]

    sw $t2,($t1) 
    #store $t2 in $t1 A[0] = A[1]

loop1:

    addi $t1,$t1,4 
    #add 4 to $t1, save to $t1 A[1] + 4 becomes A[2]
    blt $t1,$t8,loop 
    #dif $t1<$t8, go to loop A[2] < A[n]
    addi $t4,$t4,4 
    #add 4 to $t4, save to $t4
    move $t1,$t4
    addi $t1,$t1,4 
    #add 4 to $t1, save to $t1
    blt $t4,$t9,loop 
    #idf $t4<$t9, to go loop

print:

    la $a1,myArray 
    #loads myArray to $a1
    la $a0, result
    #loads output to $a0
    li $v0, 4 
    #loads 4 into #v0
    syscall
    la $a0, nextLine 
    #loads nextLine into $a0
    li $v0, 4 
    #loads 4 into $v0
    syscall

loop2:

    blez $t0, done 
    #if $t0<=0, go to done
    li $v0, 1 
    #loads 1 into $v0
    lw $a0, 0($a1) 
    #load an inout into $a0
    syscall
    la $a0, nextLine 
    #loads nextLine into $a0
    li $v0, 4 
    #loads 4 into $v0
    syscall
    addi $a1, $a1, 4 
    #add 4 to $a1, save to $a1
    addi $t0, $t0, -4 
    #subtracts 4 from 
    #t0, save to $t0
j loop2

    done:
    li $v0, 10
    syscall
    j done 
于 2021-11-22T18:21:55.050 回答