0

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.

4
  • Which part are you having trouble with? What language are you working in? Commented Apr 21, 2014 at 0:26
  • 1
    Indeed, unless you absolutely have to do this in assembly language (which in this day and age, for a problem of this sort, would probably only be a "homework" requirement) your actual question should depend on the details of the user interface subsystem and the run time support environment, not on the processor. Commented Apr 21, 2014 at 0:35
  • What simulator are you using? spim? MARS? Commented Apr 21, 2014 at 5:08
  • @KonradLindenbach spim Commented Apr 21, 2014 at 14:57

1 Answer 1

2

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:
Sign up to request clarification or add additional context in comments.

3 Comments

is it more efficient to do sll $a0, $v0, 2 than to li $a0, 20?
They are not equivalent. n is stored in $v0 but it needs to be multiplied by 4 to get the number of bytes required hence sll $a0 $v0 2
I forgot he was taking an input from user, my bad

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.