试图在汇编中进行不区分大小写的搜索,我的代码不起作用

时间:2015-07-18 00:49:16

标签: assembly x86

我有一个结构数组,每个元素都是数字和街道名称

struct house
{
    int number;
    char streetName[20];
}

我想用传入的令牌搜索它。这是我的代码,我不知道为什么它不起作用。请帮忙!

mov eax, 0   ; zero out the result
mov esi, list    ; move the list pointer to ESI
mov edi, token   ; move the pointer of token string to edi
mov edx, 0       ; reset edx counter
mov ecx, 0       ; reset ecx counter
L1:
mov eax, [esi+4] ; read number of street into eax
add esi, 4       ; move pointer 4 bytes to start reading street name
L2:
mov al, byte ptr[esi + ecx]; mov each char of street name from array to al
mov bl, byte ptr[edi + ecx]; mov each char of search token to bl
or al, 20h       ; convert al (case insensitive)
or bl, 20h       ; convert bl (case insensitive)
inc ecx          ; prepare next char
cmp al, bl       ; cmp al and bl
jne DIFFERENT    ; jump to DIFFERENT if different
cmp bl, 0        ; check if bl reaches to the end of the string
je done          ; jump if all match to done
jmp L2           ; jump back to L2 to check the next char
DIFFERENT:
add esi, 24      ; add esi 24 bytes to move the pointer to the next item of structure array
mov ecx, 0       ; reset the counter ecx
inc edx          ; inc the edx counter for structure array
cmp edx, count   ; check if it reaches the end of array
je not_found     ; if reaching the end but found nothing then jmp to not found
jmp L1           ; jump back to L1
not_found: 
mov eax, 0       ; set eax to 0 to indicate not found
done:

2 个答案:

答案 0 :(得分:3)

L1:
mov eax, [esi+4] ; read number of street into eax
add esi, 4       ; move pointer 4 bytes to start reading street name
L2:
mov al, byte ptr[esi + ecx]; mov each char of street name from array to al

在代码的这一部分中,读取街道号是没用的,因为之后使用AL来处理字符。

因此,你希望这个程序在字符串匹配时产生什么?至少AL为0(街道号通常是小数?),你定义EAX = 0作为未找到的信号!

我建议以下内容在EAX寄存器中留下合理的值:

L2:
mov bh, byte ptr[esi + ecx]; mov each char of street name from array to bh
mov bl, byte ptr[edi + ecx]; mov each char of search token to bl
cmp bl, 0        ; check if bl reaches to the end of the string
je done          ; jump if all match to done
or bx, 2020h     ; convert both characters at once (case insensitive)
inc ecx          ; prepare next char
cmp bh, bl       ; cmp bh and bl
je L2            ; jump back to L2 to check the next char
DIFFERENT:

您只测试标记字符串的终止零。但是当数组字符串的长度不同时会发生什么?只有当两个字符串具有相同的长度时,您的程序才能工作。

答案 1 :(得分:1)

The x86 tag wiki页面包含有关使用调试器以及指南和其他资源的信息。这正是使用调试器解决问题的理想问题。由于knm241指出的错误,你会发现你的分支没有按预期分支。