'In MASM Assembly Langauge, how would I find the MIN and MAX of values not in an array?

I am currently creating a program that repeatedly asks the user to input a value within a specified range and any value not within the range, will terminate the loop and calculate the sum, rounded average, MIN, and MAX.

I have been able to calculate the sum and average by storing and accumulating them each time the variable is accessed. Though, I am stuck on how to find the MIN and MAX of the valid values entered by the user, to then display it on the screen. My first thought was to create a variable for each value (min and max) that counts the value and only updates the variable if it is greater or lesser than the previously stored value. When attempting the implementation though, I am getting confused. Would I need to compare the user input value to the variable and update it as the MAX gets bigger or MIN get smaller? Current implementation:

_lowerLimit2Valid:
_lowerLimit1Valid:
inc validNumbers                    ;Increment validNumbers by 1 for each valid number.
inc inputNum                        ;Increment inputNum by 1 for each valid number.
add sum, eax                        ;Add the valid user number to sum variable.
cmp min, eax
jg _updateMin
jmp _inputAgain
_updateMin:
mov min, eax
cmp max, eax
jl _updateMax
jmp _inputAgain
_updateMax:
mov max, eax
jmp _inputAgain

From the above, I am actually able to update the MIN value correctly by comparing the values and updating only if it's bigger. Now my issue lies in getting the MAX value. I have figured out that the implementation above never reaches my max value as it is only updating my MIN value before looping for another value. I have MIN and MAX initialized to 0 and know how to update with each loop, but how would I grab only the first value if it is the biggest of the bunch? For example, -5, -10, -25, -45 --> Max = -5, Min = -45. As I've said, from my implementation, I have either gotten 0 (doesn't update) or a number that isn't the MAX.



Solution 1:[1]

Updating my code to this:

_lowerLimit2Valid:
_lowerLimit1Valid:
  inc validNumbers              ;Increment validNumbers by 1 for each valid number.
  inc inputNum                  ;Increment inputNum by 1 for each valid number.
  add sum, eax                  ;Add the valid user number to sum variable.
  cmp min, eax
  jge _updateMin
  jle _updateMax
  jmp _inputAgain
_updateMin:
  mov min, eax
  jmp _inputAgain
_updateMax:
  mov max, eax
  jmp _inputAgain

Makes it work as I intended. Feels like when I look at code for long periods of time I start to forget or miss obvious things.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Homerian