Delphi/Max min from array

Advertisement


Question
Plz help me write a program in Assembly Language to find the maximum number and the minimum number from an array of ten positive numbers. Store the minimum number in AX and maximum number in DX.

[Hint: Use the conditional jumps]  

Answer
Lily,
 Delphi creates very fast native code, so creating this in assembly seems like overkill. If you were searching for the min and max in an array of 1,000, or 10,000 then perhaps you may be able to gain some speed from Assembly. Also, the Assembly code can be gleaned from similar Delphi code. For example:

procedure MinMax(Input: Array of LongInt; var Min, Max: LongInt);
var
 I: LongInt;
begin
 Min:=Input[Low(Input)];
 Max:=Min;
 for I:=Low(Input)+1 to High(Input) do
 begin
   if (Input[I]<Min) then Min:=Input[I];
   if (Input[I]>Max) then Max:=Input[I];
 end;
end;

Now if you create a program that uses this procedure, and set a breakpoint on the first line above, and then run it, and check the CPU window when it stops, you'll see the Assembly code that Delphi created, and then if you see somewhere that the speed could be improved, you can copy that code, and re-implement it in assembly.

Sincerely,
Brian

PS. You'll have to change the types to WORD in order to get the compiler to create numbers that will fit in the registers you've listed above.

Delphi

All Answers


Answers by Expert:


Ask Experts

Volunteer


Brian Sill

Expertise

Designing and implementing DOS, DPMI, and Windows apps in TP, BP, and Delphi (Windows VCL, Custom Controls, Runtime UI design, Windows API, Database, SQL, Threading, Btrieve, Pervasive.SQL, Advantage Database, ADO to MDB, ADO to MS SQL, MDI Apps, MS Office Integration, barcode generation, Document imaging application development, TIFF file structure, and some IFF file experience).

Experience

Turbo Pascal and Delphi developer since 1986. Proficient in web design incuding XHTML, CSS, and Javascript. Fluent in C, C++, Visual Basic (especially VBA), and Modula-2.

Education/Credentials
Over 20 years designing and implementing DOS, DPMI, and Windows apps in TP, BP, and Delphi.

©2012 About.com, a part of The New York Times Company. All rights reserved.