Lab 2

Lab Description
In this lab we made a basic calculator that the user can select from a menu to do the basic math operations.
The menu consist of adding two numbers, subtracting two numbers, multiplying two numbers, doing exponents, and
converting the input number into roman numerals.For the multiplication and exponents we had to use loops that did
repetitive addition instead of using the mul, imul, and such functions.

Code

program lab2;
#include("stdlib.hhf")
static
x:int32;
y:int32;
r:int32;
i:int32;
j:int32;
choice:int32;

begin lab2;

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 convert to roman numbers: ",nl);

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

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

stdin.get(choice);

mov(choice,edx);

switch(edx)


case(1)
		stdout.put("enter number 1: ");
		stdin.get(x);
		if(x>65536) then
			stdout.put(nl,"number is bigger then 65536!" , nl);
		endif;
		stdout.put(nl);
		stdout.put("enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"number is bigger then 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 bigger then 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 bigger then 65536!" , nl);
		endif;
		stdout.put(nl);
		stdout.put("enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"number is bigger then 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 bigger then 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 bigger then 65536!" , nl);
		endif;
		stdout.put(nl);
		stdout.put("enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"number is bigger then 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 bigger then 65536!" , nl);
		endif;
		stdout.put(nl);

case(4)

		stdout.put("enter a number: ");

  		stdin.get(x);

		if(x>65536) then
			stdout.put(nl,"number is bigger then 65536!" , nl);
		endif;
		stdout.put(nl);
  		mov(x,eax);

  		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);

case(5)
		stdout.put("enter number 1: ");
		stdin.get(x);
		if(x>65536) then
			stdout.put(nl,"number is bigger then 65536!" , nl);
		endif;
		stdout.put(nl);
		stdout.put("enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"number is bigger then 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); j<eax; inc(j)) do
  			add(r,ecx);
        
		endfor;
			mov(ecx,r);
		endfor;
		stdout.put("The answer is: ",r);
                if(r>65536) then
			stdout.put(nl,"number is bigger then 65536!" , nl);
		endif;
		stdout.put(nl);


endswitch;

until(edx = 0);

stdout.put("end of program");

end lab2;

Screenshots
sub

add

multi

roman

exponent

Leave a Reply

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