The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: wossname on October 29, 2005, 06:41:08 PM

Title: (32 bit) example needed please
Post by: wossname on October 29, 2005, 06:41:08 PM
Can someone show me a simple example of a STRUCT that contains a DWORD and a null-terminated string.  Passing a pointer to that struct into a PROCedure that prints the string on the console.

all I have at the moment is this...


MyStruct STRUCT
itsAge DWORD 50
itsName BYTE 21 DUP(0)
MyStruct ENDS

...
.DATA
fred MyStruct <30, "Fred">
.CODE

...

INVOKE DisplayIt, OFFSET fred           ; the call that passed the pointer to the proc

...

DisplayIt PROC,
thing:PTR MyStruct

; ???????

ret
DisplayIt ENDP


The book I'm working through does not tell you how to find the address of fields inside the struct if it is passed in as a pointer.

Please help.
Title: Re: (32 bit) example needed please
Post by: Jeff on October 29, 2005, 07:08:26 PM
if you wanted to the address of a field in a structure:


DisplayIt PROC,
thing:PTR MyStruct

mov eax,thing ;move the address of the structure into eax
lea edx,(MyStruct ptr [eax]).itsName ;load the address of the itsName field into edx
;you have the address of your string in edx

ret
DisplayIt ENDP

hope that helps
Title: Re: (32 bit) example needed please
Post by: rags on October 30, 2005, 03:20:36 AM
This may help you:

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *
MyStruct STRUCT             ; changed name member to 24 bytes
        itsAge DWORD 50     ; to keep alignment
        itsName BYTE 24 DUP(0)
MyStruct ENDS

DisplayIt PROTO :DWORD



    .data
      Fred MyStruct <30,"Fred">         ; initialize struct

    .code

start:
    call main
    inkey
    exit

main proc

    cls
    INVOKE DisplayIt, ADDR Fred     ; pass ADDRESS of STRUCT MEMBER to Function
    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
DisplayIt PROC   StructPtr:DWORD
mov eax, StructPtr      ; EAX == ADDRESS of Fred
ASSUME eax: PTR MyStruct

lea edx,[eax].itsName   ; get the ADDRESS of Fred.itsName
print edx,13,10         ; edx == ADRESS of itsName struct member

ASSUME eax: NOTHING

ret
DisplayIt  ENDP
end start



Regards,
         Rags
Title: Re: (32 bit) example needed please
Post by: Ratch on October 30, 2005, 05:22:18 AM
wossname,
     This example covers a lot of things about STRUC's.  Ratch

[attachment deleted by admin]
Title: Re: (32 bit) example needed please
Post by: wossname on October 30, 2005, 11:33:56 AM
Cool, cheers for the help.  Got it working now.

Thanks alot  :)