简体   繁体   English

比较MIPS汇编中的字符串

[英]Comparing strings in MIPS assembly

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? 如果我要从数组中提取2个字符串,是否可以将它们进行比较,以查看在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! 谢谢!

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. 如果结果为0,则它​​们相等。 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. 如果不是,则结果> 0,则第一个字符串在另一个字符串之前,否则第二个字符串在下面,您将交换它们。 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. 如果您用尽了一个字符串,而在此之前它们都相等,则较短的字符串将小于较长的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM