Question

In: Computer Science

Consider the following C code: long fact(long x) { if (x <= 1) { return 1;...

Consider the following C code:

long fact(long x) { if (x <= 1) {

return 1; }

  long px = x;
  long fx = fact(x - 1);
  return px * fx;

}

Copy the above C code into a C file. For each line of C code responsible for a push or pop operation (either directly or as a result of a call or return), add a comment describing the stack operation and its purpose.

Hint: try compiling the C code to x86-64 using Arch gcc with the -S, -Og flags.

Solutions

Expert Solution

long fact(long x) {        // BEFORE ENTERING fact: push the value of x onto the stack
                           // BEFORE ENTERING fact: push the return address of next statement (from wherever fact is called) on stack

   if (x <= 1) {
       return 1;            // WHILE RETURNING from fact: pop local variable px off the stack      
                           // WHILE RETURNING from fact: pop return address and save this so that jmp can be done to this address
                           // WHILE RETURNING FROM fact: pop parameter (value of x) from stack
   }

long px = x;               // push local variable px onto the stack
long fx = fact(x - 1);    // BEFORE ENTERING fact: push the value of (x - 1) onto the stack
                           // BEFORE ENTERING fact: push the return address of next statement (i.e. return px * fx) on stack
                           // WHILE RETURNING: pop local variable px off the stack
                           // WHILE RETURNING from fact: pop return address and save this so that jmp can be done to this address
                           // WHILE RETURNING FROM fact: pop parameter (value of x - 1) from stack
                          
return px * fx;           // WHILE RETURNING from fact: pop local variable px off the stack
                           // WHILE RETURNING from fact: pop return address and save this so that jmp can be done to this address
                           // WHILE RETURNING FROM fact: pop parameter (value of x) from stack

}


Related Solutions

C code required /* * isGreater - if x > y then return 1, else return...
C code required /* * isGreater - if x > y then return 1, else return 0 * Example: isGreater(4,5) = 0, isGreater(5,4) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 24 * Rating: 3 */
Given the following Java code: class C { public int foo(C p) { return 1; }...
Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...
Write the code to return the output in Rstudio. What is the code? Code: x <-...
Write the code to return the output in Rstudio. What is the code? Code: x <- c(28, 69, 5, 88, 19, 20) Output must be: [1] 4 2 1 6 5 3
Explain the multithreading issue in the following c++ code and fix it. (consider options such as...
Explain the multithreading issue in the following c++ code and fix it. (consider options such as mutex lock). #include <iostream> #include <thread> #include <string> #include <unistd.h> using namespace std; static int tickets = 32; static int sale = 0; class Seller { public: void Main() { for (;;) { if(tickets <= 0){ cout << "In Seller[" << ID <<"] sold out"<< endl; break; }else{ tickets--; sale++; usleep(1); cout << "In Seller[" << ID <<"]"<<sale<< endl; } } } int ID;...
1) a) Explain the meaning and signifance of the fact that the genetic code is degenerate?...
1) a) Explain the meaning and signifance of the fact that the genetic code is degenerate? b) Briefly describe how complimentary base pairing explains: - the structure of the DNA double helix - DNA replication - transcription - binding of a codon to an anticodon
Consider the following information on the expected return for companies X and Y. Economy Probability X...
Consider the following information on the expected return for companies X and Y. Economy Probability X Y Boom 0.24 33 % 18 % Neutral 0.59 11 % 22 % Poor 0.17 −35 % 4 %      a. Calculate the expected value and the standard deviation of returns for companies X and Y. (Round intermediate calculations to at least 4 decimal places. Round your final answers to 2 decimal places.) b. Calculate the correlation coefficient if the covariance between X and...
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2....
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2. The variable value1 has been initialized to 200000. Declare the variable longPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable longPtr. c) Display the value of the object pointed to by longPtr. d) Assign the value of the object pointed to by longPtr to variable value2. e) Display the value of value2....
Consider the following probability distribution for stocks C and D: State Probability Return on Stock C...
Consider the following probability distribution for stocks C and D: State Probability Return on Stock C Return on Stock D 1 0.30 7 % – 9 % 2 0.50 11 % 14 % 3 0.20 – 16 % 26 % If you invest 25% of your money in C and 75% in D, what would be your portfolio's expected rate of return and standard deviation? Select one: a. 9.891%; 8.70% b. 9.945%; 11.12% c. 8.225%; 8.70% d. 10.275%; 11.12%
Consider the following set of ordered pairs. Complete parts a through c. x 1 1 3...
Consider the following set of ordered pairs. Complete parts a through c. x 1 1 3 3 1    y −1 1 4 3 −1 a. Calculate the slope and​ y-intercept for these data. ^y=______ + (______) x ​(Round to four decimal places as​ needed.) b. Calculate the total sum of squares​ (SST). SST= ​(Round to one decimal place as​ needed.) c. Partition the sum of squares into the SSR and SSE. SSE= ​(Round to three decimal places as​ needed.)...
Write pseudo code for the following parts of class BinarySearchTree: a) find(x) b) contains(x) c) add(x)...
Write pseudo code for the following parts of class BinarySearchTree: a) find(x) b) contains(x) c) add(x) d) remove(x) e) splice(x) f) min() g) max() h) pred() i) succ() j) floor() k) ceil()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT