Below is the code I have which does work. It inputs and stores the numbers the user types (can only be a list of 3 or 4 numbers).
However, this is really long and using an array index would be a lot less code and use less registers but I am uunsure how to do this. Would I need to put brackets around the source2 to make it indexed?
# 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
EDIT: So far i have this....it isnt working though - it isnt accepting integers which the user has entered
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
Is it not working because I already have numbers in $t0
? I ask the user what number of list they want (either 3 or 4 and that is stored in $t0
) but then I ask them to enter their list of numbers (which I need to put into an array). If I then put this array also in $t0
. Would it overwrite the numbers the user entered first when prompeted to say what list length they wanted?