flat assembler
Message board for the users of flat assembler.

Index > Main > struct ---> struc

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji

I try to learn to use the directive "struc" (not "struct").
How to convert this structure using only "struc" ? Embarassed
This would allow me to understand more about the use of "struc".
Thank you all.
Code:
struct      @Header
   NEXT                      dd ?
   union
      Data_FullScan dw ?
      struct
         IDLE_CODE      db ?
         NUMB_LABEL     dw ?    
         ADDR_LLT   dd ?
         SCAN_SP        dd ?
         NESTING        dw ?
      ends
   ends
   PREV_FLAG           dw ?    
ends
    

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 08 Oct 2010, 15:34
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7797
Location: Kraków, Poland
Tomasz Grysztar
To account for all the key features of "struct" it would have to look like this:
Code:
struc @Header next,data_fullscan,prev_flag
{
  .NEXT dd next+0
  .DataFullScan dw data_fullscan+0
  virtual at .DataFullScan
    .IDLE_CODE      db ?
    .NUMB_LABEL     dw ?
    .ADDR_LLT       dd ?
    .SCAN_SP        dd ?
    .NESTING        dw ?
    .substr_end = $-$$
  end virtual
  if .substr_end>$-.DataFullScan
   rb .substr_end-($-.DataFullScan)
  end if
  .PREV_FLAG dw prev_flag+0
}

virtual at 0
 @Header @Header
 sizeof.@Header = $
end virtual    
"struct" also defines "sizeof." for every field, but I didn't include that in "struc" implementation - not hard to add it, but would make it even more unreadable.
Post 08 Oct 2010, 16:32
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji

thank you very much Tomasz.
it works fine.

the full_struc for me is :
Code:
;WINK_SEGMENT_HEADER_LENGTH
struc @Header next,prev_addr,nb_char,reserved_,data_fullscan,\
              prev_flag,free_db,free_dd
{
  .NEXT          dd next+0
  .PREV_ADDR     dd prev_addr+0
  .NB_CHAR       dd nb_char+0
  .RESERVED_     dd reserved_+0
  .Data_FullScan dw data_fullscan+0
  virtual at .Data_FullScan
    .IDLE_CODE      db ?
    .PROC_LABEL     dw ?
    .ENDP__         dw ?
    .NUMB_LABEL     dw ?
    .ADDR_LLT       dd ?
    .SCAN_SP        dd ?
    .NESTING        dw ?
    .INDEX_PROC     dd ?
    .FREE___        dd ?
    .substr_end = $-$$
  end virtual
  if .substr_end>$-.Data_FullScan
   rb .substr_end-($-.Data_FullScan)
  end if
  .PREV_FLAG     dw prev_flag+0
  .FREE_DB       db free_db+0
  .FREE_DD       dd free_dd+0
}

virtual at 0
 @Header @Header
 sizeof.@Header = $
end virtual
    

but :

A) why "+0" ??

B) Why is it necessary to add this:
Code:
virtual at 0
 @Header @Header
 sizeof.@Header = $
end virtual
    

To be allowed to do, for example : mov eax,[eax+ @Header.Data_FullScan]
Why my code does not compile without that ?

C)
why this :
Code:
  if .substr_end>$-.Data_FullScan
   rb .substr_end-($-.Data_FullScan)
  end if
    

and not only this : " rb .substr_end " and nothing else.

EDIT: (about "C")

ok,i understood !!! Razz
IF "virtual/end virtual" > .Data_FullScan dw THEN rb x

_________________
I am not young enough to know everything (Oscar Wilde)- Image


Last edited by ouadji on 08 Oct 2010, 19:49; edited 1 time in total
Post 08 Oct 2010, 19:30
View user's profile Send private message Send e-mail Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
ouadji,

A) dw prev_flag+0 is a cute shortcut for
Code:
if prev_flag eq
  dw 0
else
  dw prev_flag
end if    
B) Another way to define sizeof.struct is to sum sizeofs of its fields, but with regard of alignment and nested structs / unions this task might be quite a challenge; much easier is to define an instance and measure its size.
Post 08 Oct 2010, 19:48
View user's profile Send private message Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji

Code:
if prev_flag eq
  dw 0
else
  dw prev_flag
end i
    

thank you baldr, but i don't understand. Embarassed
Code:
if prev_flag eq ;(nothing i guess)
.PREV_FLAG     dw 0
else
.PREV_FLAG     dw prev_flag
    

wat's the difference between "dw 0" and " dw prev_flag"
(in the case of this 'struct')


_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 08 Oct 2010, 19:58
View user's profile Send private message Send e-mail Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
ouadji,

With this trick empty argument means 0 (+0, to be exact Wink).
Post 08 Oct 2010, 20:18
View user's profile Send private message Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji

ok.

YY dd X±y

if X = no_thing
then X = no_thing ± y = ±y (here '+y' with y=0)

else X = y ± the_thing Smile

I guess that "?" doesn't exist with "struc" (like with "struct")

ok, this allows to initialize each field ...
in the absence of direct initialization


_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 08 Oct 2010, 20:32
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7797
Location: Kraków, Poland
Tomasz Grysztar
baldr wrote:
B) Another way to define sizeof.struct is to sum sizeofs of its fields, but with regard of alignment and nested structs / unions this task might be quite a challenge; much easier is to define an instance and measure its size.
This virtual block is not only for purpose of defining sizeof.@Header, but also offsets like @Header.NEXT.

ouadji wrote:
I guess that "?" doesn't exist with "struc"
Oh, you're right, I completely forgot about this. So instead of the ".NEXT dd next+0" you should really have something like:
Code:
  if next in <,?>
   .NEXT dd ?
  else
   .NEXT dd next
  end if    
(or just simple "if next eq") and analogously for the other fields. Only then you really have struct's behavior simulated properly.
Post 08 Oct 2010, 21:29
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
ouadji,

That was (IIRC) trick from the pre-eq era, remnants can be seen in INCLUDE\MACRO\RESOURCE.INC still. As you may expect, «-0» works the same way (since +/- have lowest precedence). To detect (and use «?» instead of) empty macro argument, eq operator had to be devised.

struc-macro is a macro of special behavior, in all other aspects it is exactly macro. So are instant macros like rept, irp or even so powerful match. Wink

Anyway, you may preprocess your source and see what develops.

----8<----
Tomasz Grysztar,

Those are related, aren't they? I was emphasizing on the most apparent effect. Thanks for correction, though.
Post 08 Oct 2010, 21:45
View user's profile Send private message Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji
Code:
struc @Header ;nothing else here
{
      .NEXT            dd ?
      .PREV_ADDR       dd ?
      .NB_CHAR         dd ?

      ; "?" for all.
    

(thank you Tomasz/baldr for your reply)
indeed, "?" exists with "struc" like with "struct".
this above does compile without any problem.

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 08 Oct 2010, 21:45
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7797
Location: Kraków, Poland
Tomasz Grysztar
baldr wrote:
That was (IIRC) trick from the pre-eq era, remnants can be seen in INCLUDE\MACRO\RESOURCE.INC still.
There never was a "pre-eq" era (such operator was present since the earliest versions of fasm, initially called "equ" - the same as preprocessor directive, then in 1.14 it was renamed to "eq" to avoid conflicts), this trick was just used to write in one line what with "if" would need more.

baldr wrote:
Those are related, aren't they? I was emphasizing on the most apparent effect. Thanks for correction, though.
Actually, those offsets are more important, they are crucial as long as you don't use the "assume" macro. In fact at first I was just using such "virtual" block to define those offsets, not sizeof.
Post 08 Oct 2010, 22:08
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji
Quote:

In fact at first,
I was just using such "virtual" block to define those offsets, not sizeof.
it's the most important for me, i only need offsets.

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 08 Oct 2010, 22:18
View user's profile Send private message Send e-mail Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3050
Location: vpcmipstrm
bitRAKE
I like to use STRUC with VIRTUAL block instead of offsets because a change in base register results in a single place to change code. Also, the code seems more appealing to me. Of course, the use of offsets is more literal - matching the addressing being used.

Oh, notice the rbp+rcx*.tmp.. Twisted Evil (I'm not evil, really.)
Code:
struc Temp {
  .a db ?
  .b db ?
  .. = $ - .a
}
virtual at rbp+rcx*.tmp..
  .tmp Temp
end virtual

mov al,[.tmp.a]
sub al,[.tmp.b]    
Post 09 Oct 2010, 04:47
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji
Code:
mov al,[rbp+(rcx*2)]
sub al,[rbp+(rcx*2)+1]
    

I personally don't like this approach.
This is powerful, I agree with you, but it removes any possibility of understanding the code directly by reading it. for me it's rather a way of complicating the code rather than simplified it.
Quote:
Also, the code seems more appealing to me.
yes, I know you like macros Razz
macros addict ? Wink

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 09 Oct 2010, 10:32
View user's profile Send private message Send e-mail Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3050
Location: vpcmipstrm
bitRAKE
Quote:
it removes any possibility of understanding the code directly by reading it.
Thank you. I was beginning to think the technique was completely useless. At least now I know it's the ultimate form of obfuscation. Very Happy

So, you'd rather look at a long string of instructions with a small variation? Do you stare at the sidewalk as you stroll through the park? I'm not the one using PROC/ENDP/STRUCT - I like the FASM language and use it - rather than an adapter layer for those familiar with another assembler. Seems we are coders of a similar nature really.
Post 12 Oct 2010, 05:47
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji

I live in the woods, no cars, no traffic here,
... and no sidewalks ! it's paradise here !

_________________
I am not young enough to know everything (Oscar Wilde)- Image


Last edited by ouadji on 12 Oct 2010, 12:23; edited 5 times in total
Post 12 Oct 2010, 09:57
View user's profile Send private message Send e-mail Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
ouadji wrote:
Code:
mov al,[rbp+(rcx*2)]
sub al,[rbp+(rcx*2)+1]
    

[color=green][size=14]I personally don't like this approach.
This is powerful, I agree with you, but it removes any possibility of understanding the code directly by reading it. for me it's rather a way of complicating the code rather than simplified it.


i dont find it hard to understand.
it takes the byte at ebp +ecx*2, and sub the byte at ebp+ecx*2+1.
it is not very hard to understand...
Post 12 Oct 2010, 11:29
View user's profile Send private message Visit poster's website Reply with quote
3200th



Joined: 23 Nov 2008
Posts: 84
Location: perfect match
3200th
edfed wrote:
i dont find it hard to understand.
Remember that you are much younger than ouadji!

Wink
Post 12 Oct 2010, 11:59
View user's profile Send private message Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji
Quote:

Remember that you are much younger than ouadji!
Evil or Very Mad
how old am i ? Wink

that said, it's true that this :
Code:
struc Temp {
  .a db ?
  .b db ?
  .. = $ - .a
}
virtual at rbp+rcx*.tmp..
  .tmp Temp
end virtual

mov al,[.tmp.a]
sub al,[.tmp.b]
    
is much easier to understand that this:
Code:
mov al,[rbp+(rcx*2)]
sub al,[rbp+(rcx*2)+1] 
    
Razz

_________________
I am not young enough to know everything (Oscar Wilde)- Image
Post 12 Oct 2010, 12:18
View user's profile Send private message Send e-mail Reply with quote
3200th



Joined: 23 Nov 2008
Posts: 84
Location: perfect match
3200th
ouadji wrote:
how old am i ?
Note that edfed is in his early twenties. I bet you are way older! Evil or Very Mad Wink
Post 12 Oct 2010, 13:46
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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 GitHub, YouTube, Twitter.

Website powered by rwasa.