Question
Write code in C that computes the value of pi using the Monte Carlo Method. Your code should get the number of darts to throw
Write code in C that computes the value of pi using the Monte Carlo Method. Your code should get the number of "darts" to "throw" from the first (and only) command-line argument
Your code should print the value of pi with 10 digits to the right of the decimal point.
Each dart will consist of two double-precision floating point numbers: an X coordinate and a Y coordinate. In order to generate the two random numbers you need for each dart, you will call the following function twice:
double drand48();
This function returns a random 64-bit floating point non-negative number whose value is in the interval [0.0 .. 1.0). Make sure to generate each dart's X coordinate first and then generate the Y coordinate.
Before you call drand48() the first time, you need to initialize the random number generator by calling:
srand48(0);
You should only call this function ONCE. The argument of 0 is required; no other value is allowed.
Both of the random number generation functions mentioned above require you to include stdlib.h.
YOU ARE REQUIRED TO USE THE RANDOM NUMBER GENERATION FUNCTIONS SPECIFIED ABOVE. THE USE OF ANY OTHER RANDOM NUMBER FUNCTION(S) WILL RESULT IN A GRADE OF 0.
To figure out whether a dart has landed within the circle, you will use the Pythagorean Theorem, d^2 == x^2 + y^2. ("^2" is used to denote "squared" in this formula. The symbol 'd' represents the distance of the "dart" from the center of the circle.)
Count darts whose 'd' value is <= 1 as being INSIDE the circle. Count darts whose 'd' value is > 1 as OUTSIDE the circle.
You will need to do the Pythagorean Theorem floating point calculations by writing a small amount of GAS assembler code. In this code, you are required to use only the syntax demonstrated in asm.c (attached below). You are not allowed to use any other GAS syntax, such as %0, or any specifier other than "=m" in an "output" or "m" in an "input."
YOU CANNOT USE ANY OTHER SYNTAX IN YOUR ASSEMBLY CODE.
You need to use assembly language to compute the value of 'd'. You do not need to use assembly language to determine whether the "dart" is within the circle. You can use an "if" statement for that.
Your code will get the number of darts to throw from the command line. Your code will be called like this to throw 10 darts: a04 10
-------------------------------------------------
// asm.c char charArray[] = {'a', 'b', 'c', 'd', 'e'}; int intArray[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; int main(int argc, char **argv) { int localVar = 0x12345678; double fpVar = 16.0; // embedded assembly language asm(" mov $0x89ABCDEF, %%eax " " mov $0xFF, %%bl " " movsx %%bl, %%eax " " movzx %%bl, %%eax " // two instructions in one asm statement // (not a good idea, but interesting that you can do this) " mov $0x12, %%al mov $0x34, %%ah " // swapping two values " mov $0xBBBBBBBB, %%ebx " " mov $0xAAAAAAAA, %%eax " " xchg %%ebx, %%eax " // extended asm " mov %[localVarIn], %%eax " // store localVar into eax " add $4, %%eax " // add 4 to eax " mov %%eax, %[localVarOut] " // copy register to localVar // int array " mov $3, %%ecx " // initialize ecx " lea %[intArray], %%ebx " // load address of start of array " mov (%%ebx), %%eax " // get first element of array " mov (%%ebx, %%ecx, 4), %%eax " // get indexed element of array // char array " mov $0, %%eax " // clear eax " lea %[charArray], %%ebx " // load address of start of array " mov (%%ebx), %%al " // get first element of array " mov (%%ebx, %%ecx, 1), %%al " // get indexed element of array // conditional statement " cmp $3, %[localVarIn] " // comparison operation " jg gt " // conditional jump if > 3 " nop " // didn't jump "gt: nop " // conditional jump target // floating point load, store " fldl %[fpVarIn] " // load fpVar onto floating point stack " fld %%st " // duplicate value on top of fp stack " faddp " // add the two values on the fp stack " fstpl %[fpVarOut] " // store the sum back into fpVar : [localVarOut] "=m" (localVar), // outputs [fpVarOut] "=m" (fpVar) : [localVarIn] "m" (localVar), // inputs [intArray] "m" (intArray), [charArray] "m" (charArray), [fpVarIn] "m" (fpVar) : "eax", "ebx", "ecx" // clobbers ); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started