You are to write an ARM assembly program that uses the Sieve of Eratosthenes (er
ID: 3741775 • Letter: Y
Question
You are to write an ARM assembly program that uses the Sieve of Eratosthenes (er-uh-tos-thuh-neez) algorithm to calculate all prime numbers less than or equal to N, where N is a named constant. A good description of the algorithm can be found at http://www.geeksforgeeks.org/sieve-of-eratosthenes/. The program below is a C++ version of the solution. The first N+1 bytes of memory beginning at address 0x40000000 will correspond to the bool prime[N+1] array. Each entry in this array will be of type DCB. The first available full-word boundary address after the end of the prime array will corresponds to the start of the vector primes data store. Each entry in this data store will be of type DCD. Obviously your program will be unable to actually display the prime numbers as does the C++ version. Submit your .s file through Canvas. // C++ program to calculate all primes less than or equal to // N using Sieve of Eratosthenes. The primes are stored in a vector #include #include using namespace std; int main() { // Create a boolean array "prime[0..N]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. const unsigned int N = 100; bool prime[N+1]; for(unsigned int i=0; i primes; // Save all primes in a vector for (unsigned int p=2; p<=N; p++) { if (prime[p] == true) primes.push_back(p); } // Display the contents of the vector for (unsigned int prime : primes) cout << prime << endl; return 0; }
Explanation / Answer
c++ program
#include iostreamh
using namespace std;
int main()
{
int N=100;
bool arr[100] = {false};
cout << "enter array numbers ";
for (int i = 0; i < N; i++)
{
cin>>arr[i];
}
for (int j=2; j*j<=N; j++)
{
// If arr[j] is not changed, then it is a prime
if (arr[j]== true)
{
for (int i=j*2; i<=N; i += j)
arr[i] = false;
}
}
// Print all prime numbers
for (int j=2; j<=N; j++)
if (arr[j])
cout << j << " ";
return 0;
}
ARM code
ORG 0000h
LJMP MAIN
ORG 40h
MAIN: MOV R1,#0XA0 ; memory address from where all numbers to be checked are stored
MOV R0,#100H ; memory address to store all the prime numbers from an array.
MOV R2,#100 ; length of array
LABEL5:MOV A,@R1
MOV B,#02
DIV AB ; Dividing the number by 2
MOV R3,A
CJNE R3,#01H,LABEL2 ; Checking whether the number is 2
MOV @R0,A
INC R0
SJMP LABEL4
LABEL1:DEC R3 ; decrementing and checking whether the number is not divisible by all possible values of number/2
CJNE R3,#01H,LABEL2
MOV B,@R1
MOV @R0,B ; moving the prime number to the array
INC R0
SJMP LABEL4
LABEL2:MOV A,@R1
MOV B,R3
DIV AB
MOV R4,B
CJNE R4,#0H,LABEL1 ; checking whether the number is divisible any number
LABEL4:INC R1
DEC R2
CJNE R2,#0H,LABEL5 ; verifying whether the counter has reacged to zero
END
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.