News:

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

c struct, union, enum to asm

Started by stanks, April 11, 2008, 11:21:16 AM

Previous topic - Next topic

stanks

I am strugling with pointers to structures, enums. Here is example:


enum enum_field_types {
     a, b, c, d
};

typedef struct test1
    char *name;
    unsigned long length;
    enum enum_field_types type;
} test2;

So i did this:


...
a = 0
b = 1
c = 2
d = 3

test1 struct
     name dd ?
     length dd ?
test1 ends

What about this enum? Should i put it in union (after length inside test1 structure) or what?

Second example:

typedef char **X;

typedef struct x1 {
     struct x1 *next;
     X data;

I will have to find another examples for C too. I can't find them now....i'm lost
Any help?

Thanks

Ossa

Hi,

pointers to anything under Win32 are always DWORDs (i.e. 32-bit). So your second example would be:

x1 STRUCT
    next dd ?
    data dd ?
x1 ENDS


You will need to remember what type the pointers are.

As for the enumerations, you need to know the size of the field that they will go in and then define the values i.e.:

...
a EQU 0
b EQU 1
c EQU 2
d EQU 3

test1 struct
    _name dd ?
    _length dd ?
    _type db ?
test1 ends


note that "name", "length" and "type" are all reserved words in MASM syntax, so I have altered the names by preceding them with an underscore.

Ossa
Website (very old): ossa.the-wot.co.uk

Ian_B

Quote from: Ossaa EQU 0
b EQU 1
c EQU 2
d EQU 3

I don't think that'll work as I'm fairly sure that one or more of a b c d are reserved in MASM as well. In my MD5 code I tried to use these as subelements of a struct and got compile errors on the naming. My workaround was the same, to use a_ b_ c_ d_. The naming doesn't matter within your own code anyway, it just won't match exactly wherever you got the original struct definitions from.

stanks

Thanks guys. But how to translate struct x1 *next; to "normal" language. Variable next points to structure x1?
Why can't i write.

next x1 <>

or

next dword x1

What if i have this under C structure:

void (*error_handler)(void);

If i understand well (which i doubt  :8) ) from some C tuts, this is function called error_handler. First void means that this function returns no (meaning) value, and second void means that function have no params. Is that OK? And how to convert this? :)

Thanks again

drizz

Quote from: stanks on April 11, 2008, 05:35:02 PMBut how to translate struct x1 *next; to "normal" language. Variable next points to structure x1?
Why can't i write.

next x1 <>

or

next dword x1

first one is normal struct initialization, second is dword initialized to size of x1.

Quote from: stanks on April 11, 2008, 05:35:02 PM
What if i have this under C structure:

void (*error_handler)(void);

If i understand well (which i doubt  :8) ) from some C tuts, this is function called error_handler. First void means that this function returns no (meaning) value, and second void means that function have no params. Is that OK? And how to convert this? :)
That's correct. Here's how:

fn_error_handler typedef proto c  ;; function definition
ptr_error_handler typedef ptr fn_error_handler  ;; function pointer definition

x1 STRUCT
fn_e_h ptr_error_handler ?
x1 ends
ptr_x1 typedef ptr x1;;; ponter type definition

.data?
next ptr_x1 ?


and here's my enum macro
?Enum macro __EnumStart:=<0>,__EnumArgs:vararg
LOCAL i,arg
i = __EnumStart
for arg,<__EnumArgs>
ifnb <arg>
&arg equ i
endif
i = i + 1
endm
endm
The truth cannot be learned ... it can only be recognized.