News:

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

Initialising a Structure

Started by AeroASM, June 23, 2005, 03:57:11 PM

Previous topic - Next topic

AeroASM

1. THis code does not work with ml /AT /c /omf

.686
.model tiny,stdcall
option casemap:none

include \masm32\include\windows.inc

.code

start:

DosHeader IMAGE_DOS_HEADER <5A4Dh,90h,3,0,4,0,0FFFFh,0,0B8h,0,0,0,40h,0,0,0,0,0,0C0h>

end start


2. Is there a way to initialise structures without putting everything inside the angle brackets?

Jeff

in the definition of the structure, it requires default values.
i.e.
IMAGE_DOS_HANDLER STRUCT
    field1 DWORD ?
    field2 DWORD ?
    field3 DWORD ?
    field4 DWORD ?
    ;...
IMAGE_DOS_HANDLER ENDS


so if in the definition you had other values (values you most commonly use), you could skip that field in the initialization.  say for instance you had the first 2 fields with 2 values and the rest uninitialized:

IMAGE_DOS_HANDLER STRUCT
    field1 DWORD 5A4Dh
    field2 DWORD 90h
    field3 DWORD ?
    field4 DWORD ?
    ;...
IMAGE_DOS_HANDLER ENDS

DosHeader IMAGE_DOS_HEADER <,,3,0,4,0,0FFFFh,0,0B8h,0,0,0,40h,0,0,0,0,0,0C0h>
if in the definition you had all the other fields initialized, you could skip the manual initialization entirely.

AeroASM

However that does not solve my problem as I would prefer to use the structure in windows.inc which currently is filled with "?"s

MichaelW

At least for ML 6.14, /omf is not a valid option.

The /AT option is not necessary with .MODEL tiny. "It only verifies that there are not base or pointer fixups, and sends /TINY to the linker."

The dword in the structure requires a .386 or higher processor directive.

Assuming the reserved members do not need to be zeroed, the structure variable can be initialized with <5A4Dh,90h,3,0,4,0,0FFFFh,0,0B8h,0,0,0,40h,0,,0,0,,0C0h>. Alternatively, you can initialize the members individually.

This is the first I have seen of the IMAGE_DOS_HEADER structure. I don't understand why Microsoft renamed the EXEHEADER structure members. The names in the original documentation made more sense.

EXEHEADER struct
   exSignature    dw 5A4Dh  ;.EXE signature
   exExtraBytes   dw ?      ;number of byte in last (partial) page
   exPages        dw ?      ;number of whole and part pages in file
   exRelocItems   dw ?      ;number of pointers in relocation table
   exHeaderSize   dw ?      ;size of header, in paragraphs
   exMinAlloc     dw ?      ;minimum allocation
   exMaxAlloc     dw ?      ;maximum allocation
   exInitSS       dw ?      ;initial ss value
   exInitSP       dw ?      ;initial sp value
   exCheckSum     dw ?      ;complemented checksum
   exInitIP       dw ?      ;initial ip value
   exInitCS       dw ?      ;initial cs value
   exRelocTable   dw ?      ;byte offset to relocation table
   exOverlay      dw ?      ;overlay number
EXEHEADER ends

eschew obfuscation

AeroASM

Quote from: MichaelW on June 23, 2005, 05:09:34 PM
At least for ML 6.14, /omf is not a valid option.

I have ml 7.00.

Quote from: MichaelW on June 23, 2005, 05:09:34 PM
The /AT option is not necessary with .MODEL tiny. "It only verifies that there are not base or pointer fixups, and sends /TINY to the linker."

Never knew that, thanks.

Quote from: MichaelW on June 23, 2005, 05:09:34 PM
The dword in the structure requires a .386 or higher processor directive.

If you look at my post, you will see that I put .686  :U

Quote from: MichaelW on June 23, 2005, 05:09:34 PM
Assuming the reserved members do not need to be zeroed, the structure variable can be initialized with <5A4Dh,90h,3,0,4,0,0FFFFh,0,0B8h,0,0,0,40h,0,,0,0,,0C0h>. Alternatively, you can initialize the members individually.

Thanks, leaving out the reserved members solved the problem. I think they caused a problem because in windows.inc they are delcared as e_res 4 dup(?). and e_res2 10 dup(?). How can I initialise members of a structure declared with dup?

Rifleman

Aero,
How about...

    e_res
    e_res+1
    e_res+2
    e_res+3

Paul

AeroASM

Paul:
What do you mean?

In general:
I have another problem. I need to initialise a fixed length string inside the struct, but MASM won't let me do it.

MichaelW

Aero,

I missed the .686 because it was scrolled up out of sight, and I was not expecting it to precede the model directive. Did you intend to make USE32 the default segment word size? I ask because this is a non-obvious effect of placing the processor directive above the model directive.

I think both of these methods will produce the same results:

mov DosHeader.e_res+0, 1     ;WORD 4 dup(?)
mov DosHeader.e_res+2, 2
mov DosHeader.e_res+4, 3
mov DosHeader.e_res+6, 4

<5A4Dh,90h,3,0,4,0,0FFFFh,0,0B8h,0,0,0,40h,0,<1,2,3,4>,0,0,,0C0h>

eschew obfuscation

Rifleman

Aero,
Michael has shown what I was trying to say.  My mistake was stepping as byte instead of word.  Getting old, I guess.

Paul

AeroASM

Why does it matter whether the processor directive is before or after the model directive? In all code I have seen it is before.
Why can't MASM switch between processor modes within a single module?
What is USE32?
That <1,2,3,4> solves my problem, but can I use it with a string:

MyStruct struct
...
TheString 8 dup(?)
...
MyStruct ends

TheVar MyStruct <...,<"hello",0,0,0>,...>

MichaelW

See "Setting Segment Word Sizes (80386/486 Only)" here:

http://webster.cs.ucr.edu/Page_TechDocs/MASMDoc/ProgrammersGuide/Chap_02.htm

See the generated listing files for more information.

.model small
.486
.stack
.data
    junk dw 0
.code
start:
    mov   eax,1
    mov   ax,1
    mov   ax,OFFSET junk
    jmp   @F
    org   $ + 0ff00h
  @@:
    mov   ah,10h
    int   16h
    mov   ax,4c00h
    int   21h
end start


.486
.model small
.stack
.data
    junk dw 0
.code
start:
    mov   eax,1
    mov   ax, 1
    ;error A2022: instruction operands must be the same size
    ;mov   ax,OFFSET junk
    mov   eax,OFFSET junk
    jmp   @F
    org   $ + 0ffffh
  @@:
    mov   ah,10h
    int   16h
    mov   ax,4c00h
    int   21h
end start


ML /c /Fl /Sa use16.asm
pause
LINK16 use16.obj;
pause
ML /c /Fl /Sa use32.asm
pause
LINK16 use32.obj;
pause

eschew obfuscation