Lab 4

Lab #4  Rewriting Lab #3 in c or c++

Description:

For laboratory #3 the task was to create an assembly code in a flavor of Linux. The laboratory required the student to write a code that displayed a menu that performed the operations bellow. The code must ensure that the program returns to the original menu until the user chooses to exit.  Lab #3 had the following restrictions; jmp statements and labels must be used, and imul, mul, div cannot be used.

(1) The sum of a Number   :     Factorial,   n!  ,   N= n+(n-1)+((n-1)-1)+……+1

(2) The product of a Number  :  Factorial product of a number,   n*!, N = n*(n-1)*((n-1)-1)*…*1

(3) The number 2 raised to a Number:  2^(n)

(4) Exit the program.

For Laboratory #4 there are no restrictions and C or C++ must be used to compile and run the code. the menu must include the bellow. Like always the code must display the

(1) The sum of a Number   :     Factorial,   n!  ,   N= n+(n-1)+((n-1)-1)+……+1

(2) The product of a Number  :  Factorial product of a number,   n*!, N = n*(n-1)*((n-1)-1)*…*1

(3) The number 2 raised to a Number:  2^(n)

(4) Exit the program.

Upon successful compilation and running of the code “gcc -S filename.c” must be used to convert the written code to assembly for side-by-side comparison between Lab #3, and Lab #4.

Comparison:

The C++ file when converted to assembly had way more moves calls pops and many labels within the main loop. The assembly used mainly mov,  imull, jmps with conditions, labels, call, movl, testl, pushes and pops.

However in the assembly code from lab #3 only jmp with conditions, and labels were used on the surface, but most certainly the computer does the same as the converted code in the background.

CODE:

#include	// includes input and output stream such as cin and cout
using namespace std;
int Loop();

int n, choice;
static int EndValue;

int main()// function protocol... like "begin program"
{
	    cout << "Hello. My name is Cynthia!. \n And my name is Rafael." << "\n" << "This is Laboratory 4 for the CET class 3510." << "\n";
// EndValue == 0; because of the static int.

	//while(choice!=4)
	//{
		Loop();
	//}

}

int Loop()
{

      	cout << "This is Rafael's and Cynthia's c++ version of the second Calculator"<<"\n" <<  "Please enter the value for the operation you would like to apply." << "\n" << "(1) factorial of n" << "\n" << "(2)Product Factorial" << "\n" << "(3) the exponential of 2^(n)" << "\n" << "(4) Exit the program."<< "\n"; 	
       cin >> choice; // gets choice

while(choice>4||choice<1) // checks if choice was not from list 
{
	     cout << "This is Rafael's and Cynthia's c++ version of the second Calculator"<<"\n" <<  "Please enter the value for the operation you would like to apply." << "\n" << "(1) factorial of n" << "\n" << "(2)Product Factorial" << "\n" << "(3) the exponential of 2^(n)" << "\n" << "(4) Exit the program." << "\n"; 	
      cin >> choice; // gets choice
}

   if(choice==1) // for choice one do the code bellow
{
	    cout << "Enter the value to use in this operation." << "\n Value" << endl; 	cin >> n; // gets the value to factorial sum

	while(n>0)//while n is greater than 0 do the bellow
	{
     		EndValue = EndValue+n; // adds n to zero then adds the reduced n to the old n
     		n= n-1;
		}
  	cout << "The Value is  " << EndValue << "\n";
     	EndValue=0;
     	n=0; // Resets the values for the loop so that the new values will not be changed
}

  if(choice==2)
{
	   cout << "Enter the value to use in this operation." << "\n Value" << endl; 	cin >> n; // gets the value to factorial sum
	       EndValue=1;
	 while(n>0)
	{
		      EndValue = EndValue*n;
		      n= n-1;
		}
	    cout << "The Value is  " << EndValue << "\n";
	      EndValue=0;
	      n=0; // Resets the values for the loop so that the new values will not be changed
}

  if(choice==3)
{

	    cout << "Enter the value to use in this operation." << "\n Value" << endl; 	cin >> n; // gets the value to factorial sum

      	EndValue = 1;
	 while(n>0)
	{
		      EndValue = EndValue*2;
		      n = n-1;
		}
	   cout << "The Value is  " << EndValue << "\n";
	   EndValue=0;
   	n=0; // Resets the values for the loop so that the new values will not be changed
}

  if(choice==4)
{
	     cout << "Thank-you for testing the Program :) ! " << "\n" << "Good-bye" << "\n";
}
  while(choice!=4)
{
    	Loop();
}

}

SnapShots:

Screenshot from 2014-04-09 13:36:30

 

Screenshot from 2014-04-09 13:36:30

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *