flat assembler
Message board for the users of flat assembler.

Index > Windows > Define Data-structures in allocated memory

Author
Thread Post new topic Reply to topic
pete



Joined: 20 Apr 2009
Posts: 110
pete
Hello!

In the Assembly Programming Journal #2 i read the article by Larry Hammick about optimization tricks. F.i. does he write that one should not reserve large blocks of unitialized data in the data section because it's sort of a waste when the application grows large only because of empty data reserved...

I too like tiny applications, so i wondered how i could allocate memory during runtime and arrange data-structures in this allocated memory. I'm now using HeapAlloc, which does return the address of the first byte of allocated memory, but i just can't write to this memory because of lack of knowledge about this.

Code:
    call    [GetProcessHeap]
    mov     [pheap],eax
 push    0x1000          ; 4096 Kilobytes
    push    HEAP_NO_SERIALIZE+HEAP_GENERATE_EXCEPTIONS+HEAP_ZERO_MEMORY
 push    eax             ; handle to private heap block
      call    [HeapAlloc]
 CHKERR  0
   CHKERR  STATUS_ACCESS_VIOLATION
     CHKERR  STATUS_NO_MEMORY
    mov     [aheap],eax
    


CHKERR is a macro that simply checks if eax is equivalent to the parameter and if yes, raises an error an exits the application.

Please tell me how i can put data-structures in the allocated memory, like this:

Code:
      struct1         equ     [aheap]
     struct1.member1 equ     [aheap]
     struct1.member2 equ     [aheap+4]       ; dword
     struct1.member3 equ     [aheap+8]       ; dword
     textbuffer      equ     [aheap+12]
  textbuffer2     equ     [aheap+62]
  ...
    


... and how i will be able to access those structures and buffer, like this:
Code:
      mov     [struct1.member1],0xA
    


Thank you in advance for any help!


Last edited by pete on 20 Apr 2009, 11:48; edited 2 times in total
Post 20 Apr 2009, 07:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17249
Location: In your JS exploiting you and your system
revolution
See the manual, it may help you.2.3.4 Structures
Post 20 Apr 2009, 10:45
View user's profile Send private message Visit poster's website Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete
I know how to define structures in the data section, but i want to define them in allocated memory!

To describe my problem more precisely: about 10*50 Byte buffers are needed, IF a certain case occures (i.e.: user presses button); for this case it's probably cleaner to Heap-allocate some memory instead of using the stack, right? So what i want to do is to allocate ONCE all 500 Bytes, and then "split" the allocated memory into 10 buffers that i can access with names.

I've read the chapter 2.3.4. but haven't found an answer.
Post 20 Apr 2009, 11:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17249
Location: In your JS exploiting you and your system
revolution
Code:
struc struct1 {
 .member1 dd ?
}

virtual at 0
struct1 struct1
end virtual

...

mov eax,[ebx+struct1.member1]    


Last edited by revolution on 20 Apr 2009, 15:56; edited 1 time in total
Post 20 Apr 2009, 11:30
View user's profile Send private message Visit poster's website Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete
Aren't the first three lines supposed to be inside the data section? If yes, then i would allocate the needed memory twice?
Why "virtual at 0"?
Sorry, i don't get it.... Could you please explain if time permits?
Post 20 Apr 2009, 11:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17249
Location: In your JS exploiting you and your system
revolution
The first three lines can be anywhere above the first use of the struct1 structure.

If you want your structure zero-based then "at 0" is what you put. If you want it eax-based the "at eax" is what you put.
Code:
virtual at esi
struct1 struct1
end virtual

...

mov eax,[struct1.member1] ;same as: mov eax,[esi+0]    


Last edited by revolution on 20 Apr 2009, 13:40; edited 1 time in total
Post 20 Apr 2009, 11:56
View user's profile Send private message Visit poster's website Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete
Great, thank you!!!

This is what i added after the allocation:
Code:
     virtual at eax
              tbuf1   rb      50
          tbuf2   rb      50
          ...
 end virtual
test:
    mov     word[tbuf1],0x0025
  mov     word[tbuf2],0x0025
    
Post 20 Apr 2009, 12:17
View user's profile Send private message Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete
But is something equivalent possible by using the EQU directive? Right now, i have to set eax correctly each time i want to access buffers inside my virtual space.

The address of the first byte of the space is stored at aheap. "virtual at [aheap]" doesnt work...
Post 20 Apr 2009, 12:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17249
Location: In your JS exploiting you and your system
revolution
The assembler cannot do magic for you. You will have to load at least one register first before you can access the memory pointed by the aheap variable. The limitation is you would need to do two lookups in one instruction.
Code:
mov [[aheap]+offset],someValue    
The x86 CPU can't do that.
Post 20 Apr 2009, 13:39
View user's profile Send private message Visit poster's website Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete
Thanks revolution, i see now. Since eax is dynamic it's impossible to "fix" it's value when compiling the application.
Post 20 Apr 2009, 13:55
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
In case it wasn't clear, in the first revolution's suggestion of "virtual at 0" you would normally do something like this:
Code:
struct Point {
  .x dd ?
  .y dd ?
}
virtual at 0
  Point Point
end virtual
.
.
.
; EBX = Holds a pointer to the structure
mov eax, [ebx+Point.y] ; mov eax, [ebx+4]    
Post 20 Apr 2009, 15:48
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17249
Location: In your JS exploiting you and your system
revolution
LocoDelAssembly: Yes, thanks. Just to avoid confusion I have corrected the offending code above.
Post 20 Apr 2009, 15:57
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Quote:

CHKERR STATUS_ACCESS_VIOLATION
CHKERR STATUS_NO_MEMORY

Those are incorrect - heap exceptions are exceptions, they don't give you return values. Check out SEH (Structured Exception Handling) Smile
Post 20 Apr 2009, 16:26
View user's profile Send private message Visit poster's website Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete
Since i do a lot of scripting, i wasn't aware that eax couldn't get fixed at compilation time... But instead of virtual i'm now using simple equ, since i can't figure out an positive aspect to use virtual over equ; here's the part of my program:
Code:
        tbuf1   equ eax
     tbuf2   equ eax+0x32
        mov     word[tbuf1],0x0025
  mov     word[tbuf2],0x0025
    

Works exactly for me as when using virtual.

Thanks for pointing that out (SEH), f0dder!
Post 21 Apr 2009, 06:55
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Quote:
But instead of virtual i'm now using simple equ, since i can't figure out an positive aspect to use virtual over equ
Cleaner and easier to maintain code, perhaps? Smile
Post 21 Apr 2009, 08:15
View user's profile Send private message Visit poster's website Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete
I havn't had troubles with equ yet.
Post 21 Apr 2009, 08:39
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-2020, Tomasz Grysztar.

Powered by rwasa.