Lab # 4

Description:

In this Lab we were required to write a code using C or C++ that had the same functionality as LAB#2; which was the multi function calculator and Decimal to Roman converter all in one. Afterwards we were supposed to compile the code into Assembly and compare the Assembly code with the HLA code we wrote in LAB#2.

HLA code vs. Assembly [C]:

One noticeable difference is the size of the code, the HLA version has approximately 50% less lines than the equivalent C – code compiled into Assembly. One similarity is that there is an abundance of the “MOV” command as it is vital for most operations. Something important to note is that while writing the HLA code we were not restricted to writing using only labels and jumps which is what seem to be prevalent within the assembly code. With thorough examination of both codes it is clear how HLA is a “mid-point” language between real assembly and popular languages such as C/C++.  Additionally one can see how it would be possible to write a code in HLA which would closely resemble real Assembly code by restricting oneself to writing using only jumps and labels.

Code:

#include
#include
#include

int main()
{

// Yevgeniy Babkin   --- CET3510
//
// this program provides the basic functions of a calculator 
// adding, subtracting, multiplying, dividing, & exponentiating

//static

int x;
        int y;
        int z;
        int s;
        int d;
        int r;
        char k;
        char n = 'y';
        char m = 'Y';

        printf("\nThis program will perform basic calculations of 2 numbers\n");
        printf("To exit program at anytime Press 'CTRL + C \n\n\n");
        printf("\nWould You Like To Continue??? [Y/N] \n");
        scanf("%c", &k);
        if((k==n)||(k==m))
            goto start;
        else
            goto end;
start:
 {
system("clear");
 }

        printf("\n\nPlease select operation to be performed on the 2 numbers\n");

         printf("\n   Please input 1 for addition");
         printf("\n   Please input 2 for subtraction");
         printf("\n   Please input 3 for multiplication");
         printf("\n   Please input 4 for division");
         printf("\n   Please input 5 for exponentiation");
         printf("\n   Please input 6 for Decimal to Roman Numeral Conversion\n\n");
        scanf("%d", &z);

         if((z==0) || (z>6))
         {
         printf("\ninvalid selection \n");
        printf("\nPlease select operation to be performed on the 2 numbers \n");

        printf("\nPlease input 1 for addition");
        printf("\nPlease input 2 for subtraction");
        printf("\nPlease input 3 for multiplication");
        printf("\nPlease input 4 for division");
        printf("\nPlease input 5 for exponentiation");
        printf("\nPlease input 6 for Decimal to Roman Numeral Conversion\n\n");
       scanf("%d", &z);
         }

        if(z==1)
          {
          {

 printf("\nPlease input 1st number [0-3999]: ");

  scanf("%d" , &x);
  }

         if(x>65536)
          {
             printf("\nnumber is too high!!!  TRY AGAIN!\n");
             scanf("%d" , &x);
          }

         printf("\nPlease input 2nd number [0-3999]: ");

          scanf("%d" , &y);

        if(y>65536)
          {
             printf("\nnumber is too high!!!  TRY AGAIN! \n");
            scanf("%d", &y);

           }

            x=x+y;

            printf("\n          The answer is:  %d \n", x);

          }
         if(z==2) 
           {
            {
            printf("\nPlease input 1st number [0-3999]: ");

  scanf("%d", &x);

            }
         if(x>65536)
            {
             printf("\nnumber is too high!!!  TRY AGAIN! \n");
             scanf("%d", &x);
        }

         printf("\nPlease input 2nd number [0-3999]: ");

          scanf("%d", &y); 

        if(y>65536) 
             {
             printf("\nnumber is too high!!!  TRY AGAIN! \n");
              scanf("%d", &y);

          }
              x=x-y;

            printf("\n          The answer is:  %d \n", x);

      }

         if(z==3)
         {
         {
            printf("\nPlease input 1st number [0-3999]: ");

  scanf("%d", &x);

            }
         if(x>65536)
            {
             printf("\nnumber is too high!!!  TRY AGAIN! \n");
             scanf("%d", &x);
        }

         printf("\nPlease input 2nd number [0-3999]: ");

          scanf("%d", &y);

        if(y>65536)
        {
             printf("\nnumber is too high!!!  TRY AGAIN! \n");
             scanf("%d", &y);

             }

             x=x*y;

            printf("\n          The answer is: %d \n", x);

         }

         if(z==4)
          {
          {
            printf("\nPlease input 1st number [0-3999]: ");

  scanf("%d", &x);

          }

         if(x>65536)
            {
             printf("\nnumber is too high!!!  TRY AGAIN! \n");
             scanf("%d", &x);
            }

         printf("\nPlease input 2nd number [0-3999]: ");

          scanf("%d", &y);

        if(y>65536)
           {
             printf("\nnumber is too high!!!  TRY AGAIN! \n");
             scanf("%d", &y);
           }

            x=x/y;
            printf("\n          The answer is: %d \n", x);
         }

         if(z==5) 
           {
             float a,b,c;

            {
            printf("\nPlease input 1st number [0-3999]: ");

  scanf("%f", &a);

            }
         if(x>65536)
            {
             printf("\nnumber is too high!!!  TRY AGAIN!\n" );
             scanf("%f", &a);
        }

         printf("\nPlease input 2nd number [0-3999]: ");

          scanf("%f", &b);

        if(y>65536)
         {
             printf("\nnumber is too high!!!  TRY AGAIN!\n");
             scanf("%f", &b);

         }     
             c=( pow(a,b) );

            printf("\n          The answer is: %.2f \n", c);

         }

          if(z==6)
          {
           {

 printf("\nPlease input a number [1-3999] : ");

  scanf("%d", &x);
           }
          if(x>3999)
         {
             printf("\nnumber is too high!!!  TRY AGAIN!\n");
             scanf("%d", &x);

         }     

  if(x0)

            {

printf("\n                       Your number: %d \n",x);
               printf("\n          Roman Numeral equivalent: ");

//the program is fairly basic as it subtracts known roman numeral values
//to figure out which and how many roman numearals to display.

if(x>=1000 && x<4000)                     {	                        while(x>=1000)
                       {

                               x=x-1000;
printf("M");
}
   }
if(x>=900)
               {

                        x=x-900;
printf("CM");
}
if(x>=500) 
               {

                     x=x-500;
printf("D");
}

if(x>=400)
               {

printf("CD");
}
if(x>=100 && x<400)                {                         while(x>=100)
                       {

                               x=x-100;
printf("C");
}
}
if(x>=90)
               {

                        x=x-90;
printf("XC");
}
if(x>=50)
{	
                        x=x-50;
printf("L");
}
if(x>=40)
               {

                        x=x-40;
printf("XL");
}
if(x>=10 && x<40)                {                         while(x>=10)
                       {

                               x=x-10;
printf("X");
}
}
if(x>=9)
               {

                         x=x-9;
printf("IX");
}
if(x>=5)
               {

                       x=x-5;
printf("V");
}
if(x>=4)
               {

                       x=x-4;
printf("IV");
}
if(x>=1 && x<4)                {	                        while(x>=1)
                       {

                               x=x-1;
printf("I");
}

                }

}
        printf("\n");  
      }   

printf("\n\nTo perform another operation input '1', to EXIT input any other # \n\n");
         scanf("%d", &r);
         if(r == 1)
         {
          goto start;
         }

      end:     printf("\n\n\n Thank You for using this program \n\n\n" );

return 0;
}

Screenshot:

lab 4 screenshot

Leave a Reply

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