c - For 循环始终返回 not_prime 程序集 X86 32 位

标签 c assembly x86 primes

我对组装很陌生。我们正在使用 32 位程序集 x86。我正在尝试将此 C 代码重构为程序集。

//C code
    for (i=2; i*i<=n; i++){
      if ((n%i)==0){
        is_prime=false;
          break;
       }
}
//=================================================
;ASSEMBLY CODE
;eax will contain the read in integer
forLoop:
mov edx, eax ; copy eax into edx
imul eax, edx ; multiply eax by edx value stored in eax
mov edx, 0 ; set remainder to 0
cmp eax, ecx ; if eax * eax
jg skip ; if greater than jump to skip.
mov ebx, eax ;  move to divide
div ebx ; restore eax
mov edx, 0 ; set divisor to 0
mov ebx, eax ; copy iterator to divisor
push eax ; save iterator
mov eax, ecx ; copy n to numerator
div ebx ; divide EAX/EBX
pop eax ; restore iterator
add eax, 1 ; inc iterator
cmp edx, 0 ; compare divisor to 0
jne forLoop
mov dword [ebp-4], 0
jmp skip

Sample Output
1
not prime
2
prime
3
prime
4
not prime 
5
not prime
6
not prime
7
not prime
8
not prime
9
not prime
0

所以 for_loop 是唯一将我的 bool 值本质上设置为 false 并使其 print_not_prime 的东西。因此,我认为当我除 edx 的余数时没有正确设置!我很困惑。

最佳答案

mov ebx, eax; div ebx 将始终为您提供 1,因为您将 eax 除以自身。无论如何,如果你不将其归零,我仍然会在 edx 中,你可以将其复制回来。或者更好的是,不要在 eax 中破坏它,只需将 i*i 放入 edx 或任何地方(即 imul edx、eax)。 - clown

关于c - For 循环始终返回 not_prime 程序集 X86 32 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35821040/

相关文章:

assembly - 加载数字流时避免缓存污染

c - 对话框创建和销毁循环会增加内存使用量

c++ - 从数组中选择一个数字

c - 为什么在将整数分配给指针时最好使用强制转换?

assembly - 英特尔在哪里记录了 ModR/M 字节中扩展寄存器 (R8-R15) 的编码?

c++ - Visual Studio中所谓的 "Frames"是什么?

windows - 在 32 位 Windows 上使用 NASM 汇编创建一个 exe 文件

c - 如何使用格式字符串漏洞从堆栈中读取任意指针?

c - 逻辑运算符、表达式和条件语句( bool 值)

assembly - 相当于 Gfortran 中的 asm volatile ?