News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Issue sorting an array with size > 100

Started by sage112, March 02, 2015, 03:27:44 PM

Previous topic - Next topic

sage112

Hi everyone!

Currently learning MASM and the ix86 Architecture. I'm sorting an array in descending order and it works except when the size of array is larger than 100. It works fine with the program is <100. I've isolated the source of the error but I'm not sure why the code is causing it.

UPDATE: Here's the entire sort function and exchange procedures.

;---------------------------------------------------------------
;Sort Procedure - This will sort the array into descending order
;Receives: Address of the array and the user's request
;Returns: An array sorted in descending order
;Preconditions: The user must have provided a number of integers
; and the array must be filled with valid integers
;---------------------------------------------------------------s
;sort list
   Sort         PROC
      push   ebp
      mov      ebp, esp
      mov      edi, [ebp + 12]
      mov      ecx, [ebp + 8]
      dec      ecx            ;outer loop is set to one less than request
      mov      ebx, 0         ;k=0

   L1:
      mov      eax, ebx   ; i = k
      mov      edx, eax   
      inc      edx         ; j = k+1
      push   ecx            ;store the value of the outer loop
      mov      ecx, [ebp + 8]   ; inner loop is set to request

   L2:
      cmp      ecx,0
      je      exchangeNext
      mov      esi, [edi + edx*4]   ; store element j of the array
      cmp      esi, [edi + eax*4]   ;compare to element i
      jg      greater            
      inc      edx
      loop   L2
               

   greater:
      cmp      ecx,0
      je      exchangeNext
      mov      eax, edx      ; i = j
      inc      edx            ;increase j for the next iteration of the for loop
      loop   L2
   
   exchangeNext:
      lea      esi, [edi+ebx*4]            
      push   esi            
      lea      esi, [edi+eax*4]   ; swapping array[k] and array
      push   esi
      call   exchange
      pop      ecx            ;restore outer loop value
      inc      ebx            ;move forward through the outer loop
      loop   L1

      pop      ebp
      RET      8
   Sort      ENDP

;---------------------------------------------------------------
;Exchange- This will swap values in an array
;Receives: Address of the two values to swap
;Returns: Switches the values in the array
;Preconditions: The user must have provided a number of integers
;and the array must be filled with valid integers
;----------------------------------------------------------------      
   exchange   PROC
      pushad
      mov      edx, [edi+eax*4]   ; store the value of array
      mov      esi, [edi+ebx*4]   ; store the value of array[k]

;Error occurs when the values are swapped but only for array size > 100
      mov      [edi+eax*4], esi   ; switch the values
      mov      [edi+ebx*4], edx
      popad

      RET      8
   exchange   ENDP

dedndave

i don't think you're showing us enough code to figure out what's wrong

the lines you indicate may cause the problem
but, it might be because the pointers are off

we can't really say, because we don't know how the pointers are handled

sage112

Just updated my previous thread with more code. Would appreciate any help.

dedndave

it's a little hard to follow, but that is much better   :t
i will have some time in the morning to have a look at it

jj2007

Quote from: sage112 on March 02, 2015, 04:10:13 PM
Just updated my previous thread with more code. Would appreciate any help.

That is like sending a photo of a fish asking "how does it taste?". Send COMPLETE code, from .386 to end start. Or do you think we find it amusing adding the bits and pieces for you?

rrr314159

Hello sage112, welcome to the forum,

These guys are being awful tough on you! I think they should lighten up,

QuoteThe Campus: A protected forum where programmers learning assembler can ask questions in a sensible and safe atmosphere without being harassed or insulted.

You have to understand, when you get to the age of some of the people here, just getting out of bed can be so tough you're grumpy for the rest of the day!

The place where the error occurs in your algo is not where the coding error is. That's probably where you allocate and define the memory for the array that edi is pointing to. So u thot you were showing us the relevant spot - where the error occurs - but no, it's in the rest of the code. That's very frustrating to a person who sees that at a glance - and who probably took a half hour to get out of bed this morning because his lumbago was killing him :biggrin:

Take a closer look at your data statements, particularly that array. As a guess - I see that you're reading from edi+eax*4, and ebx*4, successfully, but it blows up (assuming that's the type of error you get?) when you try to write. If edi array is defined just above the code section - but it's 2 short - that's exactly what would happen. Try increasing its size.

I notice b4 you call exchange proc, you push those 2 addresses on the stack, then discard them returning, but never use them - instead you re-compute them in the proc. Obviously in an earlier version of the prog u got the addresses off the stack - perhaps stopped using them because of this problem, and decided to re-compute edi+eax*4? Shouldn't cause a problem but it's ugly (if u don't mind my saying so).

BTW don't write square brackets with an i between - the web page thinks it means italics!

Take 2 aspirin and call us in the morning with the rest of the code ... ideally, "COMPLETE code, from .386 to end start" so someone can actually run it and see the error occur. Don't forget to fill in the data.

c u later ...

I am NaN ;)

sage112

Thanks for the direction (and lack of snark) rrr314159! You were right the issue was that my edx was increment into unallocated memory. Thought it was better to no throw the entire program out to you guys but apparently that's the way to go from now on.

hutch--

 :biggrin:

> you're grumpy for the rest of the day!

Yeah, thats my excuse. Still, it makes life easier for folks who will test something if you post working code. Many here walk on water regularly but raising the dead is not an easy task.  :P

rrr314159

Glad it worked out, sage112, and good luck! But believe me, another day I'll be the snarky one and others will appear in a good mood. It's so easy for a comment meant as a joke to sound different to the recipient ... on the internet it's very important to cut everyone some slack, don't u agree?

c u later ...
I am NaN ;)