News:

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

Recreating a C style Struct

Started by somecoder, May 25, 2009, 06:14:11 AM

Previous topic - Next topic

somecoder

It's possible to use the in-built struct support that comes with MASM, however, I wanted to learn how a struct works in memory and have created the following code. On the text strings like szCSDVersion it echos out the correct information. I am having problems printing the information that is numeric. It either prints a square or a long number which is wrong. If you could let me know how to print out the information that GetVersionEx returns to dwMajorVersion, dwMajorVersion that would be a great help (take it easy on me as I am very new to asm).


.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib

   

.data

  ;start of homemade struct
  dwOSVersionInfoSize         DWORD 9Ch
  dwMajorVersion         DWORD ?
  dwMinorVersion         DWORD ?
  dwBuildNumber DWORD ?
  dwPlatformId DWORD ?
  szCSDVersion BYTE  128  dup (?)
  ;end of homemade struct
   
.code

start:
        invoke GetVersionEx, OFFSET dwOSVersionInfoSize
        invoke ExitProcess,0

end start

MichaelW

This is a quick modification of your code that depends on MASM32 macros to do the numeric to string conversions, display the results, wait for a key, and call ExitProcess. All of this can be done without macros - it just requires more effort.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib

; These are required here to support the inkey macro, but
; in other code they may be required for other purposes.
include \masm32\include\msvcrt.inc
includelib \masm32\lib\msvcrt.lib

include \masm32\macros\macros.asm

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
.data
    ;start of homemade struct
    dwOSVersionInfoSize       DWORD 9Ch
    dwMajorVersion            DWORD ?
    dwMinorVersion            DWORD ?
    dwBuildNumber             DWORD ?
    dwPlatformId              DWORD ?
    szCSDVersion              BYTE  128  dup (?)
    ;end of homemade struct
.code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    invoke GetVersionEx, OFFSET dwOSVersionInfoSize
    ; The return value is non-zero for success or zero for failure.
    print str$(eax),13,10
    print uhex$(dwOSVersionInfoSize),13,10
    print str$(dwMajorVersion),13,10
    print str$(dwMinorVersion),13,10
    print str$(dwBuildNumber),13,10
    print str$(dwOSVersionInfoSize),13,10
    print ADDR szCSDVersion,13,10,13,10

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


Some non-obvious details that you should be aware of are that depending on the members of the structure  C compilers may pad the structure to control the alignment of the members, and that Windows expects the members to be aligned as the C compilers would align them. This does not affect the OSVERSIONINFO structure, but it does affect some other structures.
eschew obfuscation