c - Assembly + C - 排序结构

标签 c assembly x86 nasm

我正在从事一个同时使用 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/

相关文章:

graphics - 英特尔芯片组中的 "graphics memory aperture base"是什么?

linux - 在 nasm 汇编 intel x86 中获取数字而不是 Ascii

c++ - 从字符串中解析数字

转换两个指针(float* 到 int*)

将字符串与c中的字符串列表进行比较

C++ __usercall Hook

c - 将数据写入 C 中限制大小的文本文件

c - 鼠标初始化后如何从端口 60h 提取 3 或 4 字节鼠标数据包

performance - 为什么jnz不算循环?

组装:打印时无意中添加十六进制数字