开发者

Comparing strings in MIPS assembly

开发者 https://www.devze.com 2023-04-12 04:19 出处:网络
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

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 i开发者_Go百科s 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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号