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

China☆狼群 提交于 2019-12-02 01:47:43

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 :)

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!