Сборка MIPS - простой запрос о том, как сделать числа, введенные в массив - PullRequest
1 голос
/ 21 ноября 2011

Ниже приведен код, который работает.Он вводит и хранит числа, которые вводит пользователь (может быть только списком из 3 или 4 чисел).

Однако это очень долго, и использование индекса массива будет намного меньше кода и будет использовать меньше регистров, ноЯ не уверен, как это сделать.Нужно ли ставить скобки вокруг source2, чтобы сделать его проиндексированным?

    # Entering the user's numbers

# Prompt user to enter their numbers
  la $a0, number            # load the address of number into $a0
  li $v0, 4             # 4 is the print_string syscall
  syscall

# Declare an array
la $t3, array           # load address of array into $t3
li $t4, 0           # index value 0 is the start of the memory address of the array
mul $t5, $t4, 4         # multiply index value by 4 because each element is four bytes
add $t5, $t3, $t5       # add base address of array to index value into $t5

# Get the first number from the user, put into $t1
li $v0, 5           # load syscall read_int into $v0
syscall             # make the syscall
move $t1, $v0           # move the number read into $t1
sw $t1, 0($t5)          # store number held in $t1 into memory address location $t5

# Get the second number from the user, put into $t2 and store in array
li      $v0, 5              # load syscall read_int into $v0
syscall                     # make the syscall
move $t1, $v0                           # move the number read into $t1
add $t5, $t5, 4                         # add 4 bytes to go to the next position in the array
sw $t1, 0($t5)          # store number into memory address location of $t5

# Get the third number from the user, put into $t3
li $v0, 5               # load syscall read_int into $v0
syscall                     # make the syscall
move $t1, $v0           # move the number read into $t1
add $t5, $t5, 4         # add 4 bytes to go to the next position in the array
sw $t1, 0($t5)          # store number into memory address location of $t5

# Branches to L3 if user chose to enter only three numbers
beq $t0, 3, L3          # if content in $t0 = 3, branch to L3

# Get the fourth number from the user, put into $t4
li $v0, 5               # load syscall read_int into $v0
syscall                 # make the syscall
move $t1, $v0           # move the number read into $t1
add $t5, $t5, 4         # add 4 bytes to go to the next position in the array
sw $t1, 0($t5)          # store number into memory address location of $t5

# Branches to L3 if user chose to enter only four numbers
beq $t0, 4, L3          # if content in $t0 = 4, branch to L3

РЕДАКТИРОВАТЬ: Пока у меня есть это .... хотя он не работает - он не принимает целые числа, которые пользователь ввел

    loop:
    lw $t2, 0($a0) # load array element from memory
    addi $t2, $t2, 1 # increment element
    sw $t2, 0($a0) # write back to memory
    addi $a0, $a0, 4 # increment array pointer by 4 (word = 4 bytes)
    addi $t1, $t1, 1 # increment loop counter by 1
    blt $t1, $t0, loop # loop, if necessary

Разве это не работает, потому что у меня уже есть числа в $t0?Я спрашиваю пользователя, какой номер списка он хочет (3 или 4, и который хранится в $t0), но затем я прошу его ввести свой список чисел (который мне нужно поместить в массив).Если я тогда положу этот массив также в $t0.Будет ли он перезаписывать числа, которые пользователь вводил первым, когда ему предлагалось указать желаемую длину списка?

...