0

BACKGROUND on this part of assignment:

COUNTLIST:
Generate an array counts which holds the number of times each value [10, 29] is seen in randArray. For example, counts[0] should equal the number instances of the value 10 in array. counts[14] should equal the number of instances of the value 24 in randArray

Added context: the list randArray is sorted in ascending order before countList is called. So an array with numbers: 10, 10, 11, 12, 12, would be copied over to another array as 2,1,2.

countList PROC
    push    EBP
    mov     EBP, ESP
    
    mov     ESI, [ESP + 20]            ; randArray (200 digits w/n this array)
    mov     ECX, [ESP + 16]            ; loop counter (ARRAYSIZE, which is 200)
    dec     ECX                        ; 200 - 1

    mov     EBX, [ESP + 12]            ; array we want to add to (countArray)
    mov     EDX, [ESP + 8]             ;"counter" for # of repeats for a digit

    _FirstLoop:
        ; grab first digit of sorted array
        mov     EAX, [ESI]

        ; compare current value to next value
        cmp     EAX, [ESI + 4]
        je      _NextDigit
        jne     _AddCountArray

    _NextDigit:
        ; move to next item in array
        inc     EDX
        add     ESI, 4
        loop    _FirstLoop

    _AddCountArray:
        ; add total number of instances of specified digit to countArray
        mov     [EBX], EDX
        add     EBX,  TYPE DWORD
        loop    _FirstLoop

    pop     EBP
    RET     16

countList ENDP

Maybe because I've looked at this assignment too long I'm missing where exactly the loop is messing up? The array that's meant to have values added into it is empty.

Any step in the right direction is much appreciated.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
chris
  • 21
  • 3
  • Did you try running your code in a debugger so you can see where it's messing up? – Joseph Sible-Reinstate Monica Nov 24 '20 at 04:52
  • ESI isn't incrementing to the next element like it's intended to be under _NextDigit but _AddCountArray is not adding any value to the new array. At least from what I see from the debugger and general output of the display procedure. – chris Nov 24 '20 at 05:02
  • You're counting array elements, not single digits. And you're just counting array elements (a normal histogram), not only *repeated* elements. The known range of values lets you easily map the value range to the count index range, and just `add dword ptr [counts + eax*4], 1` if you want to do it that way. (That's what you'd do on an unsorted list. Counting 1 + repeats might be faster for large runs of repeats on a sorted list, though.) – Peter Cordes Nov 24 '20 at 05:21
  • What happens when the inner `loop` instruction makes ECX = 0, and then execution reaches the 2nd `loop` instruction? You to handle that possible case at the bottom of your outer loop, after storing. e.g. maybe a `jecxz`, if you insist on using the `loop` instruction to tie ECX up as a counter even though you already have a pointer incrementing through the array which would make a perfectly good loop condition. (`do { ... }while(p != endp)`) – Peter Cordes Nov 24 '20 at 05:25
  • Unfortunately, I wouldn't be able to use do/while loops or ptr. The whole process would have to be as "babied down" (for lack of a better phrase here) as possible for this assignment. I have to generate an array of 200 random digits between 10-29, then sort the list and then do the procedure listed above (countList). soooo, it gets sorted regardless. – chris Nov 24 '20 at 05:33
  • Also I saw what you're mentioning in regards to _AddCountArray with the extra loop bit. Not sure how to go about fixing it right now but I'm definitely aware of it – chris Nov 24 '20 at 05:36
  • A `do {}while` loop in asm can literally be as simple as `cmp ecx, edx / jne top_of_loop` at the bottom. I'm writing it in C pseudocode because that's more compact, but all of that has exact equivalents in asm. [Why are loops always compiled into "do...while" style (tail jump)?](https://stackoverflow.com/q/47783926) – Peter Cordes Nov 25 '20 at 06:07

0 Answers0