Lab 2

Lab Description:

In this lab we ere required to create a 32bit calculator which let’s the user choose a function: add, subtract, multiply, exponentiate, or convert to roman numerals,  without using the HLA commands mul, imul or pow.

Code:

program Calculator;
#include("stdlib.hhf")
static
x:int32;
y:int32;
z:int32;
i:int32;
j:int32;
n:int32;
choice:int32;

begin Calculator;

repeat

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

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

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

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

stdout.put("Enter 5 to convert to roman numerals: ",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,"this number is too large." , nl);

		endif;
		stdout.put(nl);
		stdout.put("Enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"this number is too large." , nl);

		endif;
		stdout.put(nl);
		mov(x,eax);
		mov(y,ebx);
		add(ebx,eax);
		mov(eax,z);
		stdout.put("The answer is: ",z,nl);
		if(z>65536) then
			stdout.put(nl,"this number is too large." , nl);

		endif;
		stdout.put(nl);

case(2)
		stdout.put("Enter number 1: ");
		stdin.get(x);
		if(x>65536) then
			stdout.put(nl,"this number is too large." , nl);

		endif;
		stdout.put(nl);
		stdout.put("Enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"this number is too large." , nl);

		endif;
		stdout.put(nl);
		mov(x,eax);
		mov(y,ebx);
		sub(ebx,eax);
		mov(eax,z);
		stdout.put("The answer is: ",z,nl);
		if(z>65536) then
			stdout.put(nl,"this number is too large." , nl);

		endif;
		stdout.put(nl);

case(3)
		stdout.put("Enter number 1: ");
		stdin.get(x);
		if(x>65536) then
			stdout.put(nl,"this number is too large." , nl);
		endif;

		stdout.put(nl);
		stdout.put("Enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"this number is too large." , nl);

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

		endwhile;
		stdout.put("the answer is: ",z,nl);
		if(z>65536) then
			stdout.put(nl,"this number is too large." , nl);
		endif;
		stdout.put(nl);

case(4)

		stdout.put("Enter number 1: ");
		stdin.get(x);
		if(x>65536) then
			stdout.put(nl,"this number is too large." , nl);
		endif;

		stdout.put(nl);
		stdout.put("Enter number 2: ");
		stdin.get(y);
		if(y>65536) then
			stdout.put(nl,"this number is too large." , nl);

		endif;
		stdout.put(nl);
		mov(x,eax);
		mov(y,ebx);
		mov(1,z);
			for(mov(0,i); i<ebx; inc(i)) do
 				mov(0,ecx);
			for(mov(0,j); j<eax; inc(j)) do   			 				add(z,ecx);             		 		endfor; 		    			mov(ecx,z); 			 		endfor; 		                 stdout.put("the answer is: ",z,nl);  		if(z>65536) then
			stdout.put(nl,"this number is too large." , nl);
		endif;
		stdout.put(nl);

case(5)

stdout.put("Enter a decimal number or Enter 0 to exit." nl);
  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: ");
mov(0,n);
	stdin.get(n);
	mov(n,eax);

endwhile;

endswitch;

until(edx = 0);

stdout.put("Program End", nl);

end Calculator;

Screenshots:

case123 case45

Leave a Reply

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