«« Site Rescued »»

While the site is still going to move from its current host, a new site will now take its place. More Info.

The new site may have less content, but the core will now remain. And it will now play nicely with phones! Keep an eye on the DelphiDabbler Blog for news.

» Help

How to find the minimum and maximum of three integers


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)}
  cmp eax,edx      // if a > b then
  jg @1            //   goto @1
  xchg eax,edx     // else swap(a,b)
  cmp eax,ecx      // if a < c then
  jg @2            //   goto @2
  xchg eax,ecx     // else swap(a,c)
  cmp ecx,edx      // if c > e then
  jg @3            //   goto @3
  xchg ecx,edx     // else swap(b,c)
  mov ecx,[ebp+8]  // get the address of max
  mov [ecx],eax
  mov ecx,[ebp+12] // get the address of min
  mov [ecx],edx
Author: Unknown
Contributor: Bruce Wernick
Added: 2014-01-22
Last updated: 2014-01-22

« Return to contents »