When I call WSAStartup, like this:

sub esp, WSAdata_size
mov eax, esp
push eax
push dword 0x0002
call _WSAStartup@8

it returns 10107 (WSASYSCALLFAILURE). I tried running it on two computers. Has anyone been able to successfully use networking in NASM?

I'm using the latest version of NASM32 (march 8 2007)
Posted on 2007-08-22 13:26:10 by CommodoreLXIV

When I call WSAStartup, like this:

sub esp, WSAdata_size
mov eax, esp
push eax
push dword 0x0002
call _WSAStartup@8

it returns 10107 (WSASYSCALLFAILURE). I tried running it on two computers. Has anyone been able to successfully use networking in NASM?

I'm using the latest version of NASM32 (march 8 2007)


I get the same error when attempting to use the stack in such a manner. If you take a look at the WSAData Structure you will see instances of WORD/SHORT-sized (2-byte) values. Upon assuming those were actually DWORDS the value of WSAdata_size becomes, at least, 404 bytes. Using that value, your stack implementation works (returns zero). I don't know why this happens, as opposed to function-calling where almost everything is a DWORD, structures like this should work as-is.

Funny enough, the following code does exhibit error and assumes the value of WSAdata_size that NASM32 already states...



push wd
push dword 0x0002
call _WSAStartup@8


wd RESB WSAdata_size


The above code works on my machine. It should work on yours as well. I would suggest doing this as opposed to polluting the stack. You should never use the stack in such a manner unless you know with 100% certainty that it will work, which is hard to guarantee outside of using your own function libraries.

As a side note, in NASM32 you have the ability to use the INVOKE macro. Here is an example for you...


%include '..\..\..\inc\win32\windows.inc'
%include '..\..\..\inc\win32\ws2_32.inc'
%include '..\..\..\inc\nasm32.inc'

entry    WSADemo


proc    WSADemo

invoke WSAStartup, DWORD 0x0002, wc

ret

endproc


wc RESB WSAdata_size


msgNUM db '%i',13,10,0;


HtH :)
Posted on 2007-08-22 14:53:19 by SpooK
Aha, the problem is that it needs to be aligned to 4 bytes (either the stack or the WSAdata structure, I can't tell which). So this works:

sub esp, (WSAdata_size + 0x02)
mov eax, esp
push eax
push dword 0x0002
call _WSAStartup@8

but I'll take your suggestion and use the for this data. Thanks.
Posted on 2007-08-22 15:12:10 by CommodoreLXIV

Aha, the problem is that it needs to be aligned to 4 bytes (either the stack or the WSAdata structure, I can't tell which). So this works:

sub esp, (WSAdata_size + 0x02)
mov eax, esp
push eax
push dword 0x0002
call _WSAStartup@8

but I'll take your suggestion and use the for this data. Thanks.


From my OS development experience, the stack *shouldn't* absolutely need to be 4-byte aligned, but doing so increases stack use efficiency and helps reduce errors by functions that assume 4-byte alignment. As a result of this experience, you can safely assume that Windows needs the stack 4-byte aligned ;)
Posted on 2007-08-22 15:38:39 by SpooK
Win32 requires the stack to be 4-byte aligned. Win32 structures obviously need to have their fields aligned, including padding the structure to a multiple of four bytes (unless they're declared with "pragma pack(1)" in the header files).
Posted on 2007-08-22 18:38:06 by f0dder