I'm trying to create a demo program demonstrating struct(s) in MASM ,

I've written a code like this :

struct1 struct
first db ?
second dw ?
struct1 EndS

.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc ; holds predifned structures

include \masm32\include\kernel32.inc 
include \masm32\include\user32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib

MessageTitle  db "The title",0
MessageText   db "The first program which shows simple messagebox",0


Initializedstructure struct1 <'A',1024>
;invoke MessageBox, NULL, addr MessageText, addr MessageTitle, MB_OK
mov eax, struct1.first 
;invoke ExitProcess, NULL
end start

but when I disassembled the program I found some kind of instructions that not sensible for initializing the structure of the program :

.text:00401000 start:
.text:00401000                 inc     ecx
.text:00401001                 add     [eax+edi*4], al
.text:00401001 ; ---------------------------------------------------------------------------
.text:00401004                 dd 7Fh dup(0)
.text:00401200                 dd 380h dup(?)
.text:00401200 _text           ends

Why MASM assembled the code like this ? I think I've made some mistake in the code,haven't I? I think there's no well-explained document about it ...


You need to have Initializedstructure struct1 <'A',1024> in your .data segment, not in your .code segment.

| improve this answer | |
  • I did what you said and I used mov eax , struct1.first or struct1.second but in the disassembled code they didn't reference correctly.sth like : mov eax, 0 and mov ebx, 1 , could you say what's the problem ? – user3679015 Mar 4 '15 at 19:32

Masm uses 0 and 1 as the address constants is: you told it to do so because you told it to use the offset in the structure, NOT the memory location. And indeed, the field "first" sits at offset 0 and occupies a byte. This makes the offset for "second" equal to 1. You probably wanted to access the instantiation of your structure, which you placed at the address called Initializedstructure. In this case, you would have to use

mov eax, [Initializedstructure].First

to access the field "first" of that Initializedstructure. And, by the way, should you try to access such structures using vector instructions (SSE, AVX), Masm frequently looses it altogether and you need to additionally specify the operand size, such as

vmovdqu xmm0, xmmword ptr [Initializedstructure].Some128bitField

Hope it helps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.