Lab Description:
This is another recreation of lab 2 in C code, and Assembly code. We are recreating lab 2 and in both formats to see the comparison from C code, HLA, and Assembly. There were some discrepancies between each code when compared to each other. In some ways, C and HLA were alike. Many of the loops in HLA can transition into C, but the arithmetic operations are done in different ways. In HLA, there are arithmetic commands that you use to add, subtract, multiply, divide, and exponentiation such as add(register_operand, dest_operand), sub(add(register_operand, dest_operand), and etc. In C, there is just straight arithmetic operation ( z = 3+3 ; ). When comparing HLA to assembly,they are very similar but in Assembly most of the commands have a ” L ” on the end of the command such as addl(); or mov(); and many more. Assembly does not use loops and tends to use labels and jumps (which are in HLA, and can be used if wanted if you don’t want to use loops). When assembly prints or reads it calls the function that allows that to happen. Real Assembly compared to C is vastly different from each other. C has the full ability of loops but Assembly doesn’t so it uses jumps and labels to shift control to different parts of the program. But one thing Assembly and C do have in common is that they use the same command to print data on the screen, and to read data from the user but written in different variations.
Some of the observations that I have notice were that in Assembly that to establish a source and destination, a % sign is put in front of your desired source and destination(for example:” movl 24(%esp), %eax “); in HLA the command is written as mov(source, dest). Many of the words that were being printed out in Assembly, usually had .string before it for every single output. Real Assembly and HLA resembled the most compared to each other as the most of the code in HLA is compatible in Real Assembly but the code just has to be tweaked a little bit. I noticed that all the codes had different headings before each code was started. In HLA, all the variables used had declared as a static variable as in Real Assembly, you don’t have to. In C you have the ability to declare a static variable but it is not necessary to do to have the variables to be in your code. Jump or jmp (and all the other conditional jumps) in HLA were still written the same way in Real Assembly. With HLA and Real Assembly, data is always being moved in and out of registers. In C, you do not deal with moving data but put more concentration on displaying output or creating a solution to a problem in a particular way.
Code:
#include main() { int Decision, decimal, x, y, i = 0, j = 0, z = 0, t; do { //User enters the decision printf("Which math operation would you like to do? Menu Values: Enter 1 (for addition), 2 (for subtraction), 3 (for multiplication), 4 (exponenation), 5 (roman numeral), If you want to exit press '0' "); scanf("%d", &Decision); if(Decision == 1) //Start addition { printf("Enter 2 integers that you would like to add \n"); scanf("%d", &x); scanf("%d", &y); x += y; printf("Your sum is: %d \n", x); } else if (Decision == 2) // Start subtraction. { printf("Enter 2 integers that you would like to subtraction \n"); scanf("%d", &x); scanf("%d", &y); x -= y; printf("Your difference is: %d \n", x); } // Program starts multiplication. else if (Decision == 3) // Start multiplication { // User enters 1st number printf("Enter 1st Number: \n"); scanf("%d", &x); // User enters 2nd number printf("Enter 2nd Number: \n"); scanf("%d", &y); z = 0; //For loop keeps looping until i(counter) equals to y; inside the loop the process of repetition of addition is done to replicate multiplication. for(i = 0; i < y; i++) { z += x; } printf("Your product is: %d \n", z); // Print the product } else if (Decision == 4) { // User enters 1st number printf("Enter 1st Number: "); scanf("%d", &x); // User enters 2nd number printf("Enter 2nd Number: "); scanf("%d", &y); t = 1; for( i = 0; i < y; i++) { z = 0; for( j = 0; j = 1000) { decimal -= 1000; printf("M"); } else if( decimal >= 900) { decimal -= 900; printf("CM"); } else if( decimal >= 500) { decimal -= 500; printf("D"); } else if( decimal >= 400) { decimal -= 400; printf("CD"); } else if( decimal >= 100) { decimal -= 100; printf("C"); } else if( decimal >= 90) { decimal -= 90; printf("XC"); } else if( decimal >= 50) { decimal -= 50; printf("L"); } else if( decimal >= 49) { decimal -= 49; printf("IL"); } else if( decimal >= 40) { decimal -= 40; printf("XL"); } else if( decimal >= 10) { decimal -= 10; printf("X"); } else if( decimal >= 5) { decimal -= 5; printf("V"); } else if( decimal >= 4) { decimal -= 4; printf("IV"); } else if( decimal >= 1) { decimal -= 1; printf("I"); } //end of the if-else loop(for calculating roman numerals) else { printf("You entered 0, exiting roman numeral program now"); } }while(decimal > 0); //end of do-while loop (for roman numeral menu) printf("\n"); // to print the question on a new line when the answer is given } else { printf("You entered 0, exiting now \n"); // Specify to user that they entered the wrong menu value } } while( Decision > 0 ); return 0; }
Screenshots:
Part 1:
Part 2: