MIPS:如何排序
我需要您帮助解决 MIPS 程序集中的排序问题:
如何编写 MIPS 程序来读取仅包含十进制整数的文本文件并按降序对它们进行排序。
该程序应执行以下操作:
■ 打开文本文件并将其内容读入字符数组。该数组应限制为 1000 个字符。 MARS 提供用于打开和读取文本文件的系统调用。
■ 逐个字符地遍历数组。将每个十进制字符串转换为二进制。十进制字符串由一个或多个十进制字符组成。它应该以空格或换行符结尾。忽略并跳过所有其他字符。将所有十进制整数存储到一个字数组中。整数数组的大小应限制为 100 个字。
■ 按降序对整数数组进行排序。
■ 显示排序后的数组
实际上,我对数组进行排序没有问题,因为我拥有它,但处理文本文件、从中读取数据、转换为十进制插入数组时出现问题。
你有什么想法吗?评论 ?建议?
提前谢谢
更新:有些人问了问题是什么? 问题是如何从txt文件中读取数字,将数字转换为十进制?这就是问题所在。
I need your help with this problem of sorting in MIPS assembly :
how to write a MIPS program to read a text file containing only decimal integers and sort them in descending order.
The program should do the following:
■ Open a text file and read its content into an array of characters. The array should be limited to 1000 characters. MARS provides the system calls for opening and reading from a text file.
■ Traverse the array character by character. Convert each decimal string into binary. A decimal string consists of one or multiple decimal characters. It should terminate by white space or a newline character. Ignore and skip all other characters. Store all the decimal integers into an array of words. The size of the integer array should be limited to 100 words.
■ Sort the integer array in descending order.
■ Display the sorted array
Actualy I have no problem with sorting the array since I have it, but the problem with dealing with the text file, reading from it, converting to decimal plugging in the array.
Do you have any ideas ? comments ? suggestions ?
Thx in advance
Update: some has asked what is the question?
the question is how to read from a txt file, convert the numbers to decimal ? this is the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,,,我将发布与您的问题类似的问题的解决方案的一些部分(虽然很复杂),,,
数据初始化
从文件读取并保存到缓冲区的代码
至于转换为十进制数,,, 我在 2 年前编写了这个过程,将字符串转换为浮点数(它还读取指数,即 2.23E5),,, 在您的情况下,您需要将字符串转换为十进制数,其中更容易,,,你应该能够弄清楚如何自己做,给出以下过程,,,
现在剩下的是从文件缓冲区读取,,,循环遍历字符,,,将它们转换为十进制数字、、、并将它们保存到数组中。
Ok ,,, I will post some parts of the solution to a problem that is similar to yours (much complicated though) ,,,
Data Initialization
Code to read from a file and save into a buffer
As for converting to decimal numbers ,,, I've written this procedure 2 years ago to convert a string to a floating point number (it also reads exponents i.e. 2.23E5) ,,, In your case you need to convert a string to a decimal which is much easier ,,, you should be able to figure out how to do it on your own giving the following procedure ,,,
Now what's left is reading from the file buffer ,,, looping through the chars ,,, converting them to decimal numbers ,,,, and saving them into the array.
使用 $v0=13,$a1=0,$a2=0 的系统调用来打开文件。
然后使用 $v0=14 的系统调用将其读入缓冲区。
Use syscall with $v0=13,$a1=0,$a2=0 to open a file.
Then use syscall with $v0=14 to read it into a buffer.