Re: Prime number in assembly?

From: psi (i_at_m.t)
Date: 07/13/04


Date: Tue, 13 Jul 2004 21:34:10 GMT


>On 10 Jul 2004 21:25:44 -0700, gyps_rueppelli@yahoo.com (Gyps) wrote:
>>Hello...
>>I would like to know if there is an efficient way to compute
>>in assembly, prime numbers within a specified range...say from
>>50,000 to 100,000 or anything else.
>>
>>I could certainly implement the standard 'Sieve' method but
>>it appears to be inefficient. Is there a better method. I am
>>currently using NASM...
>>
>>Thanks!
This could be ok but
if
char *a="-99";
unsigned z;
Why sscanf(a,"%u", z); seems take '-'?
It should be no-read (return 0)
so it is not ok
prog.exe 78 -999
    
; nasmw -f obj file.asm
; bcc32 file.obj
    
    extern _printf
    extern _fflush
    extern _sscanf
    
section _DATA public align=4 class=DATA use32
    msg_usage db "This should find prime numbers: [CRTR-C] to stop."
, 13 , 10
    db "Usage: %s min_range max_range" , 0 , 0
    
    te db "a" , 0 , 0
    tb db "b" , 0 , 0
    
    is2 db "2 " , 0 , 0
    nl db 13 , 10 , 0 , 0
    
    msg_s db "%s " , 0 , 0
    
    msg1 db "%u " , 0 , 0
    msgi db "i=%u " , 0 , 0
    msgj db "j=%u " , 0 , 0
    
    fmt1 db "%u" , 0 , 0
    intt1 dd 0h , 0h
    intt2 dd 0h , 0h
    
    buf dd 0h , 0h
    buf1 dd 0h
    buf2 dd 0h , 0h
    
    
section _BSS public align=4 class=BSS use32
    group DGROUP _BSS _DATA
    
section _TEXT public align=1 class=CODE use32
    global _main
    
_main:
    push ebp
    mov ebp, esp
    pusha
    mov ebx, [ebp + 12]
    cmp dword [ebx+4], 0
    je .use
    cmp dword [ebx+8], 0
    je .use
    push dword intt1
    push dword fmt1
    push dword [ebx+4]
    call _sscanf
    add esp, 12
    cmp eax, 0
    jg .con1
    jmp .use
.con1:
    push dword intt2
    push dword fmt1
    push dword [ebx+8]
    call _sscanf
    add esp, 12
    cmp eax, 0
    jg .con3
    jmp .use
.con3:
    push dword [intt2]
    push dword [intt1]
    call findm
    jmp .fine123
.use:
    cmp dword [ebx], 0
    je .fine123
    push dword [ebx]
    push dword msg_usage
    call _printf
    add esp, 8
.fine123:
    push nl
    call _printf
    add esp, 4
    popa
    mov eax, 0
    mov esp, ebp
    pop ebp
    ret
    
    
; void find(uint arg1, uint arg2)
; 0 k, 4 j,8 i,12 r, 16 b, 20 a, 24 Ra, 28 arg1, 32 arg2 */
findm:
    push eax
    push ebx
    push edx
    push esi
    push edi
    push ebp
    mov esi, [esp+28]
    mov ebx, [esp+32]
    cmp esi, 0
    jne .aa
    inc esi
.aa:
    cmp esi, 1
    jne .aa0
    inc esi
.aa0:
    cmp esi, ebx
    jbe .a0
    jmp .fine
.a0:
    cmp ebx, 2
    jae .a1
    jmp .fine
.a1:
    cmp esi, 2
    ja .wl
    push is2
    call _printf
    add esp, 4
    cmp ebx, 2
    je .fine
.wl:
    test esi , 0x1
    jnz .wlooo
    inc esi
.wlooo:
    
    mov [buf], esi
    fild dword [buf]
    fsqrt
    fistp dword [buf] ; a=sqrt(arg)
    mov edi, [buf]
    mov ebp, 3
.wloop:
    cmp ebp, edi
    ja .wloopf
    mov edx, 0
    mov eax, esi
    div ebp
    cmp edx, 0
    je .fin
    add ebp, 2
    jmp short .wloop
    
.wloopf:
    push esi
    push msg1
    call _printf
    add esp, 8
    cmp eax, 0
    jle .fine
.fin:
    add esi, 2
    cmp esi, ebx
    ja .fine
    jmp .wlooo
    
.fine:
    pop ebp
    pop edi
    pop esi
    pop edx
    pop ebx
    pop eax
    ret 8
    

....................
/* nasmw -f obj file.asm
/* bcc32 file.obj

extern _printf
extern _fflush
extern _sscanf

section _DATA public align=4 class=DATA use32
msg_usage db "This should find prime numbers: [CRTR-C] to stop.",
13, 10
            db "Usage: %s min_range max_range", 0, 0

te db "a", 0, 0
tb db "b", 0, 0

is2 db "2 ", 0, 0
nl db 13, 10, 0, 0

msg_s db "%s ", 0, 0

msg1 db "%u ", 0, 0
msgi db "i=%u ", 0, 0
msgj db "j=%u ", 0, 0

fmt1 db "%u", 0, 0
intt1 dd 0h, 0h
intt2 dd 0h, 0h

buf dd 0h, 0h
buf1 dd 0h
buf2 dd 0h, 0h

section _BSS public align=4 class=BSS use32
group DGROUP _BSS _DATA

section _TEXT public align=1 class=CODE use32
global _main

_main:
{< k;
 k=s
 pusha
      b=[k + 12]; /* finds argv [b]=argv[0], [b+4]=argv[1] etc */
  /* here it seems _sscanf or _printf
     change edx and-or ecx is it true? */
      w2@ [b+4]==0 ? .use; w2@ [b+8]==0 ? .use;
      _sscanf<( a < w2@ [b+4], w2@ fmt1, w2@ intt1);
       a<=0 !! .con1| jmp .use
     .con1:
      _sscanf<( a < w2@ [b+8], w2@ fmt1, w2@ intt2);
      a<=0 !! .con3| jmp .use
     .con3:
      findm( 0 < w2@ [intt1], w2@ [intt2]);
      jmp .fine123
  .use:
   w2@ [b]!=0 ! .fine123
        _printf<( a < w2@ msg_usage, w2@ [b]);
  .fine123:
   _printf<( a < nl );
 popa
 a = 0;
 s = k;
> k
  ret;
 }

/* void find(uint arg1, uint arg2)
/* 0 k, 4 j,8 i,12 r, 16 b, 20 a, 24 Ra, 28 arg1, 32 arg2 */
findm:
{< a, b, r, i, j, k;
    i=[s+28]; b=[s+32]
    i==0 ! .aa; ++i
   .aa:
    i==1 ! .aa0; ++i
   .aa0:
    i>b ! .a0; jmp .fine
   .a0:
    b<2 ! .a1; jmp .fine
   .a1:
    i>2 ? .wl
    _printf<( a < is2 );
    b==2 ? .fine
   .wl:
    test i, 0x1
    jnz .wlooo
         ++i
   .wlooo:
       {
        [buf]=i|fild w2@ [buf]|fsqrt
        fistp dword [buf]|j=[buf] /* a=sqrt(arg)
        k = 3;
       .wloop:
               {k>j ? .wloopf
                r = 0; a = i
                div( r a > r, a, k);
                r==0 ? .fin
                k+=2 | jmp short .wloop
               }
       .wloopf:
        _printf<( a < msg1, i); a<=0 ?? .fine;
       .fin:
        i+=2 | i>b ? .fine | jmp .wlooo
       }
    .fine:
> a, b, r, i, j, k;
 ret 8;
}



Relevant Pages


Loading