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

RISC-V汇编字符串右旋转功能异常,请求排查修复

问题排查与修复:RISC-V汇编字符串右旋转异常

问题分析

测试用例输入旋转位数1、ASCII增量0、字符串HAL,预期输出LHA但实际输出HAL,核心问题出在两处:

1. 缺失tempBuffer到buffer的复制逻辑

代码中end_rotate标签下仅初始化了复制所需寄存器,完全没有实现循环复制的核心逻辑,导致旋转后的内容从未覆盖原buffer,最终输出的还是初始输入的字符串。

2. 可选:负旋转量鲁棒性缺失

原代码未处理输入旋转量为负数的情况,若输入负数值会导致旋转结果不符合预期。

注:原代码的旋转逻辑本身是正确的——对应将末尾n个字符移到开头的右旋转定义(如HAL右旋转1位得LHA),与测试用例的预期匹配。

修复后的完整代码

.include "iomacros.inc"

.data
buffer: .space 101  # Reserve 101 bytes for the input string (100 chars + null terminator)
tempBuffer: .space 101 # Temporary buffer for rotation

.text
globl _start
_start:
    # Read the rotation amount (n) and ASCII shift value (p)
    getint(t0)  # Rotation amount
    getint(t1)  # ASCII shift value

    # Read the string into buffer
    gets(buffer, 100)

    # Calculate the length of the string
    la a1, buffer
    li t2, 0
calculate_length:
    lb a4, 0(a1)
    beq a4, zero, start_rotate
    addi a1, a1, 1
    addi t2, t2, 1
    j calculate_length

la a1, buffer    # Ensure a1 points to the start of buffer

start_rotate:
    # Adjust rotation amount if greater than length
    rem t0, t0, t2  # t0 now contains the effective rotation amount
    # 处理负旋转量,转换为等价正右旋转量
    bltz t0, adjust_negative
    j rotate_init
adjust_negative:
    add t0, t0, t2
rotate_init:
    li a3, 0        # Reset index for character processing

rotate_loop:
    lb a4, 0(a1)            # Load current character from buffer
    beq a4, zero, end_rotate # Exit loop if null terminator is reached

    add a5, a3, t0           # Add rotation amount to current index
    rem a5, a5, t2           # Correct position if exceeding length
    la a6, tempBuffer        # Start address of tempBuffer
    add a6, a6, a5           # Calculate correct address in tempBuffer

    sb a4, 0(a6)             # Store character in its new position
    
    addi a3, a3, 1           # Increment index for next character
    addi a1, a1, 1           # Move to the next character in the buffer
    j rotate_loop

end_rotate:
    # Copy from tempBuffer back to buffer
    la a1, tempBuffer
    la a2, buffer
    li a3, 0
copy_loop:
    bge a3, t2, end_copy     # Stop after copying all valid characters
    lb a4, 0(a1)
    sb a4, 0(a2)
    addi a1, a1, 1
    addi a2, a2, 1
    addi a3, a3, 1
    j copy_loop
end_copy:
    sb zero, 0(a2)           # Add null terminator to ensure valid string

    # ASCII decrypt: subtract the shift value
    la a1, buffer
ascii_adjust_loop:
    lb a4, 0(a1)
    beq a4, zero, output_result
    sub a4, a4, t1           # Reverse the encryption shift
    sb a4, 0(a1)
    addi a1, a1, 1
    j ascii_adjust_loop

output_result:
    puts(buffer)             # Output the decrypted string
    exit                     # Terminate program

关键修复点说明

  1. 补全复制循环:

    • 以字符串长度t2作为终止条件,确保复制所有有效字符
    • 手动添加null terminator,避免字符串格式错误
  2. 负旋转量处理:

    • 若输入旋转量为负(等价于左旋转),通过add t0, t0, t2转换为等价的正右旋转量,提升代码鲁棒性
  3. 补充完整解密流程:

    • 按照需求实现ASCII增量的逆操作(减去增量值),并添加字符串输出逻辑,完成完整解密流程

测试验证

输入:

1
0
HAL

修复后代码输出LHA,完全符合预期。

内容的提问来源于stack exchange,提问作者Rafael Lourenço

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 18:07:32