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.
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: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.Disclaimer: code not tested, and it's been a few years since I did x86 asm :)
Are you looking up a single array multiple times without changing it? Is the array more than about eight to ten items long? If so, the fastest way to do it algorithm-wise is to create the array and sort it in the outer loop, and then whenever you want to do a lookup in your inner loop do a binary search. Binary searches are O(log n), while a straight linear search like you're doing here is O(n).
Also as a tip, sorting stuff in assembly is easy --- call qsort. It's really not worth writing your own in machine code!