在 MIPS 中将整数保存为字符串
我只是想知道,MIPS 中是否有任何方法可以将数字总和存储为字符串,然后逐字节读取它们,例如:
总和 657 -> sw 进入 .ascii 指令 ->稍后对第一个索引进行 lb 操作,得到 6(以 ascii 代码表示),与 5 相同,依此类推。这可能吗?
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 ShinTakezou 的回答有缺陷。
行负 $a0, $a0
会将正数转换为负数,这会导致代码输出奇怪的结果。
如果我们删除这一行。这些代码对于正整数工作正常
I believe there is a flaw in ShinTakezou's answer.
The line neg $a0, $a0
will convert the positive number to negative, which results in a weird output for the code.
If we remove this line. The codes work fine for positive integer
当然。 “.ascii”指令不是一个,而是一个专注于 ASCII 文本存储的 .byte 指令,
就像
您可以使用
.space
为 ASCII 字符串腾出空间,然后在转换中使用缓冲区从整数到 ASCII,如果您的意思是整数的“sw into .ascii 指令”。以下代码使用 itoa 将“二进制数”转换为 ASCII 字符串,并使用 print_string 打印它(仅用于测试)。该函数使用缓冲区并将指针返回到可用于打印的第一个 ASCII 数字。这可以用作类似 sprintf 的函数实现的第一个辅助函数。在主文件中有 $v0 后,
lb R, ($v0)
选择“1”,lb R, 1($v0)
选择第二个数字 (2) 并很快;请记住该字符串以 null 结尾,因此如果您选择 0(数字),则必须停止Of course. The ".ascii" directive is none but a .byte directive focused on the storage of ASCII text
is like
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.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这是另一个实现,对于无符号整数,分解为
utoa
;对于使用utoa
的有符号整数,分解为itoa
。它们提供两个返回值,一个是要打印的字符串的地址,另一个是要打印的字符串中的字符数——这个计数在使用文件 I/O 系统调用时很有帮助。字符串缓冲区(长度至少为 12 个字符)作为参数传入,因此可以是全局的,也可以是在堆栈上,如测试main
所示。Here's another implementation, factored into
utoa
for unsigned integers, anditoa
for signed integers that usesutoa
. These provide two return values, one is the address of the string to print, and the other is a count of the number of characters in the string to print — this count is helpful when using file I/O syscalls. The string buffer (of at least 12 characters in length) is passed in as parameter, so can be a global, or on the stack as the testmain
illustrates.