我正在从事一个同时使用 x86 程序集 (NASM) 和 C 的项目。有一个用 Assembly 编写的子例程,它使用索引寻址模式来确定某一年 (int) 是小于还是大于另一年,然后根据结果返回 -1、1 或 0。看来,如果我输入超过 4 或 5 条记录,它就无法正确排序。我花了几个小时通过 gdb 运行它,并发现在第一次递增 j 的最后一次迭代中(在 i 递增之前),它运行交换,即使它不应该运行,但我不确定如何修复它。提前感谢您的任何想法。
---C代码---
最佳答案
我明白了。这是因为我在错误的位置设置了 min = i 。应该是这样的:
for (i = 0; i < numBooks - 1; i++) {
/*** WAS HERE ***/
for (j = i + 1; j < numBooks; j++) {
/*** SHOULD BE HERE ***/
min = i;
/* Copy pointers to the two books to be compared into the
* global variables book1 and book2 for bookcmp() to see
*/
book1 = &books[i];
book2 = &books[j];
cmpResult = bookcmp();
/* bookcmp returns result in register EAX--above saves
* it into cmpResult */
/* book2 comes before book1, in other
words, book1 is greater - value stored in
eax will in this case be 1 */
if (cmpResult == 1) {
min = j;
}
if (min != i) {
tempBook = books[i];
books[i] = books[min];
books[min] = tempBook;
}
}
}
}
关于c - Assembly + C - 排序结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40066650/