Store a user input string of integers into an integer array mips assembly

时间:2019-03-17 22:25:59

标签: arrays assembly mips32

just like the title states, I am trying to write a program in MIPS assembly that will take a string input by a user of 4 integers between 0-9 separated by spaces and store each of those numbers as an integer into an array. Here's what I have so far:

screen shot of first half

screen shot of second half

.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

The two problems I am having are that I expected the offset of the array to be at -12 (4 words x 4 bytes - 4 for 0 start point), but it starts at -24, and for the 3rd input, no matter what it is, the int 0 gets stored. I am running this program in MARS 4.5. Sample output below:

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

Eventually I am going to have the user input 4 strings to create a 4x4 matrix and store all of those strings in the same array.

1 个答案:

答案 0 :(得分:1)

Jester在评论中向我指出了我做错了什么。而不是每次都将数组地址增加4,而不是最后一次增加4。所以我第一次加0,然后加4,再加8,再加12,再加12,这就是为什么我以偏移量24而不是预期的12结束的原因。我移动了{ {1}}和la $s0, array进入循环,以便继续将其设置回基地址。

新循环如下所示:

la $t0, list

向Jester致以正确的指导。