首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MIPS程序集: INT到STRING函数只有2个字符(切断最后一个字符)

MIPS程序集: INT到STRING函数只有2个字符(切断最后一个字符)
EN

Stack Overflow用户
提问于 2016-01-19 08:21:28
回答 1查看 1.4K关注 0票数 0

我编写了这个函数来将int值转换为ascii字符串。但是当输入大约是315时,它会将31打印到字符串中。因为我不是装配方面的专家,在这方面任何帮助都会很感激。下面是代码:(int_buf是存储整数输入的地方,$a是字符串输出)

代码语言:javascript
复制
 itoa:
  la      $t0, int_buf   # load buf
  add     $t0, $t0, 30   # seek the end
  sb      $0, 1($t0)     # null-terminated str
  li      $t1, '0'  
  sb      $t1, ($t0)     # init. with ascii 0
  li      $t3, 10        # preload 10
  beq     $t8, $0, iend  # end if 0
loop:
  div     $t8, $t3       # a /= 10
  mflo    $t8
  mfhi    $t4            # get remainder
  add     $t4, $t4, $t1  # convert to ASCII digit
  sb      $t4, ($t0)     # store it
  sub     $t0, $t0, 1    # dec. buf ptr
  bne     $t8, $0, loop  # if not zero, loop
  addi    $t0, $t0, 1    # adjust buf ptr
iend:
  move    $a1, $t0       # return the addr.
  jr      $ra            # of the string

编辑:我发现问题不在函数中,因为它是在之后将字符串打印到文件中的方式:

代码语言:javascript
复制
li    $v0, 15
move  $a0, $s6      
move  $t8, $s2
jal   itoa
li    $a2, 4 //problem was only 2 digits selected here!
syscall

当我将空字符设置为4位时,如何防止该工具将空字符打印到我的文件中?

EN

回答 1

Stack Overflow用户

发布于 2016-01-19 10:01:53

当我将空字符设置为4位时,如何防止该工具将空字符打印到我的文件中?

计算字符串中的数字数很容易。您知道,NUL字符是在int_buf+31,因为itoa是如何编写的。所以就拿这个和你从itoa得到的地址之间的区别吧

代码语言:javascript
复制
li $a2,int_buf+31
sub $a2,$a2,$a1  # a2 is now equal to the number of digits in the string
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34871690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档