Subroutine for Determining Array Size and Address
INCLUDE NoIrvineBegin.inc
.data
minposmsg DB "The position in array of last occurrence of minimum element is: ",0
minelemmsg DB "The minimum element in the array is: ",0
maxposmsg DB "The position in array of last occurrence of maximum element is: ",0
maxelemmsg DB "The maximum element in the array is: ",0
array DWORD 5, 46, 90, 50, 5, 6, 90, 8, 20
max DWORD 0
maxposn DWORD 0
min DWORD 0
minposn DWORD 0
.code
MyProgram PROC
push eax ; save registers eax,ecx and edx before calling the subroutine
push ecx
push edx
mov eax,9
push eax ; push last parameter, size of array Arrsze
push OFFSET array ; push &array
push OFFSET minposn ; push &minposn
push OFFSET min ; push &min
push OFFSET maxposn ; push &maxposn
push OFFSET max ; push first parameter &max
call MinandMax ; call the minmax routine to fill in the variables with the max and min values found in the array
add esp,24 ; pop all the values we pushed on the stack for the function arguments
pop edx ; restore the registers we saved before the call
pop ecx
pop eax
mov edx,OFFSET minposmsg ; load address of min pos message in edx for displaying it
call WriteString ; display the message using the WriteString function
mov eax,minposn ; load the min pos value in eax for displaying
call WriteDec ; display the value as an integer using the WriteDec function
call CrLf ; print a line feed to change to next line
mov edx,OFFSET minelemmsg ; load address of min element message in edx for displaying it
call WriteString ; display the message using the WriteString function
mov eax,min ; load the min value in eax for displaying
call WriteDec ; display the value as an integer using the WriteDec function
call CrLf ; print a line feed to change to next line
mov edx,OFFSET maxposmsg ; load address of max pos message in edx for displaying it
call WriteString ; display the message using the WriteString function
mov eax,maxposn ; load the max pos value in eax for displaying
call WriteDec ; display the value as an integer using the WriteDec function
call CrLf ; print a line feed to change to next line
mov edx,OFFSET maxelemmsg ; load address of max element message in edx for displaying it
call WriteString ; display the message using the WriteString function
mov eax,max ; load the max value in eax for displaying
call WriteDec ; display the value as an integer using the WriteDec function
call CrLf ; print a line feed to change to next line
exit ; exit the program
MyProgram ENDP
;MinandMax(int &max, int &maxposn, int &min, int&minposn,
; int& Array, int Arrsze);
MinandMax PROC
push ebp ; save ebp in the stack
mov ebp,esp ; point to top of stack with ebp
push ebx ; save registers
push edi
push esi
mov ecx,0 ; ecx will be the index in the array, start from zero
mov esi,[ebp+24] ; get the array address
mov eax,[esi] ; load the first element in the array
mov ebx,[esi] ; set the minimum in register ebx for comparing
mov edx,[esi] ; set the maximum in register edx for comparing
loop1:
mov eax,[esi+ecx*4] ; load current element in the array
ifmin:
cmp eax,ebx ; compare current value with the minimum
jg ifmax ; if it's greater than the minimum, skip and test the maximum
mov edi,[ebp+16] ; load the address of the minimum value in edi
mov [edi],eax ; put the minimum value as the current element in the array
mov edi,[ebp+20] ; load the address of the minimum position in edi
mov [edi],ecx ; set the minimum position as the curent element position
mov ebx,eax ; load the new minimum in ebx
jmp else1 ; jump to else1 to continue the loop with the next element
ifmax:
cmp eax,edx ; compare current value with the maximum
jl else1 ; if it's less than the maximum, skip to go to next element
mov edi,[ebp+8] ; load the address of the maximum value in edi
mov [edi],eax ; put the maximum value as the current element in the array
mov edi,[ebp+12] ; load the address of the maximum position in edi
mov [edi],ecx ; set the maximum position as the curent element position
mov edx,eax ; load the new maximum in edx
else1:
inc ecx ; increment ecx to go to next element in array
cmp ecx,[ebp+28] ; compare the current position with the size of the array
jl loop1 ; if we haven't gone beyond the last element continue the loop
pop esi ; restore registers
pop edi
pop ebx
pop ebp ; restore ebp from the stack
ret ; return to caller
MinandMax ENDP
INCLUDE NoIrvineEnd.inc
END MyProgram