Lab Description:
In this lab we were required to create a simple 32bit calculator, much like the one made in lab 2, but using our knowledge of jumps and labels. To do this I replaced all the if, while and for loops I used previously and created corresponding labels.
Code:
program jmpCalc; #include("stdlib.hhf") static x:int32; y:int32; z:int32; choice:int32; begin jmpCalc; menu: stdout.put("Enter 1 to add: ",nl); stdout.put("Enter 2 to subtract: ",nl); stdout.put("Enter 3 to multiply: ",nl); stdout.put("Enter 0 to exit: ",nl); stdin.get(choice); mov(choice,edx); cmp(edx,1); je addlbl; cmp(edx,2); je sublbl; cmp(edx,3); je mulbl; cmp(edx,0); je endlbl; toolrg: stdout.put(nl,"this number is too large." , nl); addlbl: mov(0,z); stdout.put("Enter number 1: "); stdin.get(x); cmp(x,65536); jg toolrg; stdout.put(nl); stdout.put("Enter number 2: "); stdin.get(y); cmp(y,65536); jg toolrg; stdout.put(nl); mov(x,eax); mov(y,ebx); add(ebx,eax); mov(eax,z); cmp(z,65536); jg toolrg; stdout.put("The answer is: ",z,nl); stdout.put(nl); jmp menu; sublbl: mov(0,z); stdout.put("Enter number 1: "); stdin.get(x); cmp(x,65536); jg toolrg; stdout.put(nl); stdout.put("Enter number 2: "); stdin.get(y); cmp(y,65536); jg toolrg; stdout.put(nl); mov(x,eax); mov(y,ebx); sub(ebx,eax); mov(eax,z); cmp(z,65536); jg toolrg; stdout.put("The answer is: ",z,nl); stdout.put(nl); jmp menu; mulbl: mov(0,z); stdout.put("Enter number 1: "); stdin.get(x); cmp(x,65536); jg toolrg; stdout.put(nl); stdout.put("Enter number 2: "); stdin.get(y); cmp(y,65536); jg toolrg; stdout.put(nl); mov(x,eax); mov(y,ebx); cmp(ebx, 0); je zero; cmp(eax, 0); je zero; jg mulfor; mulend: stdout.put("The answer is: ",z,nl); cmp(z,65536); jg toolrg; stdout.put(nl); jmp menu; mulfor: cmp(ebx,0); jle mulend; add(eax,z); sub(1,ebx); jmp mulfor; zero: stdout.put("The answer is 0.", nl); jmp endlbl; endlbl: stdout.put("Program End", nl); end jmpCalc;