Lab 2

Throughout this lab we were able to create a simple 32-bit calculator which will display a menu asking the user what operation to perform. The operations supported are convert to roman numerals like we did in the first lab and also addition, subtraction, multiplication, and exponentiation. A warning will be displayed if the number entered is greater than 65,536. After the program computes the equations it will return to the original menu where you can choose to quit.

program Calc1;
#include("stdlib.hhf")
static
x:int32;
y:int32;
r:int32;
i:int32;
j:int32;
n:int32;
operation:int32;

begin Calc1;

repeat

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

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

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

stdout.put("Enter 4 to do exponents: ",nl);

stdout.put("Enter 5 to convert to roman numbers: ",nl);

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

stdin.get(operation);

mov(operation,edx);

switch(edx)

case(1)
		stdout.put("enter number 1: ");
		stdin.get(x);
		if(x>65536) then
			stdout.put(nl,"number is larger than 65536!" , nl);

		endif;
		stdout.put(nl);
		stdout.put("enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"number is larger than 65536!" , nl);

		endif;
		stdout.put(nl);
		mov(x,eax);
		mov(y,ebx);
		add(ebx,eax);
		mov(eax,r);
		stdout.put("The answer is: ",r,nl);
		if(r>65536) then
			stdout.put(nl,"number is larger than 65536!" , nl);

		endif;
		stdout.put(nl);

case(2)
		stdout.put("enter number 1: ");
		stdin.get(x);
		if(x>65536) then
			stdout.put(nl,"number is larger than 65536!" , nl);

		endif;
		stdout.put(nl);
		stdout.put("enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"number is larger than 65536!" , nl);

		endif;
		stdout.put(nl);
		mov(x,eax);
		mov(y,ebx);
		sub(ebx,eax);
		mov(eax,r);
		stdout.put("The answer is: ",r,nl);
		if(r>65536) then
			stdout.put(nl,"number is larger than 65536!" , nl);

		endif;
		stdout.put(nl);

case(3)
		stdout.put("enter number 1: ");
		stdin.get(x);
		if(x>65536) then
			stdout.put(nl,"number is larger than 65536!" , nl);
		endif;

		stdout.put(nl);
		stdout.put("enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"number is larger than 65536!" , nl);

		endif;
		stdout.put(nl);
		mov(x,eax);
		mov(y,ebx);
		while(ebx > 0) do
			add(eax,r);
			sub(1,ebx);

		endwhile;
		stdout.put("the answer is: ",r,nl);
		if(r>65536) then
			stdout.put(nl,"number is larger than 65536!" , nl);
		endif;
		stdout.put(nl);

case(4)

		stdout.put("enter number 1: ");
		stdin.get(x);
		if(x>65536) then
			stdout.put(nl,"number is larger than 65536!" , nl);
		endif;

		stdout.put(nl);
		stdout.put("enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"number is larger than 65536!" , nl);

		endif;
		stdout.put(nl);
		mov(x,eax);
		mov(y,ebx);
		mov(1,r);
		for(mov(0,i); i<ebx; inc(i)) do
 			mov(0,ecx);
		for(mov(0,j); j65536) then
		stdout.put(nl,"number is larger than 65536!" , nl);
		endif;
		stdout.put(nl);

case(5)

stdout.put("Enter a decimal number, "
);
  stdin.get(n);
  mov(n,eax);
  while(eax!=0)do

	while(eax!=0)do


  if(eax >= 1000 ) then
      stdout.put("M");
      sub(1000,eax);

      elseif(eax >= 900 ) then
      stdout.put("CM");
      sub(900,eax);

      elseif(eax >= 500 ) then
      stdout.put("D");
      sub(500,eax);

      elseif(eax >= 400 ) then
      stdout.put("CD");
      sub(400,eax);

      elseif(eax >= 100 ) then
      stdout.put("C");
      sub(100,eax);

      elseif(eax >= 90 ) then
      stdout.put("XC");
      sub(90,eax);

      elseif(eax >= 50 ) then
      stdout.put("L");
      sub(50,eax);

      elseif(eax >= 40 ) then
      stdout.put("XL");
      sub(40,eax);

      elseif(eax >= 10 ) then
      stdout.put("X");
      sub(10,eax);

      elseif(eax >= 9 ) then
      stdout.put("IX");
      sub(9,eax);

      elseif(eax >= 5 ) then
      stdout.put("V");
      sub(5,eax);

      elseif(eax >= 4 ) then
      stdout.put("IV");
      sub(4,eax);

      elseif(eax >= 1 ) then
      stdout.put("I");
      sub(1,eax);

            endif;  
  endwhile;


stdout.put(nl "Enter a decimal number, ",nl
	     "To exit press 0: ")
mov(0,n);
	stdin.get(n);
	stdout.put(nl "You have entered ", n, nl);
	mov(n,eax);

endwhile;


endswitch;

until(edx = 0);

stdout.put("end of program");
stdin.get(x);

end Calc1;


snapshot1


snapshot2



 

Leave a Reply

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