flat assembler
Message board for the users of flat assembler.

Index > Windows > newbie question about stack

Author
Thread Post new topic Reply to topic
kot3245



Joined: 19 Mar 2007
Posts: 4
Location: Russia. Moscow
kot3245
Prompt please as correctly in procedure to allocate memory on a stack for structure and correctly to get access to its elements
Structure like this:
struct SP_DEVICE_INTERFACE_DETAIL_DATA
cbSize dd ?
DevicePath db ?
ends

I have a size of structure


Last edited by kot3245 on 23 Jun 2011, 16:51; edited 2 times in total
Post 23 Jun 2011, 15:55
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
Hi kot3245, welcome to the forum.
I asked similiar question once...
http://board.flatassembler.net/topic.php?p=92372#92372
Just allocate as much as needed in your case.
Post 23 Jun 2011, 16:06
View user's profile Send private message Reply with quote
kot3245



Joined: 19 Mar 2007
Posts: 4
Location: Russia. Moscow
kot3245
Thanks a lot for your answer. I hope that it will help me.
Post 23 Jun 2011, 16:31
View user's profile Send private message Reply with quote
kot3245



Joined: 19 Mar 2007
Posts: 4
Location: Russia. Moscow
kot3245
Structure definition are dummy. The second member of structure can have variable length. Memory for structure should be allocated dynamically. Whether the question is required stack alignment on 4 bytes or not?
Post 23 Jun 2011, 23:04
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
You need to adjust the stack pointer to 4 bytes (or 8 bytes for 64-bit code), to avoid troubles (not to improve performance only).

One chance here would be defining DevicePath with an explicit size, the other would be using LocalAlloc API (or whatever). If you still need this on stack then, assuming you will going to use this with SetupDiGetDeviceInterfaceDetail, then (for 32-bit code):
Code:
proc playWithDeviceInfo, hDev, pDeviceInterfaceData, 
local reqSize:DWORD
  push ebx

  invoke SetupDiGetDeviceInterfaceDetail, [hDev], [pDeviceInterfaceData], NULL, 0, addr reqSize, NULL
  test eax, eax
  jz .fail

  sub esp, [reqSize]
  and esp, -4
  mov ebx, esp ; For convenience, since ESP changes at every PUSH (something that occurs when invoke passes parameters)
  
; This is needed only if the information could be several pages long to force allocation of the guard pages
  mov eax, ebp
.touch: 
  test byte [eax], 0
  sub eax, 4096
  cmp esp, eax
  jb  .touch


; Now time to get the data!
  mov eax, [reqSize]
  mov [ebx+PSP_DEVICE_INTERFACE_DETAIL_DATA.cbSize], eax
  invoke SetupDiGetDeviceInterfaceDetail, [hDev], [pDeviceInterfaceData], ebx, [reqSize], NULL, NULL
  test eax, eax
  jz .fail

; Example of using the data
  invoke MessageBox, 0, addr ebx+PSP_DEVICE_INTERFACE_DETAIL_DATA.DevicePath, .caption, 0

  pop ebx
.fail: ; I do nothing on error but just getting out of here
  ret

.caption db "Device Path", 0 ; Use "du" instead of "db" if you are using Unicode APIs (i.e. win32w*.inc or MesageBoxW).
endp    
PS: Note that I haven't tested the code, it is just meant to be a guide. Also, remember that the official definition of the structure uses TCHAR, not db, so if you use the unicode api it should be either "dw" or "du" (or just use TCHAR which is defined by win32*.inc).
Post 24 Jun 2011, 00:26
View user's profile Send private message Reply with quote
kot3245



Joined: 19 Mar 2007
Posts: 4
Location: Russia. Moscow
kot3245
Thanks for an example. I will try to understand a code.


Code:
....
  sub esp, [reqSize]
  and esp, -4
  mov ebx, esp ;
....
    

and esp, -4 - This alignment of a stack, isn't it?
Post 24 Jun 2011, 08:10
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1413
Location: Toronto, Canada
AsmGuru62
Yes.
Post 24 Jun 2011, 12:58
View user's profile Send private message Send e-mail 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.