繁体   English   中英

装配 + C - 排序结构

[英]Assembly + C - Sorting Structures

我正在开发一个同时使用 x86 程序集 (NASM) 和 C 的项目。 有一个用汇编编写的子例程,它使用索引寻址模式来确定某一年 (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;
            }

          }
        }
    }

暂无
暂无

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

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