简体   繁体   中英

Masm32. Find min and max in array

I need to write a procedure on Masm32, which should find min and max element in array. This procedure should take 4 parameters: 1) array of float numbers, 2) array's length, 3) min -- out parameter 4) max -- out parameter

I tried to write it, but it doesn't work. I can't get elements from array into procedure. Only first element is correct, but others is not.

This Procedure:

getMinMax PROC arr:QWORD, len:DWORD, _min:PTR QWORD, _max:PTR QWORD
  XOR ECX, ECX
  XOR EBX, EBX          

  MOV ECX, len
  DEC ECX

  FLD arr[0]
  FST _min
  FSTP _max

  ADD EBX, type arr

start:
  FLD arr[EBX]
  FLD _min

  FCOMPP
  FSTSW AX
  SAHF

  JP nomin 
  JZ nomin ; min == arr
  JC nomin ; min > arr

  ; min < arr[i]
  FLD arr[EBX]
  FSTP _min
  ADD EBX, type arr

nomin:
  LOOP start

  ret
getMinMax  ENDP

And here procedure call

invoke getMinMax, result, n, offset minVal, offset maxVal

where:

result dq 100 dup(?)
n       dd 4
minVal  dq ?
maxVal  dq ?

Could somebody help me?

a. Shouln't you use OFFSET/PTR on the first parameter?

invoke getMinMax, offset result, n, offset minVal, offset maxVal

getMinMax PROC arr:PTR QWORD, len:DWORD, _min:PTR QWORD, _max:PTR QWORD

b. You are dangerously supposing that LEN will always be at least 2. You should test for cases with LEN=0 and LEN=1

c. You have to swap these lines! The addition must always go through each iteration.

ADD EBX, type arr
nomin:

d. Your current program does not calculate any maximum.

e. Clearing ECX is superfluous right before moving LEN into it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM