Soulzityr
Soulzityr

Reputation: 456

MIPS: Storing integer input into memory using sw causes address out of range error

I am trying to store a dynamic array into a program using a stack, by first reading an input n and then creating a stack with [n] inputs. However, when I try to store word, it creates an error that I don't understand.

.data                           
ask_for_size: .asciiz "\nInsert the array size\n"
ask_for_int: .asciiz "\nInput number\n"
input:  .space 16


.text 

Main:
  li    $v0,4
  la    $a0,ask_for_size
  syscall
  li    $v0,5
  la    $a0,input
  syscall
  add   $t0,$v0,$zero
  li    $sp,0x00001000  #stack pointer
  li    $s0,0x00001000  #base pointer
  la    $a0,ask_for_int

Loop1:
  li    $v0,4
  syscall
  li    $v0,5
  syscall
  sw    $v0,0($sp)

  addi  $sp,$sp,-4
  addi  $t0,$t0,-1
  bne   $t0,$zero,Loop1
  j Loop2

Loop2:

Upvotes: 0

Views: 1050

Answers (1)

Alexis Wilke
Alexis Wilke

Reputation: 20818

Shouldn't you add -4 to $sp before you sw $v0 into it?

addi  $sp,$sp,-4
sw    $v0,0($sp)

It seems to me that at this time you're overwriting past the end of $sp allocated for your process.


Also you load $a0 only once. Are you sure it does not get modified? Otherwise, maybe that would work better?

Loop1:
  li    $v0,4
  la    $a0,ask_for_int
  syscall
  ...

Upvotes: 1

Related Questions