You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RISC-V汇编实现正负整数求和打印及错误排查

Alright, let's fix those assembly errors step by step—you're mixing up MIPS and RISC-V conventions here, which is causing all those operand and syntax issues.

Breakdown of the Errors

  1. v0 operand type errors: v0 is a MIPS register name, but in standard RISC-V, we use a7 to hold system call numbers (and a0/a1 for arguments). Your assembler doesn't recognize v0, so we need to swap those out.
  2. Incorrect la usage: la (load address) is only for loading the memory address of a label (like a string). When you want to print the integer value stored in s1/s2, you need to move that value into a0—la can't do that, it's for addresses, not register values.
  3. Wrong exit syscall number: 10 is the MIPS exit code; RISC-V uses 93 for exit, along with an exit status code in a0.

Fixed Exit & Print Code Snippet

# Exit and print results
exit:
    # Print positive total label string
    li a7, 4          # RISC-V syscall: print string
    la a0, totalPositive
    ecall

    # Print positive sum value
    li a7, 1          # RISC-V syscall: print integer
    move a0, s1       # Copy positive sum from s1 to a0 (syscall argument)
    ecall

    # Print negative total label string
    li a7, 4
    la a0, totalNegative
    ecall

    # Print negative sum value
    li a7, 1
    move a0, s2       # Copy negative sum from s2 to a0
    ecall

    # Exit program cleanly
    li a7, 93         # RISC-V syscall: exit
    li a0, 0          # Exit status code (0 = success)
    ecall

Quick Extra Check

Make sure you've defined the string labels in your .data section, like this:

.data
totalPositive: .asciiz "Positive numbers total: "
totalNegative: .asciiz "\nNegative numbers total: "

If you were using a MIPS-compatible RISC-V emulator (like SPIM), you could use v0 instead of a7, but you'd still need to replace la a0, ($s1) with move a0, s1—la is never used to move register values around, regardless of the architecture.

These changes will eliminate all the operand and syntax errors, and your program will correctly print the positive and negative sums before exiting.

内容的提问来源于stack exchange,提问作者Bella

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 15:02:56