Difference between revisions of "CSC231 Homework 10 Solution 2010"
Line 2: | Line 2: | ||
---- | ---- | ||
− | + | ||
<code><pre> | <code><pre> | ||
; findPrime.asm | ; findPrime.asm | ||
Line 120: | Line 120: | ||
</pre></code> | </pre></code> | ||
− | + | ||
<br /> | <br /> |
Revision as of 09:56, 15 December 2010
--D. Thiebaut 05:29, 8 December 2010 (UTC)
; findPrime.asm
; D. Thiebaut
;
; this program counts the number of primes number less than 1,000,000.
;
; to assemble and run:
;
; nasm -f elf -F stabs asm_io.asm
; nasm -f elf -F stabs findPrime.asm
; gcc -c driver.c
; gcc asm_io.o findPrime.o driver.o -o findPrime
;
; C++ version of the program
; --------------------------
;#include<iostream>
;
;using namespace std;
;
;bool isPrime( int n );
;
;int main(){
; int N = 1000000;
; int count = 0;
;
; for ( int n = 2; n < N; n++ )
; if ( isPrime( n ) )
; //cout << n << endl;
; count++;
;
; cout << count << endl;
; return 0;
;}
;
;bool isPrime( int n ) {
; for ( int i=2; i*i <= n ; i++ )
; if ( n % i == 0 )
; return 0;
;
; return 1;
;}
;
%include "asm_io.inc"
section .data
; count dd 0 ; number of primes found
n dd 0 ; variable tested for prime property
i dd 0 ; index of for loop
N equ 2000000
section .text
global asm_main
asm_main:
%define count edi
mov count, 0 ; initialize count
; for ( int n = 2; n < N; n++ )
; if ( isPrime( n ) )
; //cout << n << endl;
; count++;
mov ecx, 2 ;ecx is n
forn: cmp ecx, N
jge endForn
if:
;; isPrine(n )
%define ip_ret eax
%define ip_n ecx
%define ip_i esi
mov ip_i, 2
.for: mov eax, ip_i ; compare n to i*i
mul ip_i
cmp eax, ip_n
ja .endFor
.if: xor edx,edx ; divide n by i
mov eax, ip_n
div ip_i
cmp edx, 0 ; test modulo (remainder)
jne .end_if
.ret0:
jmp .endIsPrime
.end_if:
inc ip_i
jmp .for
.endFor:
inc count
.endIsPrime:
endif:
inc ecx
jmp forn
endForn:
mov eax, count ; print count
call print_int
call print_nl
ret