How to find the minimum and maximum of three integers #232

This routine that shows how to find the minimum and maximum values of three integers using assembly code.

procedure MinMax(a, b, c: integer; var mn, mx: integer);
{-return min and max (a,b,c)}
asm
  cmp eax,edx      // if a > b then
  jg @1            //   goto @1
  xchg eax,edx     // else swap(a,b)
@1:
  cmp eax,ecx      // if a < c then
  jg @2            //   goto @2
  xchg eax,ecx     // else swap(a,c)
@2:
  cmp ecx,edx      // if c > e then
  jg @3            //   goto @3
  xchg ecx,edx     // else swap(b,c)
@3:
  mov ecx,[ebp+8]  // get the address of max
  mov [ecx],eax
  mov ecx,[ebp+12] // get the address of min
  mov [ecx],edx
end;
Original resource:
Author:
Contributor: Bruce Wernick
Added: 2014/01/22
Last updated: 2014/01/22