Lab #4 Muti Function Calculator

Objective:
In this Lab we were required to write a code using C that had the same functionality as LAB#3; which was the multi function calculator that perform the following operations, the sum of a number, the product of a number, exponentional or in this case I used power, and as well as a exit function. Afterwards we were supposed to compile the code into Assembly and compare the assembly code with the HLA code we wrote in Lab 3

Observation/Similarity and differences between HLA program and C
My first observation is that the code generated is hard to read and understand. Look like the text in the code is missing certain part and in some occasion not in order. The program in HLA has a very limited amount of instruction while the new code is lengthy and almost double in size. Certain instruction like move( , ); are the same as in in both code while decision structure in lab 3 are replaced by jumps and labels. the execution time is also slightly different. We should note that High Level Language require a Compiler to convert into assembly the machine level code.(Now-a-days compilers are smart enough to generate the machine code directly). To write assembly code it is necessary to know the architecture of the processor or controller. To write an High Level Program it is not necessary to know the architecture completely. In the new generated code certain parts seem to be not necessary for the program to run while in the HLA in lab 3 every instruction or line of code is part of the program and if missing the program will not run. In conclusion an assembly language is where each statement corresponds to one machine instruction while high level language is where each statement corresponds to more than one, sometimes many, machine instructions.

#include
main()
{
int f;
int n;
int x;
int y;
int z;


do {
printf("\n");
printf("Choose Operation:\n");
printf("  1 = Addition\n");
printf("  2 = Subtraction\n");
printf("  3 = Multiplication\n");
printf("  4 = Power Function\n");
printf("  5 = Convert Numbers to Roman Numerals\n");
printf("  6 = EXIT\n");
printf("\n");
scanf("%d",&x);
 f = x;

    if (x==1){
        printf("    ADDITION\n");
        do {
        printf("Enter first value:    ");
        scanf("%d", &y);
        printf("Enter second value:   ");
        scanf("%d", &z);
        x = y +z;

        if (x>=65536) {
                printf("     **Values entered will cause overflow...**\n");
            }

            else {
                printf("Answer: %d\n", x);
            }
        }
        while(x>=65536);
        } // ends else if x = 1 statement
        

    else if (x==2){
        printf("    SUBTRACTION\n");
        do {
        printf("Enter first value:   ");
        scanf("%d", &y);
        printf("Enter second value:   ");
        scanf("%d", &z);
        x = y -z;

        if (x>=65536) {
                printf("     **Values entered will cause overflow...**\n");
            }

            else {
                printf("Answer: %d\n", x);
            }
        }
        while(x>=65536);
        } // ends else if x = 2 statement


    else if (x==3){
        printf("    MULTIPLICATION\n");
        do {
        printf("Enter first value:   ");
        scanf("%d", &y);
        printf("Enter second value:   ");
        scanf("%d", &z);

        n = y;
            while (z>1){
                
                y = y+n;
                z--;
                }


            if (y>=65536) {
                printf("     **Values entered will cause overflow...**\n");
            }

            else {
                printf("Answer: %d\n", y);
            }
        }
        while(y>=65536);
        } // ends else if x = 3 statement


    else if (x==4){
                
        printf("    POWER\n");
        do {
        printf("Enter first value:   ");
        scanf("%d", &y);
        printf("Enter power value:   ");
        scanf("%d", &z);
            
        n = y;
            while (z>1){
                
                y = y*n;
                z--;
                }
            
            if (y>=65536) {
                printf("     **Values entered will cause overflow...**\n");
            }

            else {
                printf("Answer: %d\n", y);
            }
        }
        while(y>=65536);
        } // ends else if x = 4 statement

    else if (x==5){
        printf("    CONVERSION\n");
        do {
        printf("Enter Number You Wish To Convert:   ");
        scanf("%d", &y);
        
        if (y>=65536) {
                printf("     **Values entered will cause overflow...**\n");
            }
            
        else {
            
            while(y > 0){ 
                n=y;
                if(y >= 100){
                    y = n -100; // sub(100, eax);
                    
                    printf("C");
                    
                    } 
                else if(y >= 99){
                    y = n-99; //sub(99, eax);
                    printf("IC");
                    
                    }
                else if(y >= 90){
                    y = n-90; //sub(90, eax);
                    printf("XC");
                    
                    }
                else if(y >=50){
                    y = n-50; //sub(50, eax);
                    printf("L");
                    
                    }
                else if(y >= 49){
                    y = n-49; //sub(49, eax);
                    printf("IL");
                    
                    }
                else if(y >= 40){
                    y = n-40; //sub(40, eax);
                    printf("XC");
                    
                    }
                else if(y >= 10){
                    y = n - 10; //sub(10, eax);
                    printf("X");
                    
                    }
                else if(y >= 9){
                    y = n-9; //sub(9, eax);
                    printf("IX");
                    
                    }
                else if(y >=5){
                    y = n-5; //sub(5, eax);
                    printf("V");
                    
                    }
                else if(y>=4){
                    y = n-4; //sub(4, eax);
                    printf("IV");
                    
                    }
                else if(y >= 1){
                    y = n-1; //sub(1, eax);
                    printf("I");
                    
                    }
                
                } //ends while
        } // ends else statement
            
        }
        while(y>=65536);
        
        } // ends else if x = 5 statement
            
        
}
while (f < 6);
 
} // closes program

lab 4 1

lab 4 2

lab 4 3

lab 4 4

Leave a Reply

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