The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: lelejau on December 13, 2010, 06:48:03 PM

Title: structs
Post by: lelejau on December 13, 2010, 06:48:03 PM
Hello. I'm wondering if its possible to create structs in asm like the ones we do in C++.
Title: Re: structs
Post by: Vortex on December 13, 2010, 06:57:06 PM
Hi lelejau,

Welcome to the forum.

Yes, you can create structures in asm. There are a lot of examples in the Masm32 package and the forum.
Title: Re: structs
Post by: lelejau on December 13, 2010, 06:58:58 PM
thanks. Well, I'm not familiarized (sorry If its wrong, i'm BR) with the MASM examples. It is abit hard to understand them atm. Could you give me a hand?
Title: Re: structs
Post by: redskull on December 13, 2010, 07:04:30 PM
MyStructure STRUCT
  MyElement1  DWORD ?
  MyElement2  DWORD ?
MyStructure ENDS


equals

struct MyStructure  {
    int MyElement1;
    int MyElement2;
};


-r
Title: Re: structs
Post by: lelejau on December 13, 2010, 07:12:23 PM
ok thanks, now, to use I do:

LOCAL myStruct:MyStructure


mov myStruct.MyElement1, 12h

Is that correct?
Title: Re: structs
Post by: dedndave on December 13, 2010, 07:14:09 PM
that looks good   :U
Title: Re: structs
Post by: lelejau on December 13, 2010, 07:26:16 PM
Thanks  :clap:
Title: Re: structs
Post by: drizz on December 13, 2010, 07:26:27 PM
Quote from: redskull on December 13, 2010, 07:04:30 PM
MyStructure STRUCT
  MyElement1  DWORD ?
  MyElement2  DWORD ?
MyStructure ENDS


equals

struct MyStructure  {
    int MyElement1;
    int MyElement2;
};

Actually it's not equal,
int = sdword
unsigned int = dword
// at least for 32/64 bit c++ compilers for which sizeof(int)==4


EDIT: example:
.if [edx].MyStructure.MyElement1 > 0
.endif




Title: Re: structs
Post by: lelejau on December 13, 2010, 07:29:12 PM
I'm getting error...


Quote
.data

MYSTRUCTURE STRUCT

    Window dd ?

MYSTRUCTURE ENDS


.code
   

start:

LOCAL myStruc:MYSTRUCTURE

END start

I get error A2012.
Title: Re: structs
Post by: jj2007 on December 13, 2010, 07:32:48 PM
LOCAL must be inside a proc:
include \masm32\include\masm32rt.inc

MYSTRUCTURE STRUCT

    Window dd ?

MYSTRUCTURE ENDS


.code
   

start: call MyMain
exit

MyMain proc
LOCAL myStruc:MYSTRUCTURE
MyMain endp


end start
Title: Re: structs
Post by: dedndave on December 13, 2010, 07:33:15 PM
or try this...
include \masm32\include\masm32rt.inc

MYSTRUCTURE STRUCT

    Window dd ?

MYSTRUCTURE ENDS

.code
   
MyMain proc
LOCAL myStruc:MYSTRUCTURE

mov eax,myStruc.Window
exit

MyMain endp


end MyMain


i noticed you have the structure definition inside the data section
that isn't necessary, as it only defines the data type
it does not define the data
Title: Re: structs
Post by: lelejau on December 13, 2010, 07:37:45 PM
Jesus, this looks weird..

Quote
Main proc
    invoke  CreateFile,ADDR filename,GENERIC_WRITE,\
            0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,0
    mov     hFile,eax
    invoke  WriteFile,eax,ADDR text,TEXT_LEN,ADDR _size,0
    invoke  CloseHandle,hFile


    invoke CreateFile, ADDR filename, GENERIC_READ,\
            0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,0
    mov hFile,eax
    invoke GetFileSize,hFile,NULL
    mov fsiz, eax
    invoke SetFilePointer, hFile,12,NULL,FILE_BEGIN
    invoke ReadFile,hFile, ADDR temp_buffer,fsiz,ADDR SizeReadWrite,NULL
    invoke CloseHandle, hFile

   
    invoke MessageBox, 0 ,ADDR temp_buffer, ADDR temp_buffer, MB_OK

   
Main endp
start:
    call Main
    invoke  ExitProcess,0
END start
The MessageBox seems to never stop.
Title: Re: structs
Post by: drizz on December 13, 2010, 07:40:33 PM
As Opposed to "void" type functions in c++, you need RET in asm.
Title: Re: structs
Post by: lelejau on December 13, 2010, 07:41:35 PM
ehehehe. It worked now.

In this code, I'm reading string from txt file.

How could I put it into MYSTRUCT?

But as an integer,not as string.
Title: Re: structs
Post by: dedndave on December 13, 2010, 07:52:25 PM
"text" may not be a very good name   :P
but, this works...
    INCLUDE \masm32\include\masm32rt.inc

    .DATA

filename db 'SomeFile.txt',0
text1    db '123456789012Some Text'

    .DATA?

hFile         dd ?
fsiz          dd ?
_size         dd ?
SizeReadWrite dd ?
temp_buffer   db 24 dup(?)

    .CODE

Main proc
    invoke  CreateFile,ADDR filename,GENERIC_WRITE,\
            0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,0
    mov     hFile,eax
    invoke  WriteFile,eax,ADDR text1,sizeof text1,ADDR _size,0
    invoke  CloseHandle,hFile


    invoke CreateFile, ADDR filename, GENERIC_READ,\
            0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,0
    mov hFile,eax
    invoke GetFileSize,hFile,NULL
    mov fsiz, eax
    invoke SetFilePointer, hFile,12,NULL,FILE_BEGIN
    invoke ReadFile,hFile, ADDR temp_buffer,fsiz,ADDR SizeReadWrite,NULL
    invoke CloseHandle, hFile

   
    invoke MessageBox, 0 ,ADDR temp_buffer, ADDR temp_buffer, MB_OK

    invoke ExitProcess,0
   
Main endp

    END Main


i notice that you set the file pointer to 12, then try to read beyond EOF, too
it's ok - it just won't read as many bytes as you requested
Title: Re: structs
Post by: lelejau on December 13, 2010, 07:54:27 PM
well, this was a testing, I'm not using it anymore.
Title: Re: structs
Post by: dedndave on December 13, 2010, 07:56:23 PM
you might notice a couple other changes i made

1) i used the "sizeof" operator

2) entry point is Main