Hello. I'm wondering if its possible to create structs in asm like the ones we do in C++.
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.
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?
MyStructure STRUCT
MyElement1 DWORD ?
MyElement2 DWORD ?
MyStructure ENDS
equals
struct MyStructure {
int MyElement1;
int MyElement2;
};
-r
ok thanks, now, to use I do:
LOCAL myStruct:MyStructure
mov myStruct.MyElement1, 12h
Is that correct?
that looks good :U
Thanks :clap:
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
I'm getting error...
Quote
.data
MYSTRUCTURE STRUCT
Window dd ?
MYSTRUCTURE ENDS
.code
start:
LOCAL myStruc:MYSTRUCTURE
END start
I get error A2012.
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
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
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.
As Opposed to "void" type functions in c++, you need RET in asm.
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.
"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
well, this was a testing, I'm not using it anymore.
you might notice a couple other changes i made
1) i used the "sizeof" operator
2) entry point is Main