lab#3

lab reprt:
lab description:
This lab was based on the lab2 calculator, except for a fact that we had to use labels instead of if,while and for statements. i only gave users options to add, subtract, and multiply.
code:

program calc;
#include("stdlib.hhf")
static
aint:int32;
z:int32;
x:int32;
y:int32;
ans:int32;
begin calc;

options:
stdout.put(" program calculator",nl);
stdout.put("press 1 for addition",nl);
stdout.put("press 2 for subtraction",nl);
stdout.put("press 3 for multiplication",nl);
stdout.put("press 0 to exit",nl);

stdin.get(aint);
mov(aint,edx);
cmp(edx,1);
je Addlabel;
cmp(edx,2);
je Sublabel;
cmp(edx,3);
je Multlbl;
cmp(edx,0);
je exitlbl;

huge:
      stdout.put(nl,"this number is big." , nl);


Addlabel:
		
		stdout.put("enter #1: ",nl);
		mov(0,ans);
		stdin.get(x);
		stdout.put("enter #2: ",nl);
		stdin.get(y);
		mov(x,eax);
		add(y,eax);
		mov(eax,ans);
		stdout.put(ans,nl);
jmp options;

Sublabel:
		stdout.put("enter #1: ",nl);
		mov(0,ans);
		stdin.get(x);
		stdout.put("enter #2: ",nl);
		stdin.get(y);
		mov(x,eax);
		sub(y,eax);
		mov(eax,ans);
		stdout.put(ans,nl);
jmp options;

Multlbl:
		mov(0,ans);
		stdout.put("Enter #1: ");
		stdin.get(x);
		cmp(x,65536);
		jg huge;
		stdout.put(nl);
		stdout.put("Enter number 2: ");
		stdin.get(y);
		cmp(y,65536);
		jg huge;
		stdout.put(nl);
		mov(x,eax);
		mov(y,ebx);
		cmp(ebx, 0);
		je zero;
		cmp(eax, 0);
		je zero;
		jg muldie;	
mulend:		
		stdout.put("The answer is: ",ans,nl);
		cmp(ans,65536);
		jg huge;
		stdout.put(nl);
jmp options;
muldie:		
		cmp(ebx,0);
		jle mulend;
		add(eax,ans);
		sub(1,ebx);
		jmp muldie;
zero:
  		stdout.put("The answer is 0.", nl);
jmp exitlbl;
exitlbl:
stdout.put("this is the end!", nl);

end calc;

screenshot:

lab3

Leave a Reply

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