From: kuroikaze on
Okay here's the re-written overflow-free error code.

Unfortunately it doesn't work right.

1059 is my "control" test and the answers that should come out are not
what come out of my program.

When I put in 1059 I should get 1051 and 1061 are the two primes.
Instead I get 1053 and 1060?!?


..386
..MODEL FLAT

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h ;header file for input/output

cr EQU 0dh ;carriage return character
Lf EQU 0ah ;line feed

..STACK 4096 ;reserve 4096-byte stack

..DATA ;reserve storage for data

prompt1 BYTE "Please enter a four digit integer: ", 0
prompt2 BYTE cr, Lf, " is not a prime number.", 0
prompt3 BYTE cr, Lf, " is a prime number.", 0
prompt4 BYTE cr, Lf, "Largest prime number smaller than ", 0
prompt5 BYTE cr, Lf, "Smallest prime number larger than ", 0
string BYTE 11 DUP (?)
orig DWORD ?
lower DWORD ?
higher DWORD ?


..CODE
_start:

mov ebx, 2
output prompt1
input string, 6
atod string ; put input in eax
mov orig, eax ; copy eax to orig
mov ecx, eax

origtest:

cmp ebx, eax ; subtract input from ebx
je origprime ; if input and ebx are = then jump
XOR edx,edx ; zero out edx
div ebx ; divide eax by ebx
cmp edx, 0 ; sub eax minus 0
je orignotprime ; if 0 and edx are = then jump
inc ebx ; increment ebx register
mov eax, ecx ; put the original test back in eax
jmp origtest ; restart loop

lowerloop:

sub ecx, 1
jmp lowerprimetest

lowerprimetest:

mov eax, ecx
cmp ebx, eax
je lowerprime
XOR edx,edx
div ebx
cmp edx, 0
je lowerloop
inc ebx
jmp lowerprimetest

lowerprime:

mov lower, ecx
mov ecx, orig
jmp highestloop

highestloop:

add ecx, 1
jmp higherprimetest

higherprimetest:

mov eax, ecx
cmp ebx, eax
je higherprime
XOR edx,edx
div ebx
cmp edx, 0
je highestloop
inc ebx
jmp higherprimetest

higherprime:

mov higher, ecx
jmp finalout

origprime:

mov ebx, 2
dtoa string, ecx ; Convert ecx to Ascii
output string ; Output: ecx
output prompt2 ; is a prime number
jmp lowerloop

orignotprime:

mov ebx, 2
dtoa string, ecx
output string
output prompt3 ; is not a prime number
jmp lowerloop

finalout:

output prompt4 ; Largest number smaller than
mov eax, lower
dtoa string, eax
output string
output prompt5 ; Smallest number greather than
mov eax, higher
dtoa string, eax
output string


INVOKE ExitProcess, 0

PUBLIC _start

END







From: kuroikaze on
Actually I just saw you put up another example.

I'll try to compare and see what I fubar'd.

Thanks again!

From: kuroikaze on
Funny note: The code above works somewhat correctly for the number
10. It reports 7 and 11 as the two nearest primes.

However 100% of the time it says the original number is prime. I'm
not sure why.

From: kuroikaze on
On Oct 23, 12:30 pm, kuroik...(a)gmail.com wrote:
> Actually I just saw you put up another example.
>
> I'll try to compare and see what I fubar'd.
>
> Thanks again!

Hmm I thought I just posted this but I don't see it...I'll try again.

Basically it seems to denote whatever number I put in as prime.
Also if I put in 10 it is correctly spitting back 7 and 11 as the two
nearest primes.
Something goes wrong with the bigger numbers I guess?

From: Frank Kotler on
kuroikaze(a)gmail.com wrote:
> prompt2 BYTE cr, Lf, " is not a prime number.", 0
> prompt3 BYTE cr, Lf, " is a prime number.", 0

....
> output prompt2 ; is a prime number

....
> output prompt3 ; is not a prime number

This may have something to do with the strange results.

This is an example of the helpfulness of "full-talking names" or
"meaningful" names. If you'd called your strings "is_prime_message" and
"not_prime_message" this would have been easy! :)

Best,
Frank
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: DynatOS 64-bit is here!!!
Next: Slow nasm