Coder Social home page Coder Social logo

primes's Introduction

Description

This is an implementation of the Sieve of Eratosthenes in x86-64 assembly. The program takes a single command line argument to specify an upper bound and prints all primes up to it.

I decided to take this on without any linking to stdlib, so I first needed to write a simple library of functions to do basic tasks like printing, allocating memory and parsing using Linux syscalls and raw assembly.

This is the largest assembly project I've taken on before having only used it prior to learn about computers at a low level. I'm happy with how it turned out and the amount I learned doing it, despite the frustrations of debugging it. I took the better part of a day to get it working correctly.

It gets all of the performance a sieve should get. It uses standard optimiztions like only checking up to the square root of the number, checking operating only on know primes, and skipping even numbers. It also uses a bit array to store the sieve lessening the memory requirements. The sieve, of all major prime generation algorithms, is the most time efficient but least memory efficient, so indexing into a bit array is a good compromise.

Concerns

  • I'm definitely not following calling conventions. I considered doing it, but I'm not writing a library that any other code will use, so I didn't want to deal with it too much. As a result, only one function makes any use of the stack for its variables, which is the one to print numbers. The rest use registers or allocated memory for everything.

  • I'm sure there are optimizations that could be made. I'm always amazed by how esoteric compiler output tends to be to get the best performance. Most of what I did what relatively straightforward... except the division by 10 in the print_num function. I got a little crazy with that one.

Usage

I ran it on an older laptop with an Intel i7-8550U CPU running Windows with WSL.

There's a build script included using as to assemble and ld to link.

./build.sh

Then it can be run:

./gen-primes <upper_bound>

This repo includes the output of:

./gen-primes 1000000 > primes.txt

primes's People

Contributors

hhammon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.