MIPS - How do I store user input and store it in an array dynamically?

StackOverflow https://stackoverflow.com/questions/23189111

  •  06-07-2023
  •  | 
  •  

Pregunta

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.

¿Fue útil?

Solución

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:
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top