Lab 2

Description:
In this lab,we are going to write a calculator program that has menu to let the user decide what operations to do. The operations include additon,subtraction,multiplication and exponentiation. The program should not use any mul funtion when doing multiplication which I found it very difficult to do.Anyway,I have tried to write up a program that does the operation.


program Calc;
#include("stdlib.hhf");

static
   Choice: int32;
   Number1: int32;
   Number2: int32;
   Output: int32;

begin Calc;

stdout.put("Please select your choice of operation.",nl);
stdout.put("1). Addition",nl);
stdout.put("2). Subtraction",nl);
stdout.put("3). Multiplication",nl);
stdout.put("3). Exponentiation",nl);
stdout.put("Enter any other number to exit the program.",nl);
stdin.get(Choice);
mov(Choice,EDX);
while(EDX>=1 && EDX<=5) do
   switch(EDX)

   
   case(1)
      stdout.put("Option1:Addition" nl);
      stdout.put("Please enter the first number:"nl);
      stdin.get(Number1);
      stdout.put("Please enter the second number:"nl);
      stdin.get(Number2);
      mov(Number1,EAX);
      add(Number2,EAX);
      mov(EAX, Number1);
      stdout.put("The answer is: ",Number1,nl);

   case(2)
      stdout.put("Option2:Subtraction" nl);
      stdout.put("Please enter the first number:"nl);
      stdin.get(Number1);
      stdout.put("Please enter the second number:"nl);
      stdin.get(Number2);
      mov(Number1,EAX);
      sub(Number2,EAX);
      mov(EAX, Number1);
      stdout.put("The answer is: ",Number1,nl);

   case(3)
      stdout.put("Option3:Multiplication" nl);
      stdout.put("Please enter the first number:"nl);
      stdin.get(Number1);
      stdout.put("Please enter the second number:"nl);
      stdin.get(Number2);
      mov(0,EAX);
      mov(Number1,EAX);
      mov(0,EBX);
      mov(Number2,EBX);
      while(EBX!=1) do
         add(Number1,EAX);
         dec(EBX);
      endwhile;
      mov(EAX,Output);
      stdout.put("The answer is: ",Output,nl);

   case(4)
      stdout.put("Option4:Exponentiation" nl);
      stdout.put("Please enter your first number:"nl);
      stdin.get(Number1);
      stdout.put("Please enter the value of the exponent:"nl);
      stdin.get(Number2);
      mov(Number1,EAX);
      mov(Number2, ECX);
      if(ECX=0)then
         mov(1,EAX);
      else
         while(ECX!=1)do
            mov(0,EBX);
               while(EAX!=0)do
                  add(Number1,EBX);
                  dec(EAX);
               endwhile;
               mov(EBX,EAX);
               dec(ECX);
         endwhile;
      endif;
      mov(EAX,Output);
      stdout.put("The answer is: ",Output,nl);
   endswitch;

   stdout.put(nl,"Please select your choice of operation.",nl);
   stdout.put("1). Addition",nl);
   stdout.put("2). Subtraction",nl);
   stdout.put("3). Multiplication",nl);
   stdout.put("4). Exponentiation",nl);
   stdout.put("Enter any other number to exit the program.",nl);
   mov(0, EDX);
   stdin.get(Choice);
   mov(Choice,EDX);
   endwhile;

   stdout.put(nl,"The Program will now Exit.");

   end Calc;

lab2

Leave a Reply

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