Factorial Calculator with Exponentiation Lab# 3
LAB DESCRIPTION:
For laboratory three students were encouraged to use labels, compare, and jump statements [commands]. The coder–student–must create a hla enveloped program that displays a menu upon start-up with the following options (1) The sum of a number, (2) The product of a number, (3) The number 2 raised to a number of the users choice [“n”]. (1) Is defined as the factorial, for a positive number n the program will make n! = n+(n-1)+((n-1)-1)+…..+1, note that the factorial of 0 is equal to 1. Please go to this website for information on why 0!=1 http://mathworld.wolfram.com/Zero.html
In addition (2) is defined as n*! = n*(n-1)*((n-1)-1)*(n-3)*………*1.
CODE:
program FactLab3; #include ("stdlib.hhf"); static a : int32; b : int32; c : int32; Application : int32; begin FactLab3; Start: stdout.put("Hello This is Rafael AND Cynthia's second calculator. :)"); stdout.put("The following opperations are available.", nl, "(1)The sum of a number[factorial]",nl, "(2)The product of a number[factorial miltiplication ]",nl , "(3)The exponetial of 2^(number)",nl ,"(4)And final an exit to the program",nl ); stdout.put("Reminder: For Application (1),(2),(3) you may not use a negative number!", nl); stdout.put("Please select from the above list.",nl"Application",nl); stdin.get(Application); cmp(Application,4);//checks if out side available choices JG Start;//Sends back to Start Label at the begining cmp(Application,1);//checks if out side available choices JL Start;//Sends back to Start Label at the begining cmp(Application,1);//checks if FactorialStart Label was selected JE FactorialStart; cmp(Application,2);//checks if FactorialMul Label was selected JE FactorialMul; cmp(Application,3);//checks if n2Exp Label was selected JE n2Exp; cmp(Application,4);//checks if Last Label was selected, this ends the program JE Last; FactorialStart://jumps to this part when Applications is equal to 1. stdout.put("You may not enter a number above or equal to 65000 or 2 raised to the 16 exponential value!",nl, "Please enter the number to be used.",nl); stdin.get(a); cmp(a,0); //checks for special case 0! = 1 sends to output loop JE FactorialSpecial; cmp(a,0); //checks for negative numbers a=1,4>=1,3>=1,2>=1,1>=1... mov(edx,ecx); // 5+0,5+5,5+10,5+15....20+20+20....60+60...120+0... cmp(eax,1); */ add(edx,c);// was add(eax,c);....infinite loop when i tried to save back to eax after full loops sub(1,b); mov(c,ecx); mov(ecx,a); //stdout.put("The first loop value is ", "c ", c, " a ", a , nl, nl); //just outputted values to check the loop to make sure the answer was right at each loop cmp(b,1);// used for loops 4 loops then 3 then 2 then 1 JGE FactorialMulLoop;// sends back to first loop for repeated addition JNGE FactorialMulLoop2;//sends to second loop FactorialMulLoop2:// second loop decides the multiplication /* //n = n*(n-1)*n-2...*1 sub(1,b); cmp(b,1); */ sub(1,eax); mov(eax,b); mov(c,edx);//mov(c,eax)gave...infinite loop when attempted to save value. mov(0,c); //stdout.put("The second loop value is ", "c ", c, " a ", a , nl, nl); //used to check the loop values cmp(eax,1);// used to compare for loops, 4 loops then 3 then 2 then 1 JGE FactorialMulLoop;// sends back to the first loop with degraded value for its loop JNGE Output2;// sends to output once all the loops for the first and second are completed Output2: mov(ecx,c); stdout.put("The factorial product is ", c,nl,nl,nl); mov(0,eax); mov(0,ebx); //Resets registers for next operation mov(0,ecx); mov(0,edx); jmp Start; // sends to begging of program for an other selection n2Exp: stdout.put("Please enter the exponent value to raise the number two to the ",nl); stdin.get(a); cmp(a,0); // set a case for 2^0 sends to output imediately JE n2ExpSpecial; cmp(a,1); // set a case for 2^1 sends to output imediately JE n2ExpSpecial1; cmp(a,65000); // checks to see if number is greater than 65000 JGE FactorialREStart2; JNGE n2ExpStart; // 2 raised to the 16 is fine for entry here only because the entry is 16 and not 65536. FactorialREStart2: stdout.put("This number is not in the span available please choose an other value"); stdout.put("Please enter the number to be used.",nl ); stdin.get(a); cmp(a,65000); JGE FactorialREStart2; JNGE n2ExpStart; n2ExpSpecial: stdout.put("The exponential of 2^[0]= 1",nl,nl); jmp Start; n2ExpSpecial1: stdout.put("The exponential of 2^[1] = 2",nl,nl); n2ExpStart: mov(2,c); // sets 2 as the incrimenter mov(0,ebx);// moves 0 to register ebx for starting value jmp n2ExpLoop; // sends to main addition loop bellow n2ExpLoop: add(c,ebx);//2+0 for start point 2^2 mov(ebx,c); // saves the value for powers of 2 sub(1,a); // sub 1 from a until a is 1 cmp(a,1); //checks against 2^1 for value of the exponent JGE n2ExpLoop; JNGE n2ExpOutput; //sends to output once loop is completed n2ExpOutput: mov(ebx, b);// moves ebx to b for outputting stdout.put("The Exponential value of 2 =", b, nl,nl); mov(0,eax); mov(0,ebx); //Resets registers for next opperation mov(0,ecx); mov(0,edx); jmp Start; // sends to beggining of program for an other selection Last: end FactLab3;
Screen Shots:
I am not certain why but for the factorial once entered i received a value of 134512807 for any number at the first start of the program, but after attempting the run again I out put the correct answer while still running the same code that gave me 134512807.