【问题标题】:.IF comparing strings MASM.IF 比较字符串 MASM
【发布时间】:2013-10-19 17:01:49
【问题描述】:

我如何实现这样的目标:

abc db "abc",0
def db "def",0
textnotequal db "strings are not equal",0
textequal db "strings are equal",0

 .if abc != def
    invoke MessageBox, NULL, addr textnotequal, addr textnotequal, MB_OK

 .elseif abc == def
     invoke MessageBox, NULL, addr textequal, addr textequal, MB_OK
 .endif

我是否需要先将 abc & def 移入某些东西,或者这通常是不可能的?

【问题讨论】:

  • abcdef 是指向字符串的指针,而不是字符串本身。通过比较它们,您可以比较不同的内存地址,除非它们指向同一个地方,否则它们是不同的,即使它们指向的字符串是相等的。为了比较字符串,您必须访问这些地址并在每个地址之间逐字节进行更深入的比较,以便了解字符串是否相等。您可能需要为此编写一个函数。
  • 没错,我在下面使用简单的 repe cmpsb 行编写了这样一个函数的示例,以进行更深入的比较。

标签: assembly masm masm32


【解决方案1】:

您可以在汇编中编写您的 cmpstr 函数版本。例如:

abc db "abc",0
def db "def",0
...
mov ecx,3     #the length of the abc and def strings
cld           #set the direction flag so that EDI and ESI will increase using repe
mov esi, offset [abc]  #moves address of abc string into esi
mov edi, offset [def]  #exact syntax may differ depending on assembler you use
                       #I am not exactly sure what MASM accepts but certainly something similar to this
repe cmpsb     #repeat compare [esi] with [edi] until ecx!=0 and current chars in strings match
               #edi and esi increase for each repetition, so pointing to the next char
cmp ecx,0      #test if the above command passed until the end of strings
je strings_are_equal  #if yes then strings are equal
# here print the message that strings are not equal (i.e. invoke MessageBox)
jmp end
strings_not_equal:
# here print the message that strings are equal (i.e. invoke MessageBox)
end:

【讨论】:

  • 最终使用了 lstrcmpi.if eax == FALSE 但这绝对让我朝着正确的方向前进,谢谢。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-15
相关资源
最近更新 更多