LAB 3

lab description:
In this lab, we are going to write a 32 bits calculator with only three functions:which are addition,subtraction and multiplication using label and jump. And we are not allowed to use any “sugar” operations such as for loop and while loop, etc. However, I find it difficult to write multiplication without using the while loop.

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

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

begin Calclabel;

Menulabel:
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("Enter any other number to exit the program.",nl);
stdin.get(Choice);
mov(Choice,EDX);
cmp(EDX,1);
je Addlabel;
cmp(EDX,2);
je Sublabel;
cmp(EDX,3);
je Mullabel;
jmp Elabel;


   
 Addlabel:
      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);
      jmp Menulabel;


 Sublabel:
      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);
      jmp Menulabel;




Mullabel:
      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);
      
stdout.put("nl");

   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("Enter any other number to exit the program.",nl);
   mov(0, EDX);
   stdin.get(Choice);
   mov(Choice,EDX);
   

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

end Calclabel;

lab3

Leave a Reply

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