使用div指令的x86 NASM程序集中的浮点异常
我有一个任务,我必须输入一个数字,并找出所有素数达到但不超过这个数字。 例如,如果我在程序中输入9,它应该打印3,5和7。
我计划确定一个数是否为素数是将它除以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
在这部分代码中:
xor ecx, ecx ; Clears ecx (set to 0)
xor eax, eax ; Clears eax (set to 0)
mov eax, dword 2 ; Sets eax to 2
; NOTE: you had just set it to 0 in the prior step)
; PROBLEM; the following code computes eax/ecx, which is 2/0 - the comment is wrong
div ecx ; ecx/eax | n/2
链接地址: http://www.djcxy.com/p/15897.html
上一篇: Floating point exceptions in x86 NASM assembly using div instruction