program ConCalculator;

#include("stdlib.hhf");

static

LabelPointer:dword := &Start; // Pointer to the 'Start' label.

begin ConCalculator;

Start: // Label for the start of the program.

stdout.put("Console Calculator", nl, nl); // Print out text.

/////////////////////////////////////////////////////////////////////////////////////////////////

GetValOne: // Label for next function that gets first value from user input.

stdin.readLn(); // Let the user give their input.

stdin.geti32(); // Store a 32bit integer in EAX.

push(eax); // Store the value of EAX on the stack.

xor(eax,eax); // Clear EAX of any data.

/////////////////////////////////////////////////////////////////////////////////////////////////

GetOperation: // Label for next function that gets a mathematical operation from user input.

stdin.readLn(); // Let the user give their input.

stdin.getc(); // Store a character in AX.

mov(ax,bx); // Move the operation data from AX into BX.

xor(ax,ax); // Clear EAX of any data.

/////////////////////////////////////////////////////////////////////////////////////////////////

GetValTwo: // Label for next function that gets second value from user input.

stdin.readLn(); // Let the user give their input.

stdin.geti32(); // Store a 32bit integer in EAX.

mov(eax,ecx); // Move the second value into ECX.

xor(eax,eax); // Clear EAX of any data.

pop(eax); // Get first value off stack and back into EAX.

/////////////////////////////////////////////////////////////////////////////////////////////////

if(bx = '*') then // Check if the operator was for multiplication.

stdout.puti32(eax); // Print out the first value.
stdout.put("*"); // Print out symbol for multiplication.
stdout.puti32(ecx); // Print out the second value.
stdout.put("="); // Print out the equal sign.

mul(ecx); // Do the multiplication. (EAX * ECX)

stdout.puti32(eax); // Print out the product.

endif;

/////////////////////////////////////////////////////////////////////////////////////////////////

if(bx = '/') then // Check if the operator was for division. To Do: Fix integer overflow error.

div(cl,ax); // Do the division. (AX / CL)

stdout.puti8(al); // Print out the quotient.

if(ah != 0) then // Check if AH contains anything.

stdout.put('.'); // Print out the decimal.

stdout.puti8(ah);  // Print out the remainder.

endif;

endif;

/////////////////////////////////////////////////////////////////////////////////////////////////

if(bx = '+') then // Check if the operator was for addition.

add(ecx, eax); // Do the addition. (ECX + EAX)

stdout.puti32(eax); // Print out the sum.

endif;

/////////////////////////////////////////////////////////////////////////////////////////////////

if(bx = '-') then // Check if the operator was for subtraction.

sub(ecx,eax); // Do the subtraction. (ECX - EAX)

stdout.puti32(eax); // Print out the difference.

endif;

/////////////////////////////////////////////////////////////////////////////////////////////////

if(bx = '^') then // To Do

// To Do

endif;

/////////////////////////////////////////////////////////////////////////////////////////////////

LoopCheck:

stdin.readLn(); // Let the user give their input.

stdin.getc(); // Store a character in AX.

if(ax = 'c') then // Check if the character was 'c'.

jmp(LabelPointer); // Jump back to the beginning and run the program again.

endif;


end ConCalculator;


Here's a small console calculator I've been working on. Nothing big, just though I'd share it. Still have to fix a lot of things and add support for multiple operations. (Also, yes, I comment everything. No matter how trivial it may be. ;))

- Steve
Posted on 2006-04-08 00:19:43 by ~Steve~