[NASM] Passing a data structure as a parameter?

Let's say, hypothetically, I wanted to call WSAStartup() in ASM, and the address is 0x7CFFEEDD.

The first parameter can simply be given 1, so we'll use that. However, the second parameter requires a WSADATA&. How would I go about doing that?

[BITS 32]

segment .text

global _start

_start:
push esp ; this is the WSADATA&. What do I do? :/
xor edx, edx
inc edx
push edx
mov eax, 0x7CFFEEDD
call eax
[510 byte] By [Zarathu] at [2007-11-20 8:30:37]
# 1 Re: [NASM] Passing a data structure as a parameter?
First you are NOT passing a structure. &

So yo pass a reference (which is actually a pointer in this case.

1) Allocate memory for the structure
2) Initialize the structure
3) Pass the address of the structure.
TheCPUWizard at 2007-11-10 3:55:09 >
# 2 Re: [NASM] Passing a data structure as a parameter?
1, 2, and 3 are very easy to put into words...but how do I go about this syntactically? I'm a bit new to ASM.
Zarathu at 2007-11-10 3:56:12 >
# 3 Re: [NASM] Passing a data structure as a parameter?
It looks like you're doing socket programming.

I don't use NASM but TASM syntax.

1. In your data area define/allocate space to WSADATA (I don't know how many bytes does WSADATA take).
2. 'mov' to some register, say EBX, the offset (also known as relative address) of that structure and push it before calling WSAStartup.

In TASM you would do:

.data
.... ; other data you may want to include

WSADATA db ' ' ; In this and following lines replicate
dw ; WSADATA structure definition in terms of
... ; bytes, words (2 bytes), d-words (4 bytes), etc.
;; End of WSADATA definition
... ; Other pgm variables, nothing to do with WSADATA
.code
.........
mov ebx, offset WSADATA
push ebx
mov eax, 7CFFEEDDh ; you have this and next lines (changed to TASM)
call eax
.........

Notes:
a) As far as I remember NASM doesn't use keyword 'offset' to refer to address. So omit 'offset', I just included it to be consistent with TASM syntax.
b) Make sure you comply with Parameter Passing order, so you push 1 and WSADATA address in the expected order.

Hope that helps.
iviggers at 2007-11-10 3:57:15 >
# 4 Re: [NASM] Passing a data structure as a parameter?
Oh, thanks! I didn't know you could have multiple lines defining a single directive like that.

So in essence:

struct S {
byte a,
byte b,
WORD c
};

would be:
S db 'X'
db 'Y'
dw 0x4141

?
Zarathu at 2007-11-10 3:58:21 >
# 5 Re: [NASM] Passing a data structure as a parameter?
Maybe (even probably, but it depends on lots of settings in your compiler. It could just as easily be

S db 'X'
db 0
db 'Y'
db 0
dw 0x4141
TheCPUWizard at 2007-11-10 3:59:22 >
# 6 Re: [NASM] Passing a data structure as a parameter?
Just make sure your code complies with NASM syntax. NASM is most related to Linux development. Then check NASM manuals, since directives like db may be substituted by 'byte' or so.
iviggers at 2007-11-10 4:00:20 >
# 7 Re: [NASM] Passing a data structure as a parameter?
Syntactically, it's fine... You can do "S db 's', 'u', 'p', 0 ;null-termination" and whatnot, so that's not a problem. I'll give it a go, thanks!

[EDIT]

Also, what if a part of a struct{} is another struct{}?

struct A {
B watevaz,
byte HAAAAAAAY
};

struct B {
byte stufflez,
byte anotherStufflez
};
Zarathu at 2007-11-10 4:01:24 >