flat assembler
Message board for the users of flat assembler.

Index > Main > best way to fill structures

Author
Thread Post new topic Reply to topic
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox 01 May 2008, 11:27
how do you fill structures?
thre are many ways to do that, lest say its sockaddr_in and wndclass

first one has only 16 bytes, so i use stack, and after that:


mov dword [ebp],0x50000002 ;AF_INET, port 80
mov dword [ebp+4],eax ;hers ip4 addr
xor eax,eax
mov dword [ebp+8],eax ;0
mov dword [ebp+12],eax ;0

second, wndclass is much bigger.
if i set 2 or 3 fileds, i do in same way as sockaddr_in, what if i would have to set more fileds? Id like to know how you do it, maybe there are better ways (i heard its possible to initilize stack with 0's).

and how do i use 64bit instructions? mov qword [ebp+8],0
or 64bit registers (rax, rbx, ...)?
Post 01 May 2008, 11:27
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 01 May 2008, 11:32
Many different ways. If you need a 'default' structure, it's better if you keep it a global pre-initialized one. In this way you won't need any instructions to initialize it because it'll be initialized. It's (usually IMO) not a good idea to keep structures on the stack.

If it's very big you can initialize it with 0s by using the string instructions (rep & all those).

it all depends on what you want from the app. However I recommend a global structure but don't take my word for it.

or did I misunderstand you?
Post 01 May 2008, 11:32
View user's profile Send private message Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox 01 May 2008, 11:35
if i dynamicly load memory, i always use heap. On stack i use static elements, like global structures.

rep? never heard, ill read about it.
Post 01 May 2008, 11:35
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 01 May 2008, 11:55
i thought the procedure you call required a pointer to this structure, so a global one would have been fine.

here's a short description of the string instructions i was referring to:

Quote:
REP - Repeat String Operation

Usage: REP
Modifies flags: None

Repeats execution of string instructions while CX != 0. After
each string operation, CX is decremented and the Zero Flag is
tested. The combination of a repeat prefix and a segment override
on CPU's before the 386 may result in errors if an interrupt occurs
before CX=0. The following code shows code that is susceptible to
this and how to avoid it:

again: rep movs byte ptr ES:[DI],ES:[SI] ; vulnerable instr.
jcxz next ; continue if REP successful
loop again ; interrupt goofed count
next:


You can then use the string instructions (e.g STOS) to set the bytes to 0.

Quote:
STOS - Store String (Byte, Word or Doubleword)

Usage: STOS dest
STOSB
STOSW
STOSD
Modifies flags: None

Stores value in accumulator to location at ES:(E)DI (even if operand
is given). (E)DI is incremented/decremented based on the size of
the operand (or instruction format) and the state of the Direction
Flag. Use with REP prefixes.


Something like this:

Code:
mov eax, 0
mov ecx, SIZE_STRUCT / 4  ; size of structure divided by 4
                          ; because we write 4 bytes at a time

mov edi, STRUCT_OFFSET  ; pointer to struct start

rep stosd    


I just made up this code, dunno if there are typos or something
of course in an actual code you should use "xor eax, eax" instead of "mov eax, 0" (sorry tom) but this was only an example
Post 01 May 2008, 11:55
View user's profile Send private message Reply with quote
Mac2004



Joined: 15 Dec 2003
Posts: 314
Mac2004 02 May 2008, 06:14
I use virtual structures to access structures through registers like this:

Code:
struc FRAME_STRUC
   {
        .x                    dd 0 ;start x-coordinate
        .y          dd 0 ;start y-coordinate
    .x_length       dd 0 ;x length
      .y_length       dd 0 ;y length
      .color          dd 0 ;color
 };end of structure

virtual at 0
         local_frame FRAME_STRUC ; define a local virtual instance
end virtual

        frame1 FRAME_STRUC ; Define a test sructure
     


And this is how I access the structure:

Code:
mov esi,frame1

mov dword[esi+local_frame.x],200
mov dword[esi+local_frame.y],300
;.....
     


regards,
Mac2004
Post 02 May 2008, 06:14
View user's profile Send private message Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox 02 May 2008, 09:57
structures in file? interesting Wink
Post 02 May 2008, 09:57
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.