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
v0operand type errors:v0is a MIPS register name, but in standard RISC-V, we usea7to hold system call numbers (anda0/a1for arguments). Your assembler doesn't recognizev0, so we need to swap those out.- Incorrect
lausage: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 ins1/s2, you need to move that value intoa0—lacan't do that, it's for addresses, not register values. - Wrong exit syscall number:
10is the MIPS exit code; RISC-V uses93for exit, along with an exit status code ina0.
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
相关产品推荐
相关产品推荐

