Lab Description:
This lab consist of building a 32 bits calculator that can perform, addition, subtraction, multiplication, exponentiation and converting decimal to roman numeral. I used if statement and loop to write the program
Code:
program lab2; #include( "stdlib.hhf"); static n:int32; n1:int32; n2:int32; begin lab2; stdout.put("Press [1]:Addition [2]:Subtraction [3]:Multiplication [4]:Exponentiation [5]:Roman numeral [0]:exit",nl); repeat stdout.put(nl); stdout.put("Do operation number: or press 0 to exit:"); stdin.get(n); mov(n, ecx); if(n=1) then stdout.put("enter n1: "); stdin.get(n1); if(n1>65536) then stdout.put(" overflow",nl); endif; mov(n1, eax); stdout.put("enter n2: "); stdin.get(n2); if(n2>65536) then stdout.put(" overflow ",nl); endif; mov(n2, ebx); add(eax, ebx); mov(ebx,n); stdout.put("The sum is:" ,n, nl); elseif (n=2) then stdout.put("enter n1: "); stdin.get(n1); if(n1>65536) then stdout.put("overflow ",nl); endif; mov(n1, ebx); stdout.put("enter n2: "); stdin.get(n2); if(n2>65536) then stdout.put("overflow ",nl); endif; mov(n2,eax); sub(eax,ebx); mov(ebx,n); stdout.put(nl); stdout.put("The difference is:",n, nl); elseif (n=3) then stdout.put("enter n1: "); stdin.get(n1); if(n1>65536) then stdout.put("overflow ",nl); endif; mov(n1,eax); stdout.put("enter n2: "); stdin.get(n2); if(n2>65536) then stdout.put("overflow ",nl); endif; mov(n2,ebx); mov(0,eax); for(mov(0,ecx); ecx<ebx; inc(ecx)) do add(n1,eax); mov(eax,n); endfor; stdout.put(nl); stdout.put("Product is:",n,nl); elseif (n=4) then stdout.put("enter n1:"); stdin.get(n1); if(n1>65536) then stdout.put("overflow ",nl); endif; mov(n1,eax); stdout.put("enter n2: "); stdin.get(n2); if(n2>65536) then stdout.put("overflow ",nl); endif; mov(n2,ecx); if(ecx=0)then mov(1,eax); else while(ecx!=1)do mov(0,ebx); while(eax!=0)do add(n1,ebx); dec(eax); endwhile; mov(ebx,eax); dec(ecx); endwhile; endif; mov(eax,n); stdout.put(" Answer is:",n, nl); elseif (n=5) then stdout.put("enter n:"); stdin.get(n); mov(n,eax); stdout.put("roman is: "); if(eax>=100) then while(eax>=100) do sub(100,eax); stdout.put("C"); endwhile; endif; if (eax>=90) then stdout.put("XC"); sub(90,eax); endif; if (eax>=50) then stdout.put("L"); sub(50,eax); endif; if (eax>=40) then stdout.put("XL"); sub(40,eax); endif; if(eax>=10) then while(eax>=10) do sub(10,eax); stdout.put("X"); endwhile; endif; if (eax>=9) then stdout.put("IX"); sub(9,eax); endif; if (eax>=5) then stdout.put("V"); sub(5,eax); endif; if (eax>=4) then stdout.put("IV"); sub(4,eax); endif; if(eax>=1) then while(eax>=1) do stdout.put("I"); sub(1,eax); endwhile; endif; endif; until(n=0); end lab2;
Screen-Shots: