Lab Description:
This lab was about a 32 bit calculator. The way the program works is that the program ask the user if they would like math operation they would like to do. The mathematical operations start inside the repeat statement which contains if-else statements which contains the mathematical operations for addition, subtraction, multiplication, division, exponentiation. Multiplication and exponentiation functions operations are done through addition function done multiple times in for loops. After they are done with their current operation, the user will be asked again what operation they would like to do, if they want to exit, they could press “0” to exit and the repeat loop ends.
Code:
program calc32; #include( "stdlib.hhf"); static numone: int32; numtwo: int32; x: int32; y: int32; z: int32; w: int32; Decision: int32; begin calc32; repeat //User enters the decision stdout.put("Which math operation would you like to do? Enter 1 (for addition), 2 (for subtraction), 3 (for multiplication), 4 (division), 5 (exponentation), If you want to exit press '0' ", nl); stdin.get(Decision); mov(Decision, ecx); if(Decision = 1) then // User enters 1st number stdout.put("Enter 1st Number: "); stdin.get(numone); mov(numone, eax); //User enters 2nd number stdout.put("Enter 2nd Number: "); stdin.get(numtwo); mov(numtwo, ebx); //Program adds numbers together add(eax, ebx); mov(ebx, x); stdout.put("Your sum is:", x, nl); // Program starts subtraction. elseif (Decision = 2) then // User enters 1st number stdout.put("Enter 1st Number: "); stdin.get(numone); mov(numone, ebx); //User enters 2nd number stdout.put("Enter 2nd Number: "); stdin.get(numtwo); mov(numtwo, eax); //Program subtracts numbers together EBX - EAX sub(eax, ebx); mov(ebx, y); stdout.put("Your difference is:", y, nl); // Program starts multiplication. elseif (Decision = 3) then // User enters 1st number stdout.put("Enter 1st Number: "); stdin.get(eax); mov(eax, x); stdout.put("Enter 2nd Number: "); stdin.get(ebx); mov(ebx, y); // Clear EAX mov(0,eax); //For loop keeps looping which keeps adding x to EAX, until the counter ECX reaches the same value as EBX for(mov(0, ecx); ecx < ebx; inc(ecx)) do add(x, eax); mov(eax, z); endfor; stdout.put("Your product is: ", z, nl); // Program starts division. elseif (Decision = 4) then // User enters 1st number stdout.put("Enter 1st Number: "); stdin.get(numone); mov(numone, ebx); //User enters 2nd number stdout.put("Enter 2nd Number: "); stdin.get(numtwo); mov(numtwo, eax); mov( 0, eax); div(z, eax:ebx); stdout.put("Your Quotient is:", z, nl); //Program starts exponentiation elseif (Decision = 5) then // User enters 1st number stdout.put("Enter 1st Number: "); stdin.get(eax); mov(eax, x); // User enters 2nd number stdout.put("Enter 2nd Number: "); stdin.get(ebx); mov(ebx, y); // Clear EAX mov(0,eax); for(mov(0, ecx); ecx < ebx; inc(ecx)) do add(x, eax); mov(eax, z); mov(0, eax); for(mov(0, ecx); ecx < ebx; inc(ecx)) do add(z, eax); mov(eax, w); endfor; stdout.put("Your answer is: ", w, nl); endfor; endif; until( ecx = 0 ); end calc32;