News:

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

A question about replacing values in structures

Started by Rainstorm, December 13, 2006, 12:13:08 PM

Previous topic - Next topic

Rainstorm

hi,

Is there anyway i could replace the value in a field, in a structure with a string/zero terminated string ?
like in the below example I've replaced 20 with 2 in tit.a. - instead if i wanted to
replace that field with say  'hello',0  -is there a way to do that ? (either in the variable definition or any other way later in the code)

    .data

  titan struct
      a   db    20
      b   dd    30
      d   dd    40
  titan ends

tit titan <2>

; -------------------------------------------------------------------------
    .code

start:

  movzx ebx, tit.a
  print "a - "
  print ustr$(ebx),13,10
 
  print "b - "
  print ustr$(tit.b),13,10
 
  mov ebx, tit.d
  print "d - "
  print ustr$(ebx),13,10

inkey
exit

end start


thanks.

PBrennick

Rainstorm,
You can store a string in a structure. An example of doing that would be the FileOpen structure. Here is an example of it being done (from windows.inc)


OFSTRUCT STRUCT
  cBytes        BYTE      ?
  fFixedDisk    BYTE      ?
  nErrCode      WORD      ?
  Reserved1     WORD      ?
  Reserved2     WORD      ?
  szPathName    BYTE  OFS_MAXPATHNAME dup(?)
OFSTRUCT ENDS

You just need to reserve enough bytes for what you want to do.
Paul

The GeneSys Project is available from:
The Repository or My crappy website


Rainstorm

hi,.. just reading the stuff pertaining to structures..in the masm programmer's guide.
it says there quote..
QuoteIf the override is shorter, the assembler pads the override with spaces to equal the length of the initializer. If the initializer is a string and the override value is not a string, the override value must be enclosed in angle brackets or curly braces

(i take it the 'override' is the value supplied in the variable definition)
so according to the above, an override that's not a string should work.. but in the below example code
it doesn't seem to work.. i get this error when compiling..
** initializer must be a string or single item ** for the variable decleration line -
tit titan <8>
what am i doing wrong ?

  .data
  titan struct
      a   db    "hello"
      b   dd    30
      d   dd    40
  titan ends

tit titan <8>    ; override that's not a String
; -------------------------------------------------------------------------
    .code
start:
  mov ebx, offset tit.a
  print "a - "
  print ebx,13,10
inkey
exit
end start


thankyou.

TNick

this is working:

tit titan <<8>>

because you use one pair of angle brackets for showing where the srtucture values are and another pair for override.

Regards,
Nick

Rainstorm

#5
tnick, thanks ..yes that works proper..guess that's what they meant by 'must be enclosed in angle brackets' - an additional pair..

just having a prob with this code..the value of tit.a comes out proper but tit.d just shows 0
  .code
start:
movzx ebx, tit.a

  print "a - "
  print ustr$(ebx),13,10
 
  print "b - "
  print ustr$(tit.b),13,10

  xor ebx, ebx
  mov ebx, tit.d
  print "d - "
  print ustr$(ebx),13,10

inkey
exit

end start

TNick

sorry, not fmiliar with those macros.. what ustr$ does? What do you try to do?
PS
there's no need for XOR ebx, ebx before MOV ebx, tit.d. What you say there is:
ebx = 0
ebx = tit.d

Nick

TNick

Ok documentation is ready. So, what you are doing there is:
mov ebx, tit.d
ebx = 40
ustr$(ebx) - unsigned integer from string. 40 (28h) wich represents character "(" can't be turned into an integer
print ... - shows 0 because - I think - if ustr$ can't turn that string in a integer returns 0.

Quick test: set tit.d to 54 (36h) and you will have on screen 6.

Nick


Rainstorm

hi,
sry i forgot to post the '.data' part in the last bit of code.
from our previouss discussion, i used the double <<>> & the field a, now displays correctly
but why is field d getting affected ? - normally ustr$(tit.d) should display 40. - it seems to be happening after i used the <<>> for field a

    .data
  titan struct
      a   db    "hello"
      b   dd    30
      d   dd    40
  titan ends

tit titan <<8>>
; -------------------------------------------------------------------------
    .code
start:
movzx ebx, tit.a
  print "a - "
  print ustr$(ebx),13,10
 
  print "b - "
  print ustr$(tit.b),13,10

  mov ebx, tit.d
  print "d - "
  print ustr$(tit.d),13,10

inkey
exit
end start


thankyou..
Rainstorm.

TNick

Hello, Rainstorm, and sorry for this late answer! I did today a short test and this is what I came out with:

Program used to test:


.586
.MODEL FLAT, STDCALL
OPTION CASEMAP:NONE

    INCLUDE windows.inc
    INCLUDE kernel32.inc
   
    INCLUDELIB kernel32.lib
   
    AStructDef    STRUCT
        AString        DB        "Some String"
        AWord        DW        02Dh
        ADWORD        DD        091h
    AStructDef ends
   
.DATA
    Marker1        DWORD        0FFFFFFFFh
    MyStruct    AStructDef    <>
    Marker2        DWORD        0FFFFFFFFh


.CODE
APP_ENTRY_POINT:
   
   
    mov        eax,        OFFSET MyStruct
    INVOKE    ExitProcess,NULL
END APP_ENTRY_POINT


I've replaced values in angle brackets and had output in debuger as follows:
Quote
##########################################################################################
##########################################################################################

.DATA
    Marker1        DWORD        0FFFFFFFFh
    MyStruct    AStructDef    <>
    Marker2        DWORD        0FFFFFFFFh



00403000  FF FF FF FF 53 6F 6D 65 20 53 74 72 69 6E 67 2D  ÿÿÿÿSome String-
00403010  00 91 00 00 00 FF FF FF FF                       .'...ÿÿÿÿ

##########################################################################################
##########################################################################################

.DATA
    Marker1        DWORD        0FFFFFFFFh
    MyStruct       AStructDef    <"a",1,2>
    Marker2        DWORD        0FFFFFFFFh



00403000  FF FF FF FF 61 20 20 20 20 20 20 20 20 20 20 01  ÿÿÿÿa          
00403010  00 02 00 00 00 FF FF FF FF                       ....ÿÿÿÿ


##########################################################################################
##########################################################################################

.DATA
    Marker1        DWORD        0FFFFFFFFh
    MyStruct       AStructDef    <<8>,1,2>
    Marker2        DWORD        0FFFFFFFFh


00403000  FF FF FF FF 08 01 00 02 00 00 00 00 00 00 00 00  ÿÿÿÿ.........
00403010  00 00 00 00 00 FF FF FF FF                       .....ÿÿÿÿ

##########################################################################################
##########################################################################################

As you can see there, when using an integer as initializer for a string member, next members are shifted right next to that member. I can't tell why is this happening and Iwould like to ask other experimented members to help us in this one.

Regards,
Nick

Rainstorm


PBrennick

TNick,
As you can see from your excellent testing, the override changes the size of the first member of the structure to represent the new specifications of the override. The structure size, however, was assigned at build time and the override is assigned at run time. For this reason, the structure size remains the same and is padded with zeroes. The program acts like the structure size has changed when it hasn't, that is why the values are moved to be one right after another (zero padded to adjust for the word and dword. The first member is byte so the 1 is right after the 8. The 1 is word sized so there is a 0 between the 1 and the 2.

hth,
Paul
The GeneSys Project is available from:
The Repository or My crappy website

TNick

Hi, Paul!
Hope your health is better!
Thanks for this answer, I understand now. :)

Best regards,
Nick

Rainstorm

hi,
so then, such a thing isn't safe to do..though its mentioned as possible in the manual.
is that right ?

thanks

PBrennick

#14
Rainstorm,
It is safe to do that though I would find it hard to justify. I only mentioned what is happening because it is always good to know what is happening. TNick's help has been exceptional and please do not think I am saying anything derogatory here! Notice how he explains everything and how he uses a debugger to see what is happening. This is ALWAYS the thing to do.

Kudos to you, TNick!  :U

EDIT: The text got a little scrambled when I copied it from the editor. I use an external editor so I can spellcheck. Sorry, it is fixed now.

Paul
The GeneSys Project is available from:
The Repository or My crappy website