MASM cmpsb问题

时间:2010-10-09 09:56:44

标签: assembly winsock masm

我在比较两个字符串时遇到问题,一个字符串从irc服务器接收数据,一次一行,另一个字符串保存硬编码数据(“PING:”) 但每次我尝试比较字符串都没有任何反应。你能帮助我吗?

比较功能在Handleping

这是我目前正在使用的代码:

.386
.model flat, stdcall
option casemap: none


include \masm32\include\windows.inc 
include \masm32\include\user32.inc 
include \masm32\include\kernel32.inc 
include \masm32\include\shell32.inc
include \masm32\include\wsock32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\shell32.lib
includelib \masm32\lib\user32.lib 
includelib \masm32\lib\kernel32.lib 
includelib \masm32\lib\wsock32.lib
includelib \masm32\lib\masm32.lib
include \masm32\include\msvcrt.inc
includelib  \masm32\lib\msvcrt.lib


.data

txt db "An error occured while calling WSAStartup",0
txt1 db "An error occured while creating a socket",0
txt2 db "An error occured while connecting",0
capt db "SCHiM",0
wsadata WSADATA <>
hostname db "irc.corruptcode.org",0
Port dd 6667 
USER db "USER SCHiMBez 8 * :SCHiMBez",13,10 
CHANNEL db "JOIN #botss",13,10  
NICK db "NICK SCHiMBez",13,10 
trans_buffer db 500 dup (0)
failmatch db "They match!",0
sin sockaddr_in <?>
buff      db 500 dup (0) 
bbuff db (0)
sendbuff db 500 dup (0)
Pong db "PONG :irc.corruptcode.org",13,10,0
Ping db "PING :irc.corruptcode.org"

CLRF    db 13d, 10d
lstring EQU     LENGTHOF Ping


.data?

sock dd ? 
ErrorCode  dd ?


.code



show_error proc caption:ptr byte, err_txt:ptr byte
    invoke WSAGetLastError
    mov ErrorCode, eax
    invoke MessageBoxA, MB_OK, caption, err_txt, 0
    ret
show_error endp

main proc
    invoke  AllocConsole
    invoke WSAStartup, 101h,addr wsadata

    .if eax==0   ; An error occured if eax != 0, because there's no return value for this api, if there's return, there's an error
        invoke socket,AF_INET,SOCK_STREAM,0     ; Create a stream socket for internet use
        .if eax!=INVALID_SOCKET
            mov sock,eax

            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            ;Now we have a socket ready for use, we still have to be able to connect to somewere though...
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

            mov sin.sin_family, AF_INET
            invoke htons, Port  ; convert port number into network byte order first
            mov sin.sin_port,ax ; note that this member is a word-size param.
            invoke gethostbyname, addr hostname

            mov eax,[eax+12]    ; move the value of h_list member into eax
            mov eax,[eax]       ; copy the pointer to the actual IP address into eax
            mov eax,[eax]       ; copy IP address into eax
            mov sin.sin_addr,eax

            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            ;Now That's done we can connect to a site! (an irc channel in this case)
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

            invoke connect, sock, addr sin, sizeof sin

;invoke lstrcpy, addr sendbuff, addr USER   

;call sndd  ;possible error producer ;p ;if it produces an error, uncomment...

invoke send, sock, addr USER, 29,   0   
invoke send, sock, addr NICK, 15,   0   
invoke send, sock, addr CHANNEL, 13,    0   




            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            ;Receive response from the server
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤


  loopspt:  


        call Recvv  ;Receiving data



        call HandlePing
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            ;Do something with the data
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤


        invoke MessageBox, 0, addr buff, addr capt, 0


 jmp loopspt 




            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            ;All data received & check for errors
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

            .else
                invoke show_error, offset capt, offset txt2
            .endif
        .else
            invoke show_error, offset capt, offset txt1
        .endif
     invoke ExitProcess, 0

            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            ;Recvv funciong
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
Recvv:

invoke RtlZeroMemory, addr buff, sizeof buff
mov bbuff, 0

Gline:

invoke recv,sock,addr bbuff,sizeof bbuff,0

cmp bbuff, 10d
je done

invoke lstrcat, addr buff, addr bbuff   

jmp Gline

done:
ret


            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            ;Recvv funciong
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤


            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            ;Ping? Pong! commented for now
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤


HandlePing:         


        cld                         ; Work upward
        mov     cx, lstring         ; Load length of string
        mov     esi, offset buff  ; Load offset of string1
        mov     edi, offset Ping  ; Load offset of string2
        repe    cmpsb               ; Compare
        je      allmatch            ; Jump if all match

        jmp zzor
        allmatch:                          
        invoke MessageBoxA, 0, addr failmatch, addr capt, MB_OK

zzor:
ret                             ;return

            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            ;Ping? Pong! commented for now
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤


            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            ;Send function
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

sndd:

invoke lstrcat, addr sendbuff, 13d   ;this works 
invoke lstrcat, addr sendbuff, 10d   ;this works

invoke lstrlen, sendbuff    ;ERROR HERE, ERROR HERE!!!


invoke send, sock, addr sendbuff, eax,  0



ret

            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
            ;Send function
            ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤





main endp
end main

提前致谢 -SCHiM

ps:我现在要去健身房,我可以在2或2.5小时内回复任何评论

1 个答案:

答案 0 :(得分:1)

我怀疑问题在这里:

    mov     cx, lstring         ; Load length of string

在32位模式下,REP前缀使用ECX的全部32位作为计数。可能发生的事情是ECX的上半部分有一个非零值,因此repe cmpsb扫描得太远,并且不可避免地很快就会遇到不匹配的字节。