【问题标题】:Store a user input string of integers into an integer array mips assembly将用户输入的整数字符串存储到整数数组 mips 程序集中
【发布时间】:2019-03-17 22:25:59
【问题描述】:

就像标题状态一样,我正在尝试在 MIPS 程序集中编写一个程序,该程序将接受用户输入的字符串,该字符串由用户输入的 0-9 之间用空格分隔的 4 个整数,并将这些数字中的每一个作为整数存储到数组中.到目前为止,这是我所拥有的:

.data
input1: asciiz "Input Row 1: "
row1: .asciiz "Row 1: "
array: .space 16
list: .space 8
.text
la $s0, array              #load address of array to s0
li $s1, 0                  #index of array
li $v0, 4                  #call for print string
la $a0, input1             #load string to be printed
syscall
li $v0, 8                  #call for user input
la $a0, list               #address of where the input will be stored
li $a1, 8                  #space allowed for input
syscall
loop:
la $t0, list               #load address of the string
add $t2, $zero, $zero      #index of the string
load:
sll $t3, $t2, 1            #multiply string index by 2 to skip spaces
sll $t4, $s1, 2            #multiply index of array by 4 for size of word
addu $t0, $t0, $t3         #position string
addu $s0, $s0, $t4         #position array
lbu $t1, 0($t0)            #load byte of the string to t1
addiu $t1, $t1, -48        #convert char to integer
sb $t1, 0($s0)             #store byte into the array
addi $t2, $t2, 1           #increment index of string by 1
addi $s1, $s1, 1           #increment index of array by 1
blt $t2, 4, load           #if the index of the string is less than 4, load the next character
li $v0, 11                 #printing first input as integers from here
addi $a0, $zero, 0xA       #new line ascii character
syscall
li $v0, 4                  #call for print string
la $a0, row1               #load string to be printed
syscall
li $v0, 1                  #print integer call
lb $a0, -24($s0)           #load first number input
syscall
li $a0, 32
li $v0, 11                 # syscall number for printing character
syscall
li $v0, 1
lb $a0, -20($s0)           #load second number input
syscall
li $a0, 32
li $v0, 11                 # syscall number for printing character
syscall
li $v0, 1
lb $a0, -16($s0)           #load third number input
syscall
li $a0, 32
li $v0, 11                 # syscall number for printing character
syscall
li $v0, 1
lb $a0, -12($s0)           # load fourth number input
syscall
exit:
li $v0, 10
syscall

我遇到的两个问题是,我希望数组的偏移量为 -12(4 个字 x 4 个字节 - 0 起始点为 4),但它从 -24 开始,对于第三个输入,不管它是什么,int 0 都会被存储。我在 MARS 4.5 中运行这个程序。下面的示例输出:

Input Row 1: 1 2 3 4
Row 1: 1 2 0 4
-- program is finished running --

最终我会让用户输入 4 个字符串来创建一个 4x4 矩阵并将所有这些字符串存储在同一个数组中。

【问题讨论】:

  • 一个明显的问题是addu $s0, $s0, $t4 #position array 会覆盖$s0,因此下一次循环迭代将不会使用正确的基地址。使用你的 MARS 单步执行程序,看看它在做什么。
  • 那是用来遍历数组的。由于添加的第一个数字是 0。它不会更改基地址。在第二个循环中,索引递增到 1,因此该行增加了 4,即单词的长度。我逐步完成了该程序以确保它似乎做了我期望它做的事情。但是,我没有正确地遍历数组吗?
  • 您在每次迭代中计算$s1*4,这是正确的,但应始终添加到array。由于您将其添加到$s0,并且您还将其写入$s0,因此它将无法正常工作。
  • 但是 $s0 拥有与数组相同的地址。它不应该同样工作吗?我认为在数据中实例化数组只是为了指定它需要多少空间。
  • $s0 仅在第一次迭代中保留array,因为您在开始时只执行一次la $s0, array,并且您执行addu $s0, $s0, $t4,这会更改$s0,因此它不再指向@987654336 @.

标签: arrays assembly mips32


【解决方案1】:

Jester 在 cmets 中向我指出了我做错了什么。我不是每次都将数组地址增加 4,而是比上一次增加了 4。所以我第一次添加 0,然后是 4,然后是 8,将我放在 12,然后再增加 12,这就是为什么我最终得到 24 的偏移量而不是预期的 12。我移动了 @ 的声明987654321@ 和 la $t0, list 进入循环,以便继续将其设置回基地址。

新循环如下所示:

loop:
add $t2, $zero, $zero     #index of the string
load: 
la $s0, array              #load address of array to s0
la $t0, list               #load adress of the string
sll $t3, $t2, 1            #multiply string index by 2 to skip spaces
sll $t4, $s1, 2            #multiply index of array by 4 for size of word
addu $t0, $t0, $t3         #position string
addu $s0, $s0, $t4         #position array
lbu $t1, 0($t0)            #load byte of the string to t1
addiu $t1, $t1, -48        #convert char to integer
sb $t1, 0($s0)             #store byte into the array
addi $t2, $t2, 1           #increment index of string by 1
addi $s1, $s1, 1           #increment index of array by 1
blt $t2, 4, load           #if the index of the string is less than 4, load the next character
li $v0, 11                 #printing first input as integers from here
addi $a0, $zero, 0xA
syscall
li $v0, 4                  #call for print string
la $a0, row1               #load string to be printed
syscall
li $v0, 1
lb $a0, -12($s0)
syscall
li $a0, 32
li $v0, 11                 # syscall number for printing character
syscall
li $v0, 1
lb $a0, -8($s0)
syscall
li $a0, 32
li $v0, 11                 # syscall number for printing character
syscall
li $v0, 1
li $a0, 32
li $v0, 11                 # syscall number for printing character
syscall
li $v0, 1
lb $a0, 0($s0)
syscall
exit:
li $v0, 10
syscall

感谢 Jester 引导我朝着正确的方向前进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-11
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多