Search code examples
assemblyx86masmirvine32

Test if value in EAX is the same as any value in a array x86


I am attempting to test if the random value generated in eax is the same as any value in an array I have allocated. The outer loop generates the array and writes it to the screen and in to the array, The inner loop is then supposed to test if the value exists in the array. I know i am not doing the inner loop correctly but I am not sure how to fix it.

It assembles just fine but when I try to run I only get a blank cmd window screen. Also I am using Irvine32 libraries. My code is below:

EDIT: I appreciate your guys help so far but now I Have two problems. The first is that when I try to evaluate the number in eax for uniqueness against my array I actually get an access violation error. My code for generating the array and testing it is below:

RandomArray PROC uses EAX

    call Randomize 
    mov esi, OFFSET arr
    mov edi, OFFSET arr
    mov ebx, TYPE arr 
    mov ecx, 15
    L1:
        mov eax, [79 - 19]
        push eax
        call RandomRange
        add eax, 19
        search1:
                 mov edx,[esi]
                 cmp eax,edx                 ; compares the values in the array and the random int
                 je L1                       ; jumps if the values are equal
                 add esi,4                   ; moves to next byte to check again
                 loop search1                ; repeats loop
        mov [esi],eax
        pop eax
        add esi, ebx
        loop L1
    ret
RandomArray ENDP
        pop ecx     
        loop OUTER_LOOP

    jmp FINISHED

    SWAP:
        mov bl, [esi]
        mov dl, [esi+1]
        xchg bl,dl 
        mov [esi],dl 
        mov [esi+1],bl
        jmp CONTINUE 

    FINISHED:
    ret

Thanks for your help in advance.


Solution

  • I only get a blank cmd window screen

    search:
        mov ecx,elementcount
        cmp eax,[esi]
        je L1
        add esi,4
        loop search
    

    This is an infinite loop because you are resetting the counter ecx on each iteration. Move the ecx assignment outside the loop and you should be fine:

    mov ecx,elementcount
    
    search:
        cmp eax,[esi]
        je L1
        add esi,4
        loop search
    

    Btw, you could probably replace that loop with rep scasd, which I believe does the exact same thing. Not sure whether it is "better" in any way though.

    mov ecx,elementcount
    rep scasd
    je L1
    

    Disclaimer: code not tested, and it's been a few years since I did x86 asm :)