Reputation: 341
Can anyone show me an example of how to first record the size, n, of the integer list from the user, and then store the n integers entered by the user into an array that can hold n integers?
I can't find something similar through google. Any help would be appreciated.
Upvotes: 0
Views: 5896
Reputation: 4961
You'll want to look at this list of MIPS system calls, the first 17 of which are supported by the spim
simulator.
With that list in mind, here's the general approach you should take:
# read n from console
li $v0 5
syscall
move $t0 $v0
# allocate dynamic memory
sll $a0 $v0 2 # sll performs $a0 = $v0 x 2^2
li $v0 9 #9 is the system code for service(sbrk) whoes work is
syscall #to allocate dynamic memory
move $t1 $zero
move $t2 $v0
loop:
bge $t1 $t0 end
# read in and store int
li $v0 5
syscall
sw $v0 0($t2)
addi $t1 $t1 1
addi $t2 $t2 4
j loop
end:
Upvotes: 2