How do I print minimum and maximum user inputs in Marie's Assembly language?

95 Views Asked by At

So in the following code I wrote. I was able to get the maximum value output after the user input 8 random integers, with a cap of 32000 on positive side and -32000 on negative side, but I can't seem to get the mimnimum value. For the minimum value it keep printing 0

Code:-

ORG 100

Main,     LOAD Count    
          STORE Count

Loop,     Input            
          STORE Value   
          LOAD Count
          ADD One       
          STORE Count 

CheckMin, LOAD Value   
          SUBT Smallest  
          SKIPCOND 800  
          JUMP Continue 
          LOAD Value  
          STORE Smallest

CheckMax, LOAD Value    
          SUBT Largest  
          SKIPCOND 400  
          JUMP Continue 

          LOAD Value    
          STORE Largest

Continue, LOAD Count  
          SUBT Seven
          SKIPCOND 800 
          JUMP Loop

Print,    LOAD Smallest 
          Output
          LOAD Largest 
          Output

          HALT
Value,    Dec 0
Count,    Dec 0
Smallest, Dec 0   
Largest,  Dec 0   
One,      Dec 1
Seven,    Dec 7

END

I tried to change the values of the "Decimals"(Dec) but it didn't work. Kept them at 0 except for the "Seven, Dec 7" which is the loop counter set to 8 for 8 user inputs. This is homework so any help is appreciated!

1

There are 1 best solutions below

0
On

You have a few logic errors, and I'd like to deal with them one at a time.


You have an assumption that the initial smallest value is 0, therefore, the code will only capture values smaller than 0 as the smallest — if you input all positive numbers, then the smallest value will be the initial value given that variable, which is 0.

Further, if you input only negative numbers, the largest will also come out as 0, which is your initial assumption of the largest.

There are three possible fixes:

  1. Regardless of smallest/largest in comparison, capture the first input as the initially known smallest value (and largest) so as to ignore that zero the initialization assumptions.  We would do that outside of the loop, so in C/pseudo code, something like this.
        int smallest = 0;    // this 0 will never be used
        int largest = 0;     //   ditto
        int count = 8;

        int x = input ();
        smallest = x;        // this will capture the first input as smallest & largest
        largest = x;         //   which then ignores the zero initializations

        for ( int i = 1; i < count; i++ ) {   // start at 1 instead of 0
           int x = input ();
           if ( x < smallest ) 
               smallest = x;
           if ( y > largest )
               largest = x;
        }

    This approach will work for any number of inputs where the count of inputs is >= 1 but if you wanted to handle a variable inputs count that supported a count of value 0 (a list of zero elements, for example, is a reasonable mathematic construct) it won't work since it demands at least one input.  A simple check on the count to skip the whole thing including first input would mitigate that.

  1. Use a boolean to tell whether a value has been captured or not.
        bool smallestCaptured = false;
        int smallest = 0;
        ...
            if ( !smallestCaptured || x < smallest ) {
                smallestCaptured = true;
                smallest = x;
            }
        ...

    This is precise, but requires a bit more logic within the loop, and, that logic involves the short-circuit disjunction operator, so a bit more complex.  Alternately it can be done with an if-then-else-if.

  1. Use a different value for the initial assumption of smallest (and largest)
        ...
        smallest, DEC 32001
        largest, DEC -32001
        ...

    Since these initializations are out of bounds of your expectations, the first input will always be captured as the smallest and also the largest.  (Using the max value will also work, e.g. 32000, -32000.)



The other problem is that when you do the if-then for the min check, it will skip the max check under certain circumstances, so your code looks a bit like this:

    if ( x < smallest ) {
        smallest = x;
        if ( x > largest )
            largest = x;
    }

This is a control flow logic error.  Should be checking for smallest and a largest independently as separate, sequential statements, not as nested statements.