"(error) attempting to write to an invalid memory address" When trying to store stack pointer

0

I'm trying to learn RISC-V under the Jupiter environment (risc32) and I came across a problem asking me to write a recursive program with RISC-V. I can't seem to get the sw instruction to work, as it always gives an error: invalid address

I've tried different offsets, different registers etc. nothing seems to work

.globl __start

.rodata
  msg_input: .string "Enter a number: "
  msg_result: .string "The result is: "
  newline: .string "\n"

.text

__start:
  # prints msg_input
  li a0, 4
  la a1, msg_input
  ecall
  #read from standard input
  li a0, 5
  ecall
  #initialize stack
  addi x31, x0, 2
  addi sp, x0, 800
  mv x5, a0
  jal x1, recfunc
  mv t0, x5

recfunc:
  addi sp, sp, -8
  sw x1, 0(sp)
  bge x5, x31, true
  lw x1, 0(sp)
  addi x10, x0, 1
  addi sp,sp, 8
  jalr x0, 0(x1)


true:  
  div x5, x5, x31
  jal x1, recfunc
  lw x1, 0(sp)
  addi sp,sp,8
  mul x10, x10, x31
  addi x10, x10, 1
  jalr x0, 0(x1)

result:
  #prints msg_result
  li a0, 4
  la a1 msg_result
  ecall
  #prints the result in t0
  li a0, 1
  mv a1, t0
  ecall
  #ends the program with status code 0
  li x5, 10
  ecall

Error occurs at:

 sw x1, 0 (x2)

(error) attempting to write to an invalid memory address 0x00000318

recursion
assembly
stack
riscv
asked on Stack Overflow Oct 25, 2019 by Aladar Wu • edited Oct 25, 2019 by Aladar Wu

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0