Author Topic: Prime Number Example  (Read 24799 times)

Nathan

  • Guest
Prime Number Example
« on: October 15, 2008, 05:39:25 AM »
This place *needs* examples.  Here is one.

; For Windows:
;   nasm -f win32 --prefix _ -o prime.obj prime.asm
;   gcc -o prime.exe prime.obj
;
; For Linux:
;   nasm -f elf32 -o prime.o prime.asm
;   gcc -o prime prime.o

section .data
msg:   db   'Enter an integer: ', 0
chr:   db   '%d', 0
ispmsg:   db   10, 'The integer %d is prime!', 10, 0
nopmsg:   db   10, 'The integer %d is not prime.', 10, 0

section .text
   global main
   extern printf, scanf

main:

push ebp
   mov ebp, esp
   sub esp, $0c

mov dword [ebp-8], 1
   push msg
   call printf
   add esp, 4

lea eax, [ebp-4]
   push eax
   push chr
   call scanf
   add esp, 8

cmp dword [ebp-4], 2
   jge next
   mov dword [ebp-8], 0
   jmp continue

next:
   jne nextt
   jmp continue

nextt:
   mov eax, [ebp-4]
   cdq
   mov ebx, 2
   idiv ebx
   cmp edx, 0
   je nexttt

finit
   fild dword [ebp-4]
   fsqrt
   fistp dword [ebp-$0c]

mov ecx, 3
   jmp ccheck
floop:
   mov eax, [ebp-4]
   cdq
   idiv ecx
   cmp edx, 0
   jne check
   mov dword [ebp-8], 0
   jmp continue
check:
   add ecx, 2
ccheck:
   cmp ecx, [ebp-$0c]
   jle floop
   jmp continue

nexttt:
   mov dword [ebp-8], 0

continue:
   xor eax, eax
   cmp eax, [ebp-8]
   jne isprime
   mov eax, [ebp-4]
   push eax
   push nopmsg
   call printf
   add esp, 8
   jmp endit

isprime:
   mov eax, [ebp-4]
   push eax
   push ispmsg
   call printf
   add esp, 8

endit:
   add esp, $0c
   pop ebp
   xor eax, eax
   ret

nobody

  • Guest
Re: Prime Number Example
« Reply #1 on: July 20, 2009, 01:41:04 PM »
hey that s nice but not for beginners and if is that for beginners try to explain your code... seems more like experts code and is not going to help anybody...

havent you ever hear about comments?

with love,
Kostas

nobody

  • Guest
Re: Prime Number Example
« Reply #2 on: July 25, 2009, 02:47:57 PM »
That example contains nothing fancy.  It is all beginner material that is (mostly) explained in common tutorials:

http://www.drpaulcarter.com/pcasm/

http://www.daniweb.com/forums/thread41309.html