Lab 3

In this lab I created a simple 32-bit calculator similar to the one in Lab #2. The calculator displays a menu asking the user what operation to perform. The operations supported are addition, subtraction, and multiplication. Once the user selects an operation the program will ask the user for the values and perform the operation. We could not use mul or imul we solely relied on labels and jumps. We could not use if, while, for, or related commands. Once the program computes the value and displays the result, it returns to the original menu until the user chooses to exit.

program calc2;
#include("stdlib.hhf")
static
r:int32;
i:int32;
c:int32;

operation:int32;

begin calc2;

menu:

stdout.put("Enter 1 to subtract: ",nl);

stdout.put("Enter 2 to add:",nl);

stdout.put("Enter 3 to multiply: ",nl);

stdout.put("Enter 0 to exit: ",nl);

stdin.get(operation);

mov(operation,edx);

		cmp(edx,1);
	jne s;
		mov(0,c); 
		stdout.put("enter first number: ");
		stdin.get(r);
		stdout.put("enter second number: ");
		stdin.get(i);
			mov(r,eax);
			mov(i,ebx);
			sub(ebx,eax);
			mov(eax,c);
		stdout.put("The answer is: ",c,nl);
	jmp menu;
	s:

		cmp(edx,2);
	jne a;
                mov(0,c);
		stdout.put("enter first number: ");
		stdin.get(r);
		stdout.put("enter second number: ");
		stdin.get(i);
			mov(r,eax);
			mov(i,ebx);
			add(ebx,eax);
			mov(eax,c);
		stdout.put("The answer is: ",c,nl);
	jmp menu;

	a:
		cmp(edx,3);
	jne m;

			mov(0,c);
		stdout.put("enter first number: ");
		stdin.get(r);
		stdout.put("enter second number: ");
		stdin.get(i);
			mov(r,eax);
			mov(i,ebx);
		mul_loop:	

		cmp(ebx,0);

		jle mul_end;
			add(eax,c);
			sub(1,ebx);
			jmp mul_loop;
		mul_end:	
		stdout.put("the answer is: ",c,nl);
		stdout.put(nl);
	jmp menu;

m:

cmp(edx,0);
	jne menu;
end calc2;

1

Leave a Reply

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