Lab # 3

Description:

This Lab is designed to practice the jump function embedded in HLA. Essentially the program is supposed to emulate a calculator and perform 3 basic functions: Addition, Subtraction, and Multiplication without relying upon loop statements and substituting them with jump statements and labels.

Code:

program calculator;
    #include("stdlib.hhf")

// Yevgeniy Babkin   --- CET3510
//
// this program provides the basic functions of a calculator 
// adding, subtracting, multiplying. Using only labels and jmps!

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);

labelbeginning:

         mov(0,eax);
         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",nl);

          stdin.get(z);

label1if:
         mov(0,ebx);
         mov(z,eax);
         mov(4,edx);
         cmp(eax,ebx);
         jle labelerror1;
         cmp(eax,edx);
         jge labelerror1;
         jmp label2;

labelerror1: 

           stdout.put(nl,"invalid selection", nl);

jmp labelbeginning;

label2:
          mov(1,edx);
          cmp(eax,edx);
          je labeladd;
          mov(2,edx);
          cmp(eax,edx);
          je labelsub;
          mov(3,edx);
          cmp(eax,edx);
          je labelmult;

label3:
          mov(1,edx);
          cmp(eax,edx);
          je labeladd2;
          mov(2,edx);
          cmp(eax,edx);
          je labelsub2;
          mov(3,edx);
          cmp(eax,edx);
          je labelmult2;

labeladd:     

      stdout.put(nl,"Please input 1st number [0-3999]: ");

       stdin.get(x);

       mov(x, eax);

          mov(65535,ecx);
          cmp(eax,ecx);

          JG labelerrortoolarge;

 labeladd2:             

          stdout.put(nl,"Please input 2nd number [0-3999]: ");

           stdin.get(y);
           mov(y,ebx);

         mov(65535,ecx);
          cmp(ebx,ecx);
          jg labelerrortoolarge2;

             ADD(EAX,EBX);
             MOV(EBX, x);
             stdout.put(nl,"          The answer is: ", x, nl);

          jmp labelrepeat;

labelsub:

           stdout.put(nl,"Please input 1st number [0-3999]: ");

       stdin.get(x);

       mov(x, eax);

          mov(65535,ecx);
          cmp(eax,ecx);
          jg labelerrortoolarge;

labelsub2:

          stdout.put(nl,"Please input 2nd number [0-3999]: ");

           stdin.get(y);
           mov(y,ebx);

         mov(65535,ecx);
          cmp(ebx,ecx);
          jg labelerrortoolarge2;

             SUB(EBX,EAX);
             MOV(EAX, x);
             stdout.put(nl,"          The answer is: ", x, nl);

     jmp labelrepeat;

labelmult:

         stdout.put(nl,"Please input 1st number [0-3999]: ");

       stdin.get(x);

       mov(x, eax);

          mov(65535,ecx);
          cmp(eax,ecx);
          jg labelerrortoolarge;

labelmult2:

              stdout.put(nl,"Please input 2nd number [0-3999]: ");

           stdin.get(y);
           mov(y,ebx);

         mov(65535,ecx);
          cmp(ebx,ecx);
          jg labelerrortoolarge2;

              mov(eax,x);
              mov(1,s);
              sub(s,ebx);
              mov(0,edx);

 whilelabel: 
              cmp(ebx,edx);

               je  whiledone;

                 add(x,eax);
                 sub(s,ebx);
                 jmp whilelabel;
 whiledone:              

               MOV(eax, x);

             stdout.put(nl,"          The answer is: ", x, nl);

labelrepeat:

stdout.put(nl,nl,"To perform another operation input '1'",nl,nl);
stdout.put(nl,"To EXIT input a # other than 1 ",nl,nl);
          stdin.get (r);

          mov(r,eax);
          mov(1,ebx);
          cmp(eax,ebx);
           je labelbeginning;

             stdout.put(nl, " Thank you For using this Program ", nl );
             jmp labelend;

labelerrortoolarge:
     stdout.put(nl,nl,"Number entered too large try again",nl,nl);
            jmp label2;

labelerrortoolarge2:
     stdout.put(nl,nl,"Number entered too large try again",nl,nl);
            jmp label3;

        labelend:
end calculator;

Screenshot:

lab3

Leave a Reply

Your email address will not be published. Required fields are marked *