保存整数作为字符串在MIPS [英] Saving integers as Strings in MIPS

查看:401
本文介绍了保存整数作为字符串在MIPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道,有没有MIPS任何方式为数字的总和存储为一个字符串,后来被字节读取字节他们,例如:

I was just wondering, is there any way in MIPS to store a summation of numbers as a string and later read them byte by byte, for example:

的总和657 - > SW成.ascii指令 - >稍后磅上的第一个索引得到6(在ASCII code)相同5等。这可能吗?

the sum 657 -> sw into a .ascii directive -> later lb on the first index to get 6 (in ascii code) same with 5 and so on. Is this possible?

推荐答案

当然。在.ascii指令是没有,但集中在ASCII文本的存储

Of course. The ".ascii" directive is none but a .byte directive focused on the storage of ASCII text


   .ascii "PP"

就像


   .byte 80,80

您可以使用。空间以腾出空间给你的ASCII字符串,然后通过SW到使用缓冲区中的皈依整数到ASCII码,如果你的意思是这个.ascii指令的整数。下面code。使用itoa并打印(只用于测试)与print_string了二进制数字转换为ASCII字符串。该函数使用一个缓冲区,并返回指针到第一ASCII数字可用于打印。这可以被用作用于sprintf的状函数实现的第一辅助函数

You can use .space to make room for your ASCII string, and then use the buffer in the convertion from integer to ASCII, if you mean this by "sw into .ascii directive" of in integer. The following code converts the "binary number" into a ASCII string using itoa and prints it (just for testing) with print_string. The function uses a buffer and returns the pointer to the first ASCII digit usable for printing. This could be used as a first helper function for a sprintf-like function implementation.


       .data

buffer:
         .space 32


      .text
      # the main supposes env. like spim or MARS
main:
      li   $a0, 1234      # a number
      jal  itoa
      move $a0, $v0
      li   $v0, 4         # print_string    
      syscall
      li   $v0, 10
      syscall             # exit

itoa:
      la   $t0, buffer    # 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
      slt  $t2, $a0, $0   # keep the sign
      li   $t3, 10        # preload 10
      beq  $a0, $0, iend  # end if 0
      neg  $a0, $a0
loop:
      div  $a0, $t3       # a /= 10
      mflo $a0
      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  $a0, $0, loop  # if not zero, loop
      addi $t0, $t0, 1    # adjust buf ptr
iend:
      beq  $t2, $0, nolz  # was < 0?
      addi $t0, $t0, -1
      li   $t1, '-'
      sb   $t1, ($t0)
nolz:
      move $v0, $t0      # return the addr.
      jr   $ra           # of the string

在你的主要有$ V0,磅R,($ V0)挑选1,磅R,1($ V0 )拿起第二个数字(2),依此类推;记得字符串是空终止,所以如果你选择0(数字),你必须停止

After you have $v0 in the main, lb R, ($v0) picks "1", lb R, 1($v0) picks second digit (2) and so on; remember the string is null-terminated, so if you pick 0 (numeric), you have to stop

这篇关于保存整数作为字符串在MIPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆