flat assembler
Message board for the users of flat assembler.

Index > Windows > "struc" and "struct"

Thread Post new topic Reply to topic

Joined: 30 Mar 2005
Posts: 129
wisepenguin 30 Apr 2005, 16:53
i read on the forum about the use of struct.

but i dont understand why "struct" is needed, when struc works.
im new to assembly so this might just be one off that struc works.


format PE GUI 4.0

include 'macro\import.inc'

section '.text' code readable executable

struc MYRECT
.left dd ?
.top dd ?
.right dd ?
.bottom dd ?


push cur
call [GetCursorPos]

push [cur.top]
push [cur.left]
push buffer_format
push msg_buffer
call [wsprintf]

push 0 ; MB_OK
push msg_title
push msg_buffer
call [MessageBox]

push 0
call [ExitProcess]


section '.data' data readable writeable
msg_buffer rb 32
msg_title db "Current cursor pos", 0
buffer_format db "left=%d top=%d", 0

section '.idata' import data readable writeable
entry start

library kernel32, 'KERNEL32.DLL', user32, 'USER32.DLL'

import kernel32, ExitProcess, 'ExitProcess'
import user32, MessageBox, 'MessageBoxA', GetCursorPos, 'GetCursorPos', wsprintf, 'wsprintfA'


the program gets the current cursor position and prints it in a message box.

i am unsure about why struc cant be used instead of "struct" macro, as i used it here to print it in the message box by using the MYRECT members.

also, win32 calls are stdcall and "wsprintf" is CDECL, is what i am doing here with the stack correct ?
or am i meant to pop some stuff after the wsprintf call ?

off to find some more info on them, and the documentation.
Post 30 Apr 2005, 16:53
View user's profile Send private message Reply with quote

Joined: 25 Aug 2004
Posts: 618
cod3b453 01 May 2005, 00:32
I think struc defines the structure but struct actually creates the structure at compile time, so I end up writing:

struc POINT
.X dd ?
.Y dd ?
struct POINT

otherwise I get a compile error
Post 01 May 2005, 00:32
View user's profile Send private message Reply with quote
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 01 May 2005, 08:21
cod3b453: you're using the old "struct" macro, the newest packages support this kind of syntax:
struct POINT
 .X dd ?
 .Y dd ?

As for the wisepenguin's question:

The "struc" directive defines the structure in a way that it defines a kind of macro, which can be then used to declare different instances of that structure.

The "struct" (in its new version) is a macro which does the same, but defines also some additional values (like the absolute field offsets and structure size) for the convenience of programmer. It was made only to make it more like the structure definitions are in other assemblers, but it's only a macro (which uses internally the "struc" definition anyway), and it's not as flexible as the "struc" directive itself. If you understand how the "struc" works (and are not biased by other assemblers' approach), it's better for you to use the "struc" and take advantage of all capabilities it gives.

In fact I personally don't recommend using the "struct" macro, even though it is used in official includes - but I've done it to satisfy the expectations of people used to structure implementations in assemblers like TASM and NASM. In my vision (based on which I have designed the "struc" mechanism for flat assembler) defining absolute structure offsets is a wrong choice and structure should be declared independently for its each appearance and usage - even when you've got dynamically allocated structure, you can do it with "virtual" directive, like:
virtual at ebx
  mystruc@ebx MYSTRUC
end virtual    

Note that "struc" directive allows definitions of structures that have variable sizes and field offsets depending on instance, and perhaps depending on some additional parameters (you can define them with all the freedom you've got with macroinstructions in fasm) - this is where the idea of absolute offsets and size completely fails.

I plan to write something more about it in the fifth section of my Design Principles article, which is not yet finished (even though officially published on this website).
Post 01 May 2005, 08:21
View user's profile Send private message Visit poster's website 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.