LAB 4

Lab Description:
In lab 4 I had the option to write a calculator that can convert its output to roman in C or C++. I choose to write in C because I never used that language and was excited to learn something new. I Learned that C code is not really different than C++ except for some part of it, like the input and output command. The differece between C and HLA is a lot because HLA is very closer to Assembly than C. In this lab the main point was not to write the code in C but to actually convert it to the assembly language. I converted it by using the command gcc -s program.c to analyze the asembly language and give a brief explanation. When i finished my C code, I tested it and everything worked good. I than typed in gcc -s cprog.c and that created a cprog.s file. The difference between the hla code from lab 2 and assembly code that was converted from C is that in lab 2 that was written in hla had the loops while in assembly there was no such thing as loops. there was only labels and jumps. the similarity was that they both were using registers to store data but there was still a lot of difference between the 2 language but those differences were trival because the main point of assembly was creating loops using labels and jumps which was introduced in HLA. I now understand why HLA is a good language to learn before you move into Assembly because it can be close to High Level languages like C but it can also go deep like Assembly. Overall, I enjoyed this lab and this has given me more option to do assembly without actuall doing it.

C CODE:

#include
#include
int main()
{
	int x, y, z=0, c;
	int i, m, n, output;// this is for the multiplication loop
	int a=1,b, e=0, j, d, f;
        int roman, integer;
	printf("\n Please Enter your first input:");
	scanf("%d",&x);
	printf("\n Enter your Second input:");
	scanf("%d",&y);
	printf("\n Enter 1 for add\nEnter 2 for subtract\nEnter 3 for multiplication \n Enter 4 for exponent");
	scanf("%d",&c);
	//printf("C=%d \n",c);

	/*ADD */
	if (c==1) {
		z=x+y;
		printf("Output1=%d\n",z);
                printf("Enter (1) to convert to roman?");
		scanf("%d",&roman);
		if (roman==1)  {
	        	if ((z>=4000) && (z<=0)) 			{ 				printf("Invalid integer\n"); 			} 			else 			{ 				if (z) 				{ 					printf("The output is equivalent to="); 					if(z>=1000 && z<4000)// start   "works" 					{ 						while (z>=1000)
						{
							z=z-1000;
							printf("M\n");
						}
				 	}
					if(z>=900)
					{
						z=z-900;
						printf("CM\n");

					}
					if(z>=500)
					{
						z=z-500;
						printf("D\n");
					}

					if(z>=400)
					{
						z=z-400;
						printf("CD\n");
					} //end
					if(z>=100 && z<400) //start "works" 					{ 						while(z>=100)
						{
							z=z-100;
							printf("C\n");
						}
					}
					if(z>=90)
					{
						z=z-90;
						printf("XC\n");
					}
					if(z>=50)
					{
						z=z-50;
						printf("L\n");
					}
					if(z>=40)
					{
						z=z-40;
						printf("XL\n");
					}     //end
					if(z>=10 && z<40) //start 					{ 						while(z>=10)
						{
							z=z-10;
							printf("X\n");

						}
					}
					if(z>=9)
					{
					z=z-9;
					printf("IX\n");

					}
					if(z>=5)
					{
						z=z-5;
						printf("V\n");

					}
					if(z>=4)
					{
						z=z-4;
						printf("IV\n");

					}
					if(z>=1 && z<4) 					{ 						while(z>=1)
						{
							z=z-1;
							printf("I\n");

						}
					}
				}// start
		}
	}
}
	/*SUBTRACT */
else
{
	if (c==2)
	{
		z=x-y;
		printf("Output2=%d\n",z);

       		printf("Enter (1) to convert to roman?");
		scanf("%d",&roman);
		if (roman==1) 
			{
        			if ((z>=4000) && (z<=0)) 				{ 					printf("Invalid integer\n"); 				} 				else 				{ 					if (z)	 					{ 						printf("The output is equivalent to="); 						if(z>=1000 && z<4000)// start   "works" 						{ 							while (z>=1000)
							{
								z=z-1000;
								printf("M\n");
							}
						}
						if(z>=900)
						{
							z=z-900;
							printf("CM\n");

						}
						if(z>=500)
						{
							z=z-500;
							printf("D\n");
						}

						if(z>=400)
						{
							z=z-400;
							printf("CD\n");
						} //end
						if(z>=100 && z<400) //start "works" 						{ 							while(z>=100)
							{
								z=z-100;
								printf("C\n");
							}
						}
						if(z>=90)
						{
							z=z-90;
							printf("XC\n");
						}
						if(z>=50)
						{
							z=z-50;
							printf("L\n");
						}
						if(z>=40)
						{
							z=z-40;
							printf("XL\n");
						}     //end
						if(z>=10 && z<40) //start 						{ 							while(z>=10)
							{
								z=z-10;
								printf("X\n");

							}
						}
						if(z>=9)
						{
							z=z-9;
							printf("IX\n");

						}
						if(z>=5)
						{
							z=z-5;
							printf("V\n");

						}
						if(z>=4)
						{
							z=z-4;
							printf("IV\n");

						}
						if(z>=1 && z<4) 						{ 							while(z>=1)
							{
								z=z-1;
								printf("I\n");

							}
						}
					}
				}	
			}
		}
	}
/*MULTIPLICATION*/
if (c==3)
{

	for(i=0;i<y;i++) 	{ 		z=x+z; 	} 	printf("Output3=%d\n",z);        	printf("Enter (1) to convert to roman?"); 	scanf("%d",&roman); 	if (roman==1)   	{ 	       	if ((z>=4000) && (z<=0)) 		{	 			printf("Invalid integer\n"); 		} 	else 	{ 		if (z) 		{ 			printf("The output is equivalent to="); 			if(z>=1000 && z<4000)// start   "works" 			{ 				while (z>=1000)
				{
					z=z-1000;
					printf("M\n");
				}
			}
		if(z>=900)
		{
			z=z-900;
			printf("CM\n");

		}
		if(z>=500)
		{
			z=z-500;
			printf("D\n");
		}

		if(z>=400)
		{
			z=z-400;
			printf("CD\n");
		} //end
		if(z>=100 && z<400) //start "works" 		{ 			while(z>=100)
			{
				z=z-100;
				printf("C\n");
			}
		}
		if(z>=90)
		{
			z=z-90;
			printf("XC\n");
		}
		if(z>=50)
		{
			z=z-50;
			printf("L\n");
		}
		if(z>=40)
		{
			z=z-40;
			printf("XL\n");
		}     //end
		if(z>=10 && z<40) //start 		{ 			while(z>=10)
			{
				z=z-10;
				printf("X\n");

			}
		}
		if(z>=9)
		{
			z=z-9;
			printf("IX\n");

		}
		if(z>=5)
		{
			z=z-5;
			printf("V\n");

		}
		if(z>=4)
		{
			z=z-4;
			printf("IV\n");

		}
		if(z>=1 && z<4) 		{ 			while(z>=1)
			{
				z=z-1;
				printf("I\n");

			}

		}
	}
}
}
}
else
/*Exponent*/
{
	if(c==4)
	{

		a=1;
		for(i=0; i<y; i++)
		{
			e=0;
			for(j=0;j<x; j++)  			{	 				e += a; 		 				a = e; 		 			} 		} printf("Outputa=%d\n",a);        printf("Enter (1) to convert to roman?"); 		scanf("%d",&roman); 		if (roman==1) 		{         		if ((a>=4000) && (a<=0)) 			{ 				printf("Invalid integer\n"); 			} 		else 		{ 			if (a)	 			{ 				printf("The output is equivalent to="); 				if(a>=1000 && a<4000)// start   "works" 				{ 					while (a>=1000)
					{
						a=a-1000;
						printf("M\n");
					}
				}
			if(a>=900)
			{
				a=a-900;
				printf("CM\n");

			}
			if(a>=500)
			{
				a=a-500;
				printf("D\n");
			}

			if(a>=400)
			{
				a=a-400;
				printf("CD\n");
			} //end
			if(a>=100 && a<400) //start "works" 			{ 				while(a>=100)
				{
					a=a-100;
					printf("C\n");
				}
			}
			if(a>=90)
			{
				a=a-90;
				printf("XC\n");
			}
			if(a>=50)
			{
				a=a-50;
				printf("L\n");
			}
			if(a>=40)
			{
				a=a-40;
				printf("XL\n");
			}     //end
			if(a>=10 && a<40) //start 			{ 				while(a>=10)
				{
					a=a-10;
					printf("X\n");

				}
			}
			if(a>=9)
			{
				a=a-9;
				printf("IX\n");

			}
			if(a>=5)
			{
				a=a-5;
				printf("V\n");

			}
			if(a>=4)
			{
				a=a-4;
				printf("IV\n");

			}
			if(a>=1 && a<4) 			{ 				while(a>=1)
				{
					a=a-1;
					printf("I\n");

				}

			}
		}

	}	

}

}
return start;

}
}

SCREENSHOT:

ADD

ADD

SUBTRACT

Screenshot from 2013-11-23 23:55:08

MULTIPLY:

Screenshot from 2013-11-23 23:57:59

EXPONENTIAL:

Screenshot from 2013-11-23 23:59:44

Leave a Reply