Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / ASM

An explorative peek into generating deterministic primes through a multi-level multi-language tunneling apparatus

, ,
Rate me:
Please Sign up or sign in to vote.
4.99/5 (33 votes)
2 Apr 2010CPOL2 min read 85.5K   117   21   39
This paper is a summary of a research exercise conducted in conjunction by Nish Sivakumar, Professor Cuthbert Calculus, and Glenn Quagmire.

Image 1

Image 2

This article was written as a joke for April Fools Day 2010. Readers are advised not to take the contents of this article too seriously.

Introduction

This paper is a summary of a research exercise conducted in conjunction by Nish Sivakumar, Professor Cuthbert Calculus, and Glenn Quagmire. The paper describes an explorative peek into generating deterministic primes through a multi-level multi-language tunneling apparatus. The example code is kept simple enough so as to keep focus on the fundamental problem we are attempting to solve. At the core, we start off with an inline assembler method consumed by an exported C function, wrapped and exported as a C++ class that's then consumed and exposed to the CLR through a C++/CLI wrapper, which is then used by a C# COM DLL that exposes an interface that is consumed through VBScript and executed through a command scriplet. 

Inline assembly

ASM
int GenerateIndex(int num)
{
  _asm
  {
    mov eax, dword ptr [num]
    xor edx, edx
    mov ecx, 3
    idiv ecx
    mov eax, edx
  }
}

This is the core randomization code and it's in assembly to keep things simple and lightweight. In the example we restrict the number of cached prime numbers to 3 and hence the divisor in the code.

C DLL Export

C++
int __stdcall DllMain()
{ 
  return 0;
}

int primes[] = { 29, 37, 61 };

__declspec(dllexport) int GetDetPrime()
{
  return primes[GenerateIndex(GetTickCount())];
}

The C DLL essentially exports a simple function that returns a deterministic prime, and the code internally delegates the randomization logic to the assembler code. At this point the library is consumable by any framework or layer capable of calling a C DLL.

C++ class Export

C++
// The header file
class __declspec(dllexport) CDetPrimeLib 
{
public:
  int GetDetPrime();
};

// Implementation file
#pragma comment(lib, "DetPrime.lib")

extern "C" __declspec(dllimport) int GetDetPrime();

int CDetPrimeLib::GetDetPrime()
{
  return ::GetDetPrime();
}

Here we wrap the C DLL and export it as a C++ class to natively support the most popular programming language in the world. It's a very thin wrapper since we don't want to add any overhead at this point.

CLI wrapper using C++/CLI

MC++
#pragma once

#pragma comment(lib, "DetPrimeLib.lib")

class __declspec(dllimport) CDetPrimeLib 
{
public:
  int GetDetPrime();
};

namespace DetPrimeManWrap 
{
  public ref class DetPrimeManaged
  {
    CDetPrimeLib* pNative;

  public:
    DetPrimeManaged()
    {
      pNative = new CDetPrimeLib();
    }

    ~DetPrimeManaged()
    {
      this->!DetPrimeManaged();
    }

    !DetPrimeManaged()
    {
      delete pNative;
    }

    int GetDetPrime()
    {
      return pNative->GetDetPrime();
    }
  };
}

While we have already provided enough API exposure to be considered mainstream, it makes good sense to also directly support .NET and thus we have a C++/CLI wrapper library that exposes the functionality to managed callers.

COM wrapper using C#

C#
[assembly: ComVisible(true)]
[assembly: Guid("6b4bf847-a45d-4f87-ba53-0d4a9fffa975")]
[assembly: AssemblyVersion("1.0.1.1")]
[assembly: AssemblyFileVersion("1.0.1.1")]

namespace DetPrimeCOM
{
    using DetPrimeManWrap;

    [Guid("18535B5E-0561-4BA8-8CF6-85B148F3A4CF")]
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IDetPrime
    {
        int GetDetPrime();
    }

    [Guid("2299461C-1FFB-4D5D-A521-0830F93FB5CC")]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class DetPrime : IDetPrime
    {
        public int GetDetPrime()
        {
            return new DetPrimeManaged().GetDetPrime();
        }
    }
}

Since we support .NET we may as well support COM too and that's what the above C# code does. The code is now ready for COM consumption.

VBScript caller

VBScript
Dim detPrime 
Set detPrime = CreateObject("DetPrimeCOM.DetPrime")
Wscript.Echo "The generated Deterministic Prime is " & detPrime.GetDetPrime

VB6 is now officially extinct and so we resort to a simple VBScript script to invoke the COM library.

Command scriplet

@%windir%\syswow64\cscript.exe /nologo DetPrime.vbs

And the last piece in our architecture is the command scriplet which executes the VBScript through cscript.exe so we can run it on a 64 bit OS, even though the code is 32 bit to support the vast majority of frameworks out there.

Conclusion

Image 3

This article was written as a joke for April Fools Day 2010. Readers are advised not to take the contents of this article too seriously.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Written By
Program Manager
United States United States
I am a former airline pilot who's currently doing research on deterministic primes under Professor Calculus.

Written By
Architect
United States United States
I am a multiple PhD with a deep interest in science. My recent research has been in the field of deterministic prime numbers.

Comments and Discussions

 
QuestionMy Vote of 5 Pin
thatraja31-Mar-14 20:56
professionalthatraja31-Mar-14 20:56 
AnswerRe: My Vote of 5 Pin
Nish Nishant2-Apr-14 4:06
sitebuilderNish Nishant2-Apr-14 4:06 
GeneralMy vote of 5 Pin
gndnet7-Nov-12 7:44
gndnet7-Nov-12 7:44 
GeneralMy Vote of 5 Pin
thatraja1-Apr-11 5:44
professionalthatraja1-Apr-11 5:44 
GeneralRe: My Vote of 5 Pin
Nish Nishant1-Apr-11 9:29
sitebuilderNish Nishant1-Apr-11 9:29 
JokeExcellent article Pin
thatraja14-Jan-11 3:40
professionalthatraja14-Jan-11 3:40 
GeneralRe: Excellent article Pin
Nish Nishant14-Jan-11 3:53
sitebuilderNish Nishant14-Jan-11 3:53 
GeneralNever tell the same joke twice Pin
User 5924122-Dec-10 16:25
User 5924122-Dec-10 16:25 
GeneralWhoa Pin
Andy Brummer2-Apr-10 5:13
sitebuilderAndy Brummer2-Apr-10 5:13 
GeneralRe: Whoa Pin
Nish Nishant2-Apr-10 5:22
sitebuilderNish Nishant2-Apr-10 5:22 
GeneralRe: Whoa Pin
Pete O'Hanlon2-Apr-10 9:09
subeditorPete O'Hanlon2-Apr-10 9:09 
GeneralPlzzzz Help Pin
Abhinav S2-Apr-10 4:45
Abhinav S2-Apr-10 4:45 
GeneralRe: Plzzzz Help Pin
Nish Nishant2-Apr-10 4:46
sitebuilderNish Nishant2-Apr-10 4:46 
GeneralRe: Plzzzz Help Pin
Abhinav S2-Apr-10 5:16
Abhinav S2-Apr-10 5:16 
GeneralNice - but impressive ! Pin
Emilio Garavaglia1-Apr-10 21:22
Emilio Garavaglia1-Apr-10 21:22 
GeneralRe: Nice - but impressive ! Pin
Nish Nishant2-Apr-10 2:02
sitebuilderNish Nishant2-Apr-10 2:02 
GeneralThis could lead to man love. Pin
Pete O'Hanlon1-Apr-10 9:27
subeditorPete O'Hanlon1-Apr-10 9:27 
GeneralRe: This could lead to man love. Pin
Nish Nishant1-Apr-10 12:34
sitebuilderNish Nishant1-Apr-10 12:34 
GeneralExcellent Pin
Douglas Troy1-Apr-10 9:16
Douglas Troy1-Apr-10 9:16 
GeneralRe: Excellent Pin
Nish Nishant1-Apr-10 12:34
sitebuilderNish Nishant1-Apr-10 12:34 
GeneralMemory hog... Pin
Mike Rich1-Apr-10 8:37
Mike Rich1-Apr-10 8:37 
GeneralRe: Memory hog... Pin
Nish Nishant1-Apr-10 12:33
sitebuilderNish Nishant1-Apr-10 12:33 
GeneralBrilliant stuff! Pin
Ravi Bhavnani1-Apr-10 7:32
professionalRavi Bhavnani1-Apr-10 7:32 
GeneralRe: Brilliant stuff! Pin
Nish Nishant1-Apr-10 12:33
sitebuilderNish Nishant1-Apr-10 12:33 
GeneralMy vote of 1 Pin
sam.hill1-Apr-10 4:54
sam.hill1-Apr-10 4:54 
Bah, Humbug

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.