Lab Description:
This lab required us to construct an algorithm for a basic calculator using HLA, but using only addition and subtraction functions. So the following program does exactly that and includes division. The menu for the program is slightly backwards in regards to the assigned order for the program but it works well.
Code:
program calculator; #include("stdlib.hhf") // Yevgeniy Babkin --- CET3510 // // this program provides the basic functions of a calculator // adding, subtracting, multiplying, dividing, & exponentiating static // declare a 16-bit variable thats greater than 0. x: int32; y: int32; z: int32; s: int32; d: int32; r: int32; begin calculator; // console.cls(); clears the original cmd window for a cleaner look. label1: console.cls(); stdout.put(nl,nl"This program will perform basic calculations of 2 numbers"); stdout.put(nl,nl"To exit program at anytime Press 'CTRL + C'",nl,nl); //forever // stdout.put(nl,"Please input 1st number [0-3999]: "); // stdin.get(x); // mov(x, EAX); // if(EAX>65536) then // stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); // stdin.get(x); // endif; // stdout.put(nl,"Please input 2nd number [0-3999]: "); // stdin.get(y); // mov(y, EBX); //if(EBX>65536) then // stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); // stdin.get(y); // endif; stdout.put(nl,"Please select operation to be performed on the 2 numbers", nl); stdout.put(nl," Please input 1 for addition"); stdout.put(nl," Please input 2 for subtraction"); stdout.put(nl," Please input 3 for multiplication"); stdout.put(nl," Please input 4 for division"); stdout.put(nl," Please input 5 for exponentiation"); stdout.put(nl," Please input 6 for Decimal to Roman Numeral Conversion"nl,nl); stdin.get(z); if((z=0) || (z>6))then stdout.put(nl,"invalid selection", nl); stdout.put(nl,"Please select operation to be performed on the 2 numbers", nl); stdout.put(nl,"Please input 1 for addition"); stdout.put(nl,"Please input 2 for subtraction"); stdout.put(nl,"Please input 3 for multiplication"); stdout.put(nl,"Please input 4 for division"); stdout.put(nl,"Please input 5 for exponentiation"); stdout.put(nl,"Please input 6 for Decimal to Roman Numeral Conversion",nl,nl); stdin.get(z); endif; if(z==1) then stdout.put(nl,"Please input 1st number [0-3999]: "); stdin.get(x); mov(x, EAX); if(EAX>65536) then stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); stdin.get(x); endif; stdout.put(nl,"Please input 2nd number [0-3999]: "); stdin.get(y); mov(y, EBX); if(EBX>65536) then stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); stdin.get(y); endif; ADD(EAX,EBX); MOV(EBX, x); stdout.put(nl," The answer is: ", x, nl); endif; if(z==2) then stdout.put(nl,"Please input 1st number [0-3999]: "); stdin.get(x); mov(x, EAX); if(EAX>65536) then stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); stdin.get(x); endif; stdout.put(nl,"Please input 2nd number [0-3999]: "); stdin.get(y); mov(y, EBX); if(EBX>65536) then stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); stdin.get(y); endif; SUB(EBX,EAX); MOV(EAX, x); stdout.put(nl," The answer is: ", x, nl); endif; if(z==3) then stdout.put(nl,"Please input 1st number [0-3999]: "); stdin.get(x); mov(x, EAX); if(EAX>65536) then stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); stdin.get(x); endif; stdout.put(nl,"Please input 2nd number [0-3999]: "); stdin.get(y); mov(y, EBX); if(EBX>65536) then stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); stdin.get(y); endif; labelmult: mov(eax,x); mov(1,s); sub(s,ebx); while(ebx!=0)do add(x,eax); sub(s,ebx); endwhile; MOV(EAX, x); stdout.put(nl," The answer is: ", x, nl); endif; if(z==4) then stdout.put(nl,"Please input 1st number [0-3999]: "); stdin.get(x); mov(x, EAX); if(EAX>65536) then stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); stdin.get(x); endif; stdout.put(nl,"Please input 2nd number [0-3999]: "); stdin.get(y); mov(y, EBX); if(EBX>65536) then stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); stdin.get(y); endif; DIV(EBX); MOV(EAX, x); stdout.put(nl," The answer is: ", x, nl); endif; if(z==5) then stdout.put(nl,"Please input 1st number [0-3999]: "); stdin.get(x); mov(x, EAX); if(EAX>65536) then stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); stdin.get(x); endif; stdout.put(nl,"Please input 2nd number [0-3999]: "); stdin.get(y); mov(y, EBX); if(EBX>65536) then stdout.put(nl,"number is too high!!! TRY AGAIN!" , nl); stdin.get(y); endif; mov(1,d); for(mov(0,s); s<ebx; inc(s))do mov(0,ecx); for(mov(0,r); r<eax; inc(r))do add(d,ecx); endfor; mov(ecx,d); endfor; stdout.put(nl," The answer is: ", d, nl); endif; if(z==6)then stdout.put(nl,"Please input a number [1-3999] : "); //the following commands "cin" the user input number // and move it into the 32 bit register stdin.get(x); mov(x, EAX); //next line is the main if loop that see's if the number is within 1-3999 range //prgrm will not run right without it!!! if(EAX<4000 && EAX>0)then //spacing is added to the 1st stdout to center the wording for //easier readability stdout.put(nl " Your number: ",x, nl, " Roman Numeral equivalent: "); //the program is fairly basic as it subtracts known roman numeral values //to figure out which and how many roman numearals to display. if(EAX>=1000 && EAX<4000) then while(EAX>=1000) do sub(1000, EAX); stdout.put("M"); endwhile; endif; if(EAX>=900) then sub(900, EAX); stdout.put("CM"); endif; if(EAX>=500) then sub(500, EAX); stdout.put("D"); endif; if(EAX>=400) then sub(400,EAX); stdout.put("CD"); endif; if(EAX>=100 && EAX<400) then while(EAX>=100) do sub(100, EAX); stdout.put("C"); endwhile; endif; if(EAX>=90) then sub(90, EAX); stdout.put("XC"); endif; if(EAX>=50) then sub(50, EAX); stdout.put("L"); endif; if(EAX>=40) then sub(40,EAX); stdout.put("XL"); endif; if(EAX>=10 && EAX<40) then while(EAX>=10) do sub(10, EAX); stdout.put("X"); endwhile; endif; if(EAX>=9) then sub(9, EAX); stdout.put("IX"); endif; if(EAX>=5) then sub(5, EAX); stdout.put("V"); endif; if(EAX>=4) then sub(4,EAX); stdout.put("IV"); endif; if(EAX>=1 && EAX<4) then while(EAX>=1) do sub(1, EAX); stdout.put("I"); endwhile; endif; endif; endif; stdout.put(nl,nl,"To perform another operation input '1'",nl,nl); stdin.get (r); if(r == 1)then jmp label1; else jmp label2; endif; stdout.put(nl, " To exit press CTRL+C ", nl ); label2: end calculator;
One Response to Lab # 2