flat assembler
Message board for the users of flat assembler.

Index > Windows > memory alignment

Author
Thread Post new topic Reply to topic
t@2



Joined: 05 Sep 2008
Posts: 3
t@2
apologies for what might be a dumb post...

- I was wondering - what is the best/standard approach to dynamic
dynamic memory allocation using fasm & win32 - i would like to allocate one or more block aligned on 16byte boundries...

any help would be greatly appreciated...

_________________
o0MyBrainHertz0o
Post 05 Sep 2008, 14:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17350
Location: In your JS exploiting you and your system
revolution
There are three main Windows allocation functions:

HeapAlloc
LocalAlloc (same as GlobalAlloc)
VirtualAlloc

The first two have no guaranteed alignment so for your case just allocate an extra 15 bytes and zero the last four bits of the return pointer after adding 15. VirtualAlloc is always aligned to a page boundary.
Post 05 Sep 2008, 15:04
View user's profile Send private message Visit poster's website Reply with quote
t@2



Joined: 05 Sep 2008
Posts: 3
t@2
many thanks Smile - i was looking at these functions - but i didnt see
any mention of alignment...may have missed it - will give this a blast.
Post 05 Sep 2008, 15:19
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17350
Location: In your JS exploiting you and your system
revolution
Alignment is simple to implement:
Code:
... ;eax has a memory pointer
add eax,15
and eax,not 15 ;now aligned to 16
...    
Only works for power-of-two alignments of course.
Post 05 Sep 2008, 15:24
View user's profile Send private message Visit poster's website Reply with quote
t@2



Joined: 05 Sep 2008
Posts: 3
t@2
yeh thanks man - been a long time since i really worked at this level..
seems its going to take some time to readjust mentally - seeing your
reply makes me feel a little dumb :/ - still i guess its always best to
ask..the last time i really coded at this level was on my amiga..

while im here - is there a way to force stack alignment in a similar
way? - so pushed params are aligned on 16byte boundries? - heh, no doubt this is just as simple... feel free to shoot me Wink

thanks again.. matt/t@2
Post 05 Sep 2008, 18:10
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
Code:
and esp, -16    


I don't your particular context but in an EBP-based frame you could do this:
Code:
some_proc:
  push ebp
  mov  ebp, esp
  and  esp, -16

; Now you have ESP aligned to a 16-byte boundary

  leave
  ret    


[edit]Removed unneded and memory waster "sub esp, 15"[/edit]


Last edited by LocoDelAssembly on 05 Sep 2008, 19:09; edited 1 time in total
Post 05 Sep 2008, 18:51
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4240
Location: 2018
edfed
maybe
Code:
add esp,15
and esp,not 15
    

edit: Rolling Eyes yekk!
Post 05 Sep 2008, 18:54
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
You know, actually it should be "and esp, -16" alone, better I edit my post.
Post 05 Sep 2008, 19:08
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. Also on YouTube, Twitter.

Website powered by rwasa.