我试图在程序集中生成一个检查两个字符串的程序。

section .data
str1 db 'mystring'
str2 db 'mystring'

output db 'cmp went fine'
len equ $-output

section .text
global main

main:
    mov ecx, str1
    cmp ecx, str2
    je ifBody0
    int 80h

    mov eax, 1
    mov ebx, 0
    int 80h

ifBody0:
    mov eax, 4
    mov ebx, 1
    mov ecx, output
    mov edx, outputlen
    int 80h

奇怪的是,当我调用条件跳转:je [label]时,它不起作用。但当我把je改为jne时,它就工作了。
我想知道我在这里做错了什么。
提前谢谢你,
大安

最佳答案

为了比较x86程序集中的字符串,有一个名为CMPS(Compare Strings)的特殊操作码。对于字节字符串,相关的操作码是CMPSB。您可以将ESI设置为源字符串,将EDI设置为目标字符串。相等检查的长度(最好是最长的字符串)在ECX中设置。小心溢出!.
所以你的代码可能是这样的:

section .data
str1 db 'mystring',0
str1len equ $-str1
str2 db 'mystring',0

output db 'cmp went fine',0x0a,0
outputlen equ $-output
output2 db 'cmp went wrong',0x0a,0
output2len equ $-output2

section .text
global main

main:
    lea esi, [str1]
    lea edi, [str2]
    mov ecx, str1len  ; selects the length of the first string as maximum for comparison
    rep cmpsb         ; comparison of ECX number of bytes
    mov eax, 4        ; does not modify flags
    mov ebx, 1        ; does not modify flags
    jne ifWrong       ; checks ZERO flag

ifRight:              ; the two strings do match
    mov ecx, output
    mov edx, outputlen
    int 80h
    jmp exit
ifWrong:              ; the two strings don't match
    mov ecx, output2
    mov edx, output2len
    int 80h
exit:                 ; sane shutdown
    mov eax, 1
    mov ebx, 0
    int 80h

10-07 20:10
查看更多