比较 MIPS 汇编中的字符串
我在数据段中定义的数组中有一堆字符串。如果我要从数组中取出 2 个字符串,是否可以比较它们以查看哪个具有更大的 mips 值?我该怎么做?基本上,我希望根据字母顺序重新排列字符串。
编辑:这不是我试图获得特定问题的帮助,而更多的是一个一般性问题,它将帮助我处理代码。谢谢!
I have a bunch of strings in an array that I have defined in the data segment. If I were to take 2 of the strings from the array, is it possible to compare them to see which has a greater value in mips? How would I do this? Basically, I'm looking to rearrange the strings based on alphabetical order.
EDIT: This is less of me trying to get help with a specific problem, and more of just a general question that will help me with my approach to the code. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果是我,我会创建一个指向字符串的指针列表。即每个字符串的地址列表。然后,您将编写一个子例程来比较给定指针的两个字符串。然后,当您需要交换字符串时,只需交换实际的指针即可。
您希望避免交换字符串本身,因为它们很可能被紧密地封装在一起,因此您必须进行大量的移位才能移动内存的孔。指针的交换很简单。如果字符串都是固定长度(或更短),那么您可以更轻松地交换字符串,然后您就不必担心移动内存孔。
但对指针列表进行排序确实是热门技巧。
要比较字符串,最简单的方法是迭代每个字符串的每个字符,然后将它们相减。如果结果为 0,则它们相等。如果不是,那么如果结果是> 0,则第一个字符串位于另一个字符串之前,否则第二个字符串较低,您将交换它们。如果您在另一根之前用完任一字符串,并且它们一直相等,则较短的字符串将小于较长的字符串。
If it were me, I'd create a list of pointers to the strings. That is, a list of the addresses of each string. Then you'd write a subroutine the compares two strings given their pointers. Then, when you need to swap the strings, you simply swap the actual pointers.
You want to avoid swapping the strings themselves, since they may well be tightly packed, thus you'd have to do a lot of shifting to move the holes of memory around. Pointers are simple to swap. You could swap strings more easily if they were all of a fixed length (or less), then you wouldn't have to worry about moving the memory holes around.
But sorting the pointer list is really the hot tip.
To compare strings, the simplest way is to iterate over each character of each string, and subtract them from each other. If the result is 0, they're equal. If not, then if the result is > 0, then the first string is before the other string, otherwise the second string is lower and you would swap them. If you run out of either string before the other, and they're equal all the way to that point, the shorter string is less than the longer one.