1 Replies - 836 Views - Last Post: 15 May 2017 - 05:23 PM

#1 javaStudent93  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 29
  • Joined: 05-October 16

Stack Procedure for Array Max and Min subtraction x86 MASM help!

Posted 27 April 2017 - 08:24 PM

So this is the .data segment of Arrays:
.data
arrayList01	DWORD	67,214,14,2,542,419,111,4,17,82,816,514,209,437,43,29,901,730,9,84
arraySize01	DWORD	($ - arrayList01) / 4	; number of 4-byte DoubleWords in arrayList01
arrayList02	DWORD	5,604,27,161,54,402,39,84,632,189,287,816,514,28,444,83,21,99,159,914
arraySize02	DWORD	($ - arrayList02) / 4	; number of 4-byte DoubleWords in arrayList02
arrayList03	DWORD	1024,31,24,604,77,151,22,389,704,64,76,489,62,319,93,217,48,777,82,19
arraySize03	DWORD	($ - arrayList03) / 4	; number of 4-byte DoubleWords in arrayList03 


In my main PROC i push all three array lists and sizes onto stack (pushing the arrayLists with OFFSETS) and then calling one PROC for each array to find the maximum number and minimum number and subtracting the minimum number from the maximum number in each array and print the three Ints.

I am having trouble with the PROC to calculate this when this PROC is called for each array. So far, my PROC just finds the maximum number in the array(s):
 calculateRange PROC

	push	ebp
	mov 	ebp, esp
	push	esi
	push	ecx		; save registers before using it to reference stack frame.

	mov	ecx,[ebp + 12]	; load array size in ecx.
	cmp	ecx, 0		; check array element count.
	jle	noElements	; skip comparison if no elements in array.
	
	mov	esi, [ebp + 8]	; load OFFSET of array in esi.
	mov	eax, [esi]	; store first array element in eax (initial largest).
	dec	ecx		; decrement array count by 1.

compare:
	add	esi, TYPE DWORD		; point to next array element.
	cmp	eax, [esi]		; compare array element to largest.
	jge	stillLargest		; jump if largest greater than or equal to element.
	mov	eax, [esi]		; store value in eax if larger than previous maximum.
		
stillLargest:
	loop	compare

noElements:
	pop	ecx
	pop	esi
	pop	ebp
	ret	8

calculateRange ENDP


I know I need to store the maximum number in the array in EAX and the minimum number in EBX and subtract EBX from EAX. My question is how? I just need a nice direction push, so to speak. I don't know how I can find/store the minimum number into EBX and subtract, and I'm not asking for the solution but if someone could help start me in a good direction it would help. What's troubling me is that we can only use this one PROC and not multiple PROCS so any help would be appreciated! Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Stack Procedure for Array Max and Min subtraction x86 MASM help!

#2 javaStudent93  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 29
  • Joined: 05-October 16

Re: Stack Procedure for Array Max and Min subtraction x86 MASM help!

Posted 15 May 2017 - 05:23 PM

Once again, I solved my own problem (since this seemed to be a hard one to solve for 320 views):

calculateRange PROC
; use STDCALL conventions to code procedure and clean up stack on return
	push	ebp
	mov 	ebp, esp
	push	esi
	push	ecx		; save registers before using it to reference stack frame.

	mov	ecx,[ebp + 12]	; load array size in ecx.
	cmp	ecx, 0		; check array element count.
	jle	noElements	; skip comparison if no elements in array.
	
	mov	esi, [ebp + 8]	; load OFFSET of array in esi.
	mov	eax, [esi]	; store first array element in eax (initial largest).
	mov	ebx, [esi]	; store first array element in ebx (initial smallest)
	dec	ecx		; decrement array count by 1.

compareMax:
	add	esi, TYPE DWORD		; point to next array element.
	cmp	eax, [esi]		; compare array element to largest.
	jge	compareMin		; jump if largest greater than or equal to element.
	mov	eax, [esi]		; store value in eax if larger than previous maximum.

compareMin:
	cmp	ebx, [esi]		; compare array element to smallest
	jle	stillSmallest		; jump if smallest less than or equal to element
	mov	ebx, [esi]		; store value in ebx if smaller than previous minimum
stillSmallest:
	loop	compareMax
	

noElements:
	sub 	eax, ebx		; subtract ebx(min) from eax(max)
	pop	ecx
	pop	esi
	pop	ebp
	ret	8

calculateRange ENDP 


Happy Assembling, folks!
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1