【问题标题】:MIPS - Storing ints In Array From User InputMIPS - 从用户输入将整数存储在数组中
【发布时间】:2013-04-14 05:59:09
【问题描述】:

我正在尝试将用户输入存储到一个数组中,但是当我使用 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

【问题讨论】:

    标签: assembly mips


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:
      • 商店应该是

        sw $v0, 数组($t0)

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

      • 在mess上方设置数组

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

      【讨论】:

        【解决方案3】:

        正确的数组输入代码

        .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
        

        【讨论】:

        • 请使用 markdown 格式以使其更具可读性。请添加一些解释,以帮助消除 StackOverflow 是免费代码编写服务的误解。
        【解决方案4】:

        试试这个:

        ...
        .p2align 2
        array: .space 40    #10 element integer array
        ...
        

        【讨论】:

        • 这并没有改变任何东西。感谢您的回复。
        • 你有没有编译错误? .p2align 2 应该将以下代码/数据对齐到 2 的幂的地址(此处为 2^2=4)。如果在mess 字符串中添加一到三个额外字符会怎样?这也可以修复array 的对齐方式。
        【解决方案5】:

        下面的代码会从用户的输入中读取他们输入的数量,如果他们想输入 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 
        

        【讨论】:

        • add $t8,$t0,$t8 #add $t8 to $t0, save to $t8 之类的评论只是多余的,没有帮助。重申 MIPS 语法如何适用于每条普通指令是混乱的,会分散对addi $t9,$t9, -4 # address of A[n-1] 等有用 cmets 的注意力。另外,在我碰巧看到的部分swap: 下,您的一些 cmets 是错误的。 la $t1, myArray 不加载A[1],甚至不加载A[1] 的地址。这是A[0]的地址,和你从la $t4, myArray得到的一样。
        • 你在一个循环中做了同样的la 4 次,没有明显的原因;您可以将其保存在您在循环之外设置的 reg 中,然后执行addu $t1, $t8, $t0 # myArray + n_byte_offset 之类的操作。 (注意注释如何解释 semantic 含义在更高一级的逻辑中,使用变量名称,而不是重复寄存器名称,因为您可以从指令本身中看到这一点。)跨度>
        • 您也可以并且应该使用lw $t2, 4($t4) 之类的东西,而不是使用单独的addiu 来偏移指针寄存器。 lw / sw 是 I 类型,从寄存器中获取 16 位立即数偏移量,因此您只需使用运行时变量的地址计算指令。
        猜你喜欢
        • 1970-01-01
        • 2014-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多