12 Replies - 2197 Views - Last Post: 19 September 2012 - 07:09 PM

#1 krosty4782  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-June 11

Structures ? Win32 api parameter structure

Posted 13 September 2012 - 08:08 PM

Hello.
How can i give to a function a structure in asm.
For example:
FillConsoleOutputAttribute, asks me for:

Quote

dwWriteCoord [in]
A COORD structure that specifies the character coordinates of the first cell whose attributes are to be set.


How can i make a COORD structure ?

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Structures ? Win32 api parameter structure

#2 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: Structures ? Win32 api parameter structure

Posted 14 September 2012 - 04:36 PM

Are you learning Assembly on your own or in a course? Either way, if you want to learn it, you must learn to crawl before you walk then run. In other words, you need to do research and learn on your own. All of the structures used in the Windows API's are well documented. All of the API calls are well documented. If you do not know what a structure is, it would behoove you to read up on what they are.

There is a pinned topic in the Assembly forum: http://www.dreaminco...etting-started/ with great info.

So, a quick search of COORD structure on MSDN gave me this page: http://msdn.microsof...9(v=vs.85).aspx

typedef struct _COORD {  
    SHORT X;  
    SHORT Y;
    } COORD,  *PCOORD;


Do you know what a short is? It is a 16bit value, and in Assembly that would be a WORD.
So you could define/convert it as:
COORD STRUCT
  x  WORD      ?
  y  WORD      ?
COORD ENDS


There are a few other ways to define a structure. But that is the easiest.
Was This Post Helpful? 0
  • +
  • -

#3 krosty4782  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-June 11

Re: Structures ? Win32 api parameter structure

Posted 14 September 2012 - 05:59 PM

In university we are seeing something buy with emu, and basic code like if, loop, i know how to do that...
I also do research, like i say in university we only saw a hello world and i do some other stuffs like a clock, open and readin files, i just didn't know how to declare structs in asm. I know what a structure is, i know other languages like c, so...
I just was confuse on how to declare structs in asm, cause in an example i 've read on the net a code that says:

;132 bits for the structure

enter 132, 0

And nothing else, no variables no STRUCT, nothing, so i thought maybe in assembly is different.
Anyway, thanks i will try that.
Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: Structures ? Win32 api parameter structure

Posted 14 September 2012 - 06:07 PM

enter 132, 0

No, this sets up a stack frame and sets aside 132 bytes on the stack for "local" variables which could be a "structure", a string, bytes, bits, whatever.

It is the same as:
    push    ebp
    mov     ebp, esp
    sub     esp, 132
at the beginning of a procedure. A structure is just a fancy name for an array, nothing more and nothing less.
You could define it this way also:
Coord   dw  10, 1

and pass the address of Coord to the Api call. Everything you "know" in other languages is NOT the same in Assembly. Forget all of that, it is different down here.
Was This Post Helpful? 0
  • +
  • -

#5 krosty4782  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-June 11

Re: Structures ? Win32 api parameter structure

Posted 17 September 2012 - 03:48 PM

Thank you.
So for example if using Cord dw 10,1.
I will have x position in Coord, and y position un Coord + 2, right ?
If using first method, how do i enter a struct into a struct?
For example, i have:

mystruct struct
1 db ?
2 db ?
mystruct ends


mystruct2 struct
 1 mystruct ?
 2 mystruct ?
mystruct2 ends


Is this possible ?

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: Structures ? Win32 api parameter structure

Posted 17 September 2012 - 04:57 PM

Sure it is possible. Not sure what Assembler you are using, but I will go with MASM.
mystruct struct
a db ?
b db ?
mystruct ends

mystruct2 struct
a mystruct <>
b mystruct <>
mystruct2 ends

.data?
Ms  mystruct2 <>

.code
start:
    mov     Ms.a.a, 1
    mov     Ms.a.b, 2
    mov     Ms.b.a, 3
    mov     Ms.b.b, 4
    PrintDec Ms.a.a 
    PrintDec Ms.a.b
    PrintDec Ms.b.a 
    PrintDec Ms.b.b

Ms.a.a = 1
Ms.a.b = 2
Ms.b.a = 3
Ms.b.b = 4

One thing though, variable names cannot start with a number.
Was This Post Helpful? 0
  • +
  • -

#7 krosty4782  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-June 11

Re: Structures ? Win32 api parameter structure

Posted 17 September 2012 - 07:49 PM

OK thanks. I get the idea now. The only problem is that nasm for win32 doesn't understand "struct" it shows: "error parser: instruction expected"
I'm using nasm for win32 with alink.
Other question, what does "<>" means ?
Thanks
Was This Post Helpful? 0
  • +
  • -

#8 krosty4782  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-June 11

Re: Structures ? Win32 api parameter structure

Posted 17 September 2012 - 08:00 PM

I could fix one thing, syntax is this:

struc SMALL_RECT 
.left: RESD 1
.top RESW 1
.right RESW 1
.bottom RESW 1
endstruc


This works.

Now i have to change syntax for this>

struc CONSOLE_SCREEN_BUFFER_INFO 
dwSize COORD <>
dwCursorPosition  COORD <>
wAttributes  RESW 1
srWindow  SMALL_RECT <> 
dwMaximumWindowSize  COORD <>      
endstruc


Because it doesn't works.
Was This Post Helpful? 0
  • +
  • -

#9 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: Structures ? Win32 api parameter structure

Posted 18 September 2012 - 07:25 AM

I dont say it much, but here I will... RTFM! The folks that developed NASM spent time writing the docs.

www.nasm.us/doc/nasmdoc4.html

You should read all of the docs, but for structures look at section 4.12.10 and 4.12.11
Was This Post Helpful? 0
  • +
  • -

#10 krosty4782  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-June 11

Re: Structures ? Win32 api parameter structure

Posted 19 September 2012 - 05:40 PM

I read the documentation before asking, but i couldn't do it.
Documentation is only about defining structs with native types, not including other type inside (struc into struct).
Thanks
Was This Post Helpful? 0
  • +
  • -

#11 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: Structures ? Win32 api parameter structure

Posted 19 September 2012 - 05:49 PM

first, what have you tried? What does "I couldn't do it mean"? What code have you tried?

A structure is a high level thing really. The CPU has no understanding of a structure.
Was This Post Helpful? 0
  • +
  • -

#12 krosty4782  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-June 11

Re: Structures ? Win32 api parameter structure

Posted 19 September 2012 - 07:06 PM

I ve tried this:
struc CONSOLE_SCREEN_BUFFER_INFO 

.dwSize COORD 
.dwCursorPosition  COORD 
.wAttributes  WORD ?
.srWindow  SMALL_RECT  
.dwMaximumWindowSize  COORD       
endstruc


and this:

struc CONSOLE_SCREEN_BUFFER_INFO 

.dwSize COORD ?
.dwCursorPosition  COORD ? 
.wAttributes  WORD ?
.srWindow  SMALL_RECT  ?
.dwMaximumWindowSize  COORD ?       
endstruc


But it seems like nasm doesn't recognize COORD like a "type".
if it is a high level thing, how am i suppoused to send a struct as parameter for my win api function?
Was This Post Helpful? 0
  • +
  • -

#13 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: Structures ? Win32 api parameter structure

Posted 19 September 2012 - 07:09 PM

So, try creating a COORD structure as you did for CONSOLE_SCREEN_BUFFER_INFO.

Maybe you should use NASMX instead of NASM as it has almost all of the structures defined plus a few more useful macros.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1