Lab Description:
This lab consist of creating a simple 32-bit calculator that will work as fallowed. First a menu will display asking the user which operation they would like to perform ( 1 for addition, 2 for subtraction, 3 for multiplication, and 4 for exponentiation and 5 to convert a decimal to a Roman numeral). After choice selection, a menu display will ask the user to input the values and then perform the operation selected. Keep in mind that once the user enter a number greater than 65536, there will be a warning sign because that might cause overflow. The approach I used to create this program is a set of 32 bits variables and a bunch of if, while and if else statement. The program will continue to run until the user chooses to exit by pressing 0.
Alassane
Code:
program lab2calculator; #include( "stdlib.hhf"); static choice: int32; n:int32; n1:int32; n2:int32; x:int32; a:int32; b:int32; c:int32; begin lab2calculator; stdout.put(nl); stdout.put("This cool calculator will ask the user to choose the operation they would like",nl," to perform,once the user makes their choice, the program will request the input",nl," value and then perform the operation. A warning will be geven if any value is",nl" greater than 65536. The program will run until the user presses 0 to excit.",nl); repeat stdout.put(nl); stdout.put("What operation would you like to perform?",nl"Please press:",nl); stdout.put(nl); stdout.put("1 for Addition"); stdout.put(nl); stdout.put("2 for Subtraction"); stdout.put(nl); stdout.put("3 for Multiplication"); stdout.put(nl); stdout.put("4 for Exponentiation"); stdout.put(nl); stdout.put("5 To conver decimal to roman numeral") stdout.put(nl); stdout.put("0 to excit",nl); stdout.put(nl); stdout.put("Please enter you choice here:"); stdin.get(choice); mov(choice, ecx); if(choice=1) then stdout.put(nl); stdout.put("Your choice was to add numbers, how cool.",nl); stdout.put(nl); stdout.put("Please enter the first value: "); stdin.get(n1); if(n1>65536) then stdout.put("Oops!!! the number you entered is greater than 65536 "); stdout.put(" look out for overflow ",nl); endif; mov(n1, eax); stdout.put(nl); stdout.put("Please enter the second value: "); stdin.get(n2); if(n2>65536) then stdout.put("Oops!!! the number you entered is greater than 65536 "); stdout.put(" look out for overflow ",nl); endif; mov(n2, ebx); add(eax, ebx); mov(ebx, x); stdout.put(nl); stdout.put("The sum of ",n1," and ",n2," is: ", x, nl); elseif (choice=2) then stdout.put(nl); stdout.put("Your choice was to subtract numbers, how cool.",nl); stdout.put(nl); stdout.put("Please enter the first value: "); stdin.get(n1); if(n1>65536) then stdout.put("Oops!!! the number you entered is greater than 65536 "); stdout.put(" look out for overflow ",nl); endif; mov(n1, ebx); stdout.put("Please enter the second value: "); stdin.get(n2); if(n2>65536) then stdout.put("Oops!!! the number you entered is greater than 65536 "); stdout.put(" look out for overflow ",nl); endif; mov(n2,eax); sub(eax,ebx); mov(ebx,a); stdout.put(nl); stdout.put("The difference between ",n1," and ",n2," is: ",a, nl); elseif (choice=3) then stdout.put(nl); stdout.put("Your choice was to multiply numbers, how cool.",nl); stdout.put(nl); stdout.put("Please enter the first value: "); stdin.get(eax); if(eax>65536) then stdout.put("Oops!!! the number you entered is greater than 65536 "); stdout.put(" look out for overflow ",nl); endif; mov(eax, x); stdout.put("Please enter the second value: "); stdin.get(ebx); if(ebx>65536) then stdout.put("Oops!!! the number you entered is greater than 65536 "); stdout.put(" look out for overflow ",nl); endif; mov(ebx,a); mov(0,eax); for(mov(0,ecx); ecx<ebx; inc(ecx)) do add(x,eax); mov(eax,b); endfor; stdout.put(nl); stdout.put(" The Product is:",b,nl); elseif (choice=4) then stdout.put("Please enter the first value:"); stdin.get(n1); if(n>65536) then stdout.put("Oops!!! the number you entered is greater than 65536 "); stdout.put(" look out for overflow ",nl); endif; mov(n1,eax); stdout.put("Please enter the second: "); stdin.get(n2); if(n2>65536) then stdout.put("Oops!!! the number you entered is greater than 65536 "); stdout.put(" look out for overflow ",nl); endif; mov(n2,ecx); if(ecx=0)then mov(1,eax); else while(ecx!=1)do mov(0,ebx); while(eax!=0)do add(n1,ebx); dec(eax); endwhile; mov(ebx,eax); dec(ecx); endwhile; endif; mov(eax,a); stdout.put("The result of ",n1," raised to ",n2," is: ",a,nl); elseif (choice=5) then stdout.put(" ", nl); stdout.put("Please enter a decimal number:"); stdin.get(n); mov(n,eax); stdout.put("The roman numeral equivalent to ", n , " is: "); if(eax>=100) then while(eax>=100) do sub(100,eax); stdout.put("C"); endwhile; endif; if (eax>=90) then stdout.put("XC"); sub(90,eax); endif; if (eax>=50) then stdout.put("L"); sub(50,eax); endif; if (eax>=40) then stdout.put("XL"); sub(40,eax); endif; if(eax>=10) then while(eax>=10) do sub(10,eax); stdout.put("X"); endwhile; endif; if (eax>=9) then stdout.put("IX"); sub(9,eax); endif; if (eax>=5) then stdout.put("V"); sub(5,eax); endif; if (eax>=4) then stdout.put("IV"); sub(4,eax); endif; if(eax>=1) then while(eax>=1) do stdout.put("I"); sub(1,eax); endwhile; endif; stdout.put(nl); if (n=0) then stdout.put("Oops You entered zero, this is not valid good bye.", nl); endif; stdout.put(" ", nl); endif; until(ecx=0); end lab2calculator;
Screen-shots:
Addition
Subtraction
Multiplication
Exponentiation
Roman numeral
Overflow