“(错误)尝试写入无效的内存地址”尝试存储堆栈指针时

时间:2019-10-25 13:40:17

标签: recursion assembly stack riscv

我正在尝试在Jupiter环境(risc32)下学习RISC-V,但遇到一个问题,要求我用RISC-V编写递归程序。我似乎无法使sw指令正常工作,因为它始终会出现错误:无效地址

我尝试了不同的偏移量,不同的寄存器等。似乎没有任何作用

.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

错误发生在:

 sw x1, 0 (x2)

(错误)尝试写入无效的内存地址0x00000318

0 个答案:

没有答案
相关问题