0
我有一个任务,我必须输入一个数字,并计算出所有素数达到但不超过该数字。例如,如果我在程序中输入了9,它应该打印3,5和7.x86 NASM汇编中使用div指令的浮点异常
我计划确定一个数是否为素数是将它除以2并检查余数是否为0.如果余数为0,程序从被除数中减1,然后循环回顶部再次除。如果余数!= 0,则将其打印到屏幕上,并再次递减被除数。发生这种情况直到股息为0为止。只是这不是发生的情况,无论什么原因,只要我使用DIV
指令,我总是会得到浮点异常,我似乎无法弄清楚为什么或如何解决它。任何人有任何想法,我如何解决这个问题?
Code: %INCLUDE "csci224.inc" SEGMENT .data prompt: DD "Please enter a number: ",0 ; prompt string message: DD " is prime.", 0 ; displays when n is prime invalid: DD "Invalid entry.", 0 i: DD 2 SEGMENT .bss input: RESD 100 ; not really necessary, ignore this SEGMENT .text main: mov edx, prompt call WriteString call ReadInt mov esi, eax ; move eax into esi to use as index for loop myloop: xor edx, edx ; clear registers xor ecx, ecx xor eax, eax mov eax, dword 2 ; mov 2 to eax div ecx ; ecx/eax | n/2 dec esi ; decrement loop counter dec ecx ; decrement numerator cmp edx, dword 0 ; is remainder zero? je myloop ; YES - not prime - jump back to top mov eax, edx ; NO - move to eax and print call WriteInt call Crlf cmp esi, 0 ; is counter zero? jz finished ; YES - end loop jmp myloop ; NO - loop again finished: ret