News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

WSAStartup

Started by Slugsnack, July 07, 2009, 08:25:58 PM

Previous topic - Next topic

Slugsnack

is the first parameter of it supposed to be a word.. or a dword ? MSDN seems to say WORD but masm's .inc file for ws2_32 says it's 2 dwords. when i tried pushing ax it crashes. doing this however, works:

mov eax, 2
mov ah, 2


then using pushing eax instead of ax. is MSDN wrong ?

Slugsnack

another question now. iczelion just initialises winsock library then calls socket with raw values whereas msdn examples use addrinfo/socketinfo structures. is there any advantage of msdn's method ?

disintx

int WSAStartup (

    WORD wVersionRequested,
    LPWSADATA lpWSAData
   );


wVersionRequested info:
QuotewVersionRequested

[in] The highest version of Windows Sockets support that the caller can use. The high order byte specifies the minor version (revision) number; the low-order byte specifies the major version number.

now, from Iczelion's example he uses
invoke WSAStartup,101h,ADDR wsadata   ; Initialize the window socket dll
He uses a constant value, I don't know why you want to pass the version info to EAX and then push that..

MichaelW

#3
At least for ML 6.14, INVOKE does not pass WORD parameters correctly. INVOKE will pass a BYTE or a WORD correctly if the parameters for the invoke match those for the prototype/procedure definition. The problem with the code below is a disagreement between the prototype and the invoke.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
    include \masm32\include\ws2_32.inc
    includelib \masm32\lib\ws2_32.lib
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      wsaData WSADATA <>
      wVersionRequested dw 202h
      dVersionRequested dd 202h
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    ;------------------------------------------------------------------
    ; This does not work. The problem is the lack of an operand-size
    ; prefix (66h) on the second PUSH instruction. Without this prefix
    ; to override the default operand size (32 bits) the instruction
    ; pushes a DWORD, so after the next PUSH instruction pushes a WORD
    ; the stack no longer has the required 4-byte alignment.
    ;------------------------------------------------------------------
    ;invoke WSAStartup, wVersionRequested, ADDR wsaData
    ; 00401000 6800304000             push    403000h
    ; 00401005 6A00                   push    0
    ; 00401007 66FF358E314000         push    word ptr [40318Eh]
    ; 0040100E E8AF010000             call    fn_004011C2

    ;----------------
    ; This works OK.
    ;----------------

    push OFFSET wsaData              ; push DWORD
    push WORD PTR 0                  ; push high-order WORD
    push wVersionRequested           ; push low-order WORD
    call WSAStartup
    ; 00401000 6800304000             push    403000h
    ; 00401005 66680000               push    0
    ; 00401009 66FF358E314000         push    word ptr [40318Eh]
    ; 00401010 E8BD010000             call    fn_004011D2

    print ustr$(eax),13,10

    ;----------------------------
    ; And both of these work OK.
    ;----------------------------

    invoke WSAStartup, dVersionRequested, ADDR wsaData
    print ustr$(eax),13,10

    invoke WSAStartup, 202h, ADDR wsaData
    print ustr$(eax),13,10,13,10

    inkey "Press any key to exit..."
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

eschew obfuscation

evlncrn8

Quote from: Slugsnack on July 07, 2009, 08:25:58 PM
is the first parameter of it supposed to be a word.. or a dword ? MSDN seems to say WORD but masm's .inc file for ws2_32 says it's 2 dwords. when i tried pushing ax it crashes. doing this however, works:

mov eax, 2
mov ah, 2


then using pushing eax instead of ax. is MSDN wrong ?

because in 32 bit you're expected to push 32 bit values as the parameters... pushing ax would be 16 bit, and unbalance the stack...

alternatively u could do

push 0202h ; does the eax, 2... ah, 2 thing for you


ToutEnMasm


Masm always pass byte,word in a dword to keep safe the stack.
The movzx intrustion is of good use to translate a byte,word to a dword.
This avoid that masm made the translation by itself.
Masm have no other choice than use a register to make the translate.
In this case one register is modified without prompt of the user.
Quote
invoke Parametre,petit ;with ,petit BYTE ,Parametre PROTO :BYTE
            ------- listing ----------
   mov    al, petit
   push   eax         
   call   Parametre
   ;-----------------------------------------
   invoke Parametre,moyen   ;moyen word    Parametre PROTO :WORD
            ------- listing ----------
   sub    esp, 002h      ;word undefined
   push   moyen
   call   Parametre   
   ;------------------------------------------------
   invoke Parametre,moyen   ;moyen word    Parametre PROTO :DWORD
            ------- listing ----------
   push   000h         ;word NULL in stack
   push   moyen      ;
   call   Parametre   
   ;------------------------------------------------
   invoke Parametre,petit   ;petit byte    Parametre PROTO :DWORD
            ------- listing ----------   
        push   000h         ;word NULL in stack
   mov    al, petit      ;translate byte in word
   movzx  ax, al     
   push   ax         ;=DWORD in stack   or movzx eax,al follow by push eax
   call   Parametre     
   ;----------------------------------------------------------------

dedndave

if you pass a byte in a dword frame
it shouldn't matter what the high-order bits are
they can be garbage - is that not correct ?

.data
someByte db 80h
someOtherStuff db 0ffh,20h,44h

.code
push dword ptr someByte

or

INVOKE someFunction,dword ptr someByte

MichaelW

Quote from: ToutEnMasm on July 09, 2009, 03:53:45 PM
Masm always pass byte,word in a dword to keep safe the stack.

Yes, as long as the parameters for the invoke match those for the prototype/procedure definition. The problem with my code was a disagreement between the prototype and the invoke.

eschew obfuscation

Slugsnack

ahh yes i forgot about the alignment.. i was so confused why it was passing a dword when it was clearly a word LOL. got it now thanks guys ; )

ToutEnMasm


The WSAStartup function is define as follow by the masm include file and by the sdk translate
Quote
WSAStartup PROTO :DWORD,:DWORD
There is no problem changing proto :BYTE to proto :DWORD
just use of movzx function is needed or masm made it


The structure have no need in alignment.
There is two versions of the winsock.h    (winsock2.h)
Take care of the version passed to the function
Quote
wVersionRequested
[in] Highest version of Windows Sockets support that the caller can use. The high-order byte specifies the minor version (revision) number; the low-order byte specifies the major version number
If it is a bad version,the dll is not found !
winsock2
Quote
WSADATA   STRUCT
   wVersion WORD ?
   wHighVersion WORD ?
IFDEF _WIN64
   iMaxSockets WORD ?
   iMaxUdpDg WORD ?
   lpVendorInfo DWORD ?
   szDescription BYTE WSADESCRIPTION_LEN+1 dup (?)
   szSystemStatus BYTE WSASYS_STATUS_LEN+1 dup (?)
   ELSE
   szDescription BYTE WSADESCRIPTION_LEN+1 dup (?)
   szSystemStatus BYTE WSASYS_STATUS_LEN+1 dup (?)
   iMaxSockets WORD ?
   iMaxUdpDg WORD ?
   lpVendorInfo DWORD ?
ENDIF
WSADATA      ENDS