Skip to main content

ASM primenumber generator

I completed the prime number generator. The algoritme was correct but i forgot to clean rdx in more places then i thought. now it works. 10m numbers take 5 sec on the ASM implementation, and 2 sec on the c++ implementation. I oprimized the ASM version  by clearing RDX with xor rdx, rdx  instead of mov rdx, 0. That leaves the ASM version at 4 seconds. c++ compilers are great for sure! 

Full code:

Assembly.cpp:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Assembly.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <ctime>

using namespace std;

extern "C" int Invoke(void*, int);
extern "C" int Try();
bool isPrime(unsigned long);

int main(int argc, char** argv[])
{
 const int count = 10000000;
 int* res = new int[count];
 
 std::clock_t start;
 double durationASM;
 double durationCPP;


 cout << "Assembly started" << endl;
 cout << "Data and pointer created: " << res << endl;

 start = std::clock();

 int a = Invoke(res, count);

 durationASM = (std::clock() - start) / (double)CLOCKS_PER_SEC;

 for (int i = 0; i < 300; i++){
  cout << "answers[" << i << "] = " << res[i] << endl;
 }

 int c = 0;

 for (int i = 0; i < count; i++){
  res[i] = 0;
 }

 start = std::clock();

 for (int i = 0; i < count; i++){
  res[i] = 0;
  if (isPrime(i)){
   res[c] = i;
   c++;
  }
 }

 durationCPP = (std::clock() - start) / (double)CLOCKS_PER_SEC;

 cout << "ASM time over " << count << " numbers: " << durationASM << endl;
 cout << "C++ time over " << count << " numbers: " << durationCPP << endl;

 system("pause");

 return 0;
}

bool isPrime(unsigned long n) {
 if (n <= 3) {
  return n > 1;
 }

 if (n % 2 == 0 || n % 3 == 0) {
  return false;
 }

 for (unsigned long i = 5; i * i <= n; i += 6) {
  if (n % i == 0 || n % (i + 2) == 0) {
   return false;
  }
 }

 return true;
}
ASM.asm:


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
.data
; rax = math register
; rcx = loop count
; rbx = number to be checked
; r8 = result boolean
; r9 = division handler
; r15 = interal loop counter
; r14 math helper
; r10 = array pointer

.code
Invoke proc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ; function input parameters:
 mov r10, rcx ; save pointer
 mov rcx, rdx ; the number of numbers to be checked

 mov rax, 0  ; set rax = 0
 mov rbx, 0; number to test
 mov r8, 0 ; result: 1 = prime, 0 = normal
 
 mov r9, 0
 mov r11, 0
 mov r12, 0
 mov r13, 0
 mov r14, 0
 mov r15, 0

 MasterLoop:
  call IsPrime
  cmp r8, 1
  je SaveResult
  Continue:
  add rbx,1
 sub rcx, 1
 cmp rcx, 0 
 jne MasterLoop
 jmp cppReturn
 
 SaveResult:
  mov r8, 0
  mov dword ptr[r10], ebx
  add r10, 4
  jmp Continue

 cppReturn:
 mov rax, 0 ; 
 ret  ; 
Invoke endp

IsPrime proc
 Step1if:
  cmp rbx, 1
  je Exit
  cmp rbx, 2
  je Save
  cmp rbx, 3
  je Save
 Step1else:
  xor rdx, rdx ; clean rdx, else weird stuff s gonna happen
  mov rax, rbx
  mov r9, 2
  div r9
  cmp rdx, 0
  je Exit
  xor rdx, rdx  ; clean rdx, else weird stuff s gonna happen
  mov rax, rbx
  mov r9, 3
  div r9
  cmp rdx, 0
  je Exit

  mov r15, 5 ; init loop
 Step2ForLoop:
  mov rax, r15
  xor rdx, rdx  ; clean rdx, else weird stuff s gonna happen
  mul rax
  cmp rax, rbx
  jg Save
  ActualLoop:
   mov rax, rbx
   xor rdx, rdx  ; clean rdx, else weird stuff s gonna happen
   div r15
   cmp rdx, 0
   je Exit
   mov r14, r15
   add r14, 2
   mov rax, rbx
   xor rdx, rdx ; clean rdx, else weird stuff s gonna happen
   div r14
   cmp rdx, 0
   je Exit
  add r15, 6
  jmp Step2ForLoop

 Save:
  mov r8, 1
  jmp Exit

 Exit:
  ret
IsPrime endp

Try proc
 mov rax, 0
 mov r9, 3
 mov rbx, 5

 mov rax, rbx
 div r9
 ret
Try endp

end

[tag: c#] [tag: OpenGL] [tag: Tortoise2D Engine] [tag: 2D] [tag: Game Engine] [tag: ocdy1001] [tag: ocdy1001official]

Comments

Popular posts from this blog

Neural Network 0: handwriting learner and sin(x)

Here is a project from a while ago, it recognises characters:
https://www.youtube.com/watch?v=V8WA54l9ec8

This is the network class:


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;publicclass NeuralNetwork {publicdouble learnRate;publicdouble momentum;public List<Neuron> inputLayer;public List<Neuron> hiddenLayer;public List<Neuron> outputLayer;static Random random =new Random();public NeuralNetwork(int inputSize,int hiddenSize,int outputSize,string file =""){ learnRate =.9d; momentum =.04d; inputLayer =new List<Neuron>(); hiddenLayer =new List<Neuron>(); outputLayer =new List<Neuron>();for(int i =0; i < inputSize; i++) inputLayer.Add(new Neuron());for(int i =0; i < hiddenSize; i++) hiddenLayer.Add(new Neuron(inputLayer));for(int i =0; i < outputSize; i++) outputLayer.Add(new Neuron(hiddenLayer));if(file !=…

C# produceral terrain generation 3

I have been working on a raindrop algorithm for erosion on the heighmap. firstit went wrong: spikes is not what you want
The better version uses 150 * 150 * 150 * 8  raindrops for a total of 27M drops. this takes 8000 milli seconds on one core(3.8ghz). Second try, not very good On the third try a river was created naturally. this is more promising.
here is a Fault-terrain that was "raindropped" and smoothed and i added small detail noise. Again a rivver. i add some materials and a water plane for fun. doesnt look too bad.

Here i am experimenting with parameters in the raindrop function.
DONE: Get basic raindrop erosion setup TODO: tweak till i get decent and realistic results.
Cody Bloemhard [tag: c#] [tag: OpenGL] [tag: Tortoise2D Engine] [tag: 2D] [tag: Game Engine] [tag: ocdy1001] [tag: ocdy1001official] [tag: Unity] [tag: Unity3D] [tag: Unity5] [tag: 3D] [tag: Cody Bloemhard]

C# Produceral Terrain Generation 1

New Project:
DESIGNABLE - PRODUCERAL - TERRAIN - GENEARTION
Goals: - Generate random terain with noise - user input to get certain structures - use fractals to get more nature-like structures - use erosion to get even more accurate results - use weather to get biomes and direct erosion - export to heightmap - export to .obj
By designable i mean that if you need a big mountain in the middle for example, you can give the data and it generates a random terrain, but with the mountain(aslo random) always present in the right place, so you can have random terrain and design so the game doesnt break.
Progress. I making heighmaps now. the engine can produce maps in 24 bit. red is low green is mid and blue is high. there are around 16m heights. if you see lots of red, it still can me a big number on that pixel, because the blue is low and red is high, but blue counts more as 10000 is more than 10. so visualy it can be weird but evertthing is right example: RGB(234, 2, 4) this will result in a red color.…