News:

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

structs

Started by lelejau, December 13, 2010, 06:48:03 PM

Previous topic - Next topic

lelejau

Hello. I'm wondering if its possible to create structs in asm like the ones we do in C++.
Learning assembly :)

Vortex

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.

lelejau

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?
Learning assembly :)

redskull

MyStructure STRUCT
  MyElement1  DWORD ?
  MyElement2  DWORD ?
MyStructure ENDS


equals

struct MyStructure  {
    int MyElement1;
    int MyElement2;
};


-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

lelejau

ok thanks, now, to use I do:

LOCAL myStruct:MyStructure


mov myStruct.MyElement1, 12h

Is that correct?
Learning assembly :)

dedndave

that looks good   :U

lelejau

Learning assembly :)

drizz

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




The truth cannot be learned ... it can only be recognized.

lelejau

I'm getting error...


Quote
.data

MYSTRUCTURE STRUCT

    Window dd ?

MYSTRUCTURE ENDS


.code
   

start:

LOCAL myStruc:MYSTRUCTURE

END start

I get error A2012.
Learning assembly :)

jj2007

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

dedndave

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

lelejau

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.
Learning assembly :)

drizz

As Opposed to "void" type functions in c++, you need RET in asm.
The truth cannot be learned ... it can only be recognized.

lelejau

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.
Learning assembly :)

dedndave

"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