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

MIPS汇编income_from_record函数实现求助:Run I/O无输出

解决MIPS汇编income_from_record函数的字符串转整数问题

你的核心需求是将ASCII数字字符串转换为十进制整数,现有代码仅完成了字符串长度统计,未实现实际转换逻辑,以下是完整的函数实现:

income_from_record:
#function to return numerical income from a specific record
#e.g. for record "Microsoft,34\r\n", income to return is 34(for which name is Microsoft)

#arguments: a0 contains pointer to start of numerical income in record 

#function RETURNS income numerical value of the asci income in a0 (34 in our example)
    
# Start your coding from here!
    li      a0, 0          # 初始化结果为0
    add     t0, zero, a0   # t0保存当前字符的地址(初始为字符串起始地址)

convert_loop:
    lb      t1, 0(t0)      # 加载当前字符到t1
    
    # 检查是否到达非数字字符(比如\r、\n或字符串终止符)
    blt     t1, 48, convert_exit  # 小于'0'的ASCII码,退出循环
    bgt     t1, 57, convert_exit  # 大于'9'的ASCII码,退出循环
    
    # 将ASCII字符转换为十进制数字:'0'的ASCII码是48,减去48得到对应数值
    subi    t1, t1, 48
    
    # 结果更新:当前结果*10 + 当前数字
    addi    t2, zero, 10
    mul     a0, a0, t2
    add     a0, a0, t1
    
    addi    t0, t0, 1      # 移动到下一个字符地址
    jal     zero, convert_loop  # 继续循环

convert_exit:
# End your coding here!
    ret

代码说明:

  • 寄存器分工:
    • a0:初始传入字符串起始地址,后续存储转换后的整数结果
    • t0:跟踪当前处理的字符地址
    • t1:存储当前读取的ASCII字符,转换为数字后参与计算
    • t2:临时存储常量10,用于乘法运算
  • 终止条件:当读取到的字符不是数字(ASCII码不在48-57区间)时退出循环,适配例子中\r\n这类非数字终止符的场景
  • 转换逻辑:通过subi t1, t1, 48完成ASCII到十进制数字的转换,再通过结果*10 + 当前数字的方式累积得到最终整数

替换你原有代码中Start your coding from here!到End your coding here!之间的内容即可正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 22:10:43