flat assembler
Message board for the users of flat assembler.

Index > Windows > Enum in FASM

Author
Thread Post new topic Reply to topic
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 03 Oct 2010, 16:03
How can i implement Enum method in FASM

Example code in c++



Code:

typedef enum _Data
{
data1,
data2,
data3

} Data;

    


I was thinking of just using a struct but that would involve knowing values of each specific variable in the Enum struct. (if you happen to know them then tell me)
Im trying to make this eunm : MEDIA_TYPE, for Device IO.

And again in C++ it looks like this

Code:

typedef enum _MEDIA_TYPE {
  Unknown,
  F5_1Pt2_512,
  F3_1Pt44_512,
  F3_2Pt88_512,
  F3_20Pt8_512,
  F3_720_512,
  F5_360_512,
  F5_320_512,
  F5_320_1024,
  F5_180_512,
  F5_160_512,
  RemovableMedia,
  FixedMedia,
  F3_120M_512,
  F3_640_512,
  F5_640_512,
  F5_720_512,
  F3_1Pt2_512,
  F3_1Pt23_1024,
  F5_1Pt23_1024,
  F3_128Mb_512,
  F3_230Mb_512,
  F8_256_128,
  F3_200Mb_512,
  F3_240M_512,
  F3_32M_512 
} MEDIA_TYPE;

    


I know Enum, automatically assigns values to variables it contains. But just how would i implement Enum in FASM?


Last edited by typedef on 03 Oct 2010, 16:11; edited 1 time in total
Post 03 Oct 2010, 16:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20627
Location: In your JS exploiting you and your system
revolution 03 Oct 2010, 16:08
Easy:
Code:
data1 = 1
data2 = 2
data3 = 3    
Twisted Evil

But seriously:
Code:
macro enum [stuff] {
      common
              local x
             x=0
 forward
             x=x+1
               stuff = x
}

enum data1, data2, data3

display data1+'0',data2+'0',data3+'0'    
Post 03 Oct 2010, 16:08
View user's profile Send private message Visit poster's website Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 03 Oct 2010, 16:19
Thank you so much...It worked..Love you man (No hommo) Laughing
Post 03 Oct 2010, 16:19
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 03 Oct 2010, 16:25
Wait, hold on a sec. Fasm wont let me declare it in a struct, or even a variable. Here's my code

Code:

enum MEDIA_TYPE,\
                data1,\
                data2 

struct DISK_GEOMETRY
   .MediaType MEDIA_TYPE ?

ends
    


Help ! Help !
I get an error there...How can i pass
Post 03 Oct 2010, 16:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20627
Location: In your JS exploiting you and your system
revolution 03 Oct 2010, 16:36
You can do this:
Code:
include 'win32ax.inc'

macro enum var,[stuff] {
 common
              local x
             x=0
         struc var \{. dd ?\}
    forward
             x=x+1
               stuff = x
}

enum MEDIA_TYPE,\
                data1,\
                data2 

struct DISK_GEOMETRY
   MediaType MEDIA_TYPE
ends    
Post 03 Oct 2010, 16:36
View user's profile Send private message Visit poster's website Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 03 Oct 2010, 19:33
yop


Last edited by typedef on 12 Oct 2013, 04:38; edited 1 time in total
Post 03 Oct 2010, 19:33
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 04 Oct 2010, 08:46
Code:
macro enum start,step,[items]
{
   common
        local count
        count=start
   forward
        items=count
        count=count+step
}

enum $0,1,\
     T_INT,T_VOID,T_IF,T_WHILE,T_DO,T_ELSE,T_WRITE,T_READ,T_RETURN,T_ERR,T_EOT

enum $10,1,\
     T_COMMA,T_MUL,T_LPAR,T_RPAR,T_LBRA,T_RBRA,T_PLUS,T_MINUS,T_SEMICOLON,T_LTE,T_GTE,T_NEQ,T_EQ

enum $20,1,\
     T_LT,T_GT,T_NOT,T_ASSIGN,T_NUM,T_ID,T_DIV    

_________________
This is a block of text that can be added to posts you make.
Post 04 Oct 2010, 08:46
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 04 Oct 2010, 12:43

macro,macro,macro ... Confused
Why do you want absolutely create high-level language directives in assembler ?
Why not build procedures in assembler to carry out all that !
Why this "madness" of converting all with macros ?
I use no macros, and I do everything i want, all in assembler.
(just my view, nothing else)

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



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler 04 Oct 2010, 12:50
Why would you want to enum at runtime?
Post 04 Oct 2010, 12:50
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 04 Oct 2010, 13:58

I was not referring specifically about "enum",
but about many messages about macros, and more broadly about the general interest for macros.
I don't like the using of macros, for me this way is not really the assembly language.
I think anything can be carried out without macros.
For me, the use of macros is really the opposite of a real assembly language programming.
Again, it's just my view, ... and sorry, i'm off topic (sticky opinion ? Smile )

(sorry for my bad english, i do my best, it's not easy for me)

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



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 04 Oct 2010, 18:24
ouadji wrote:

I was not referring specifically about "enum",
but about many messages about macros, and more broadly about the general interest for macros.
I don't like the using of macros, for me this way is not really the assembly language.
I think anything can be carried out without macros.
For me, the use of macros is really the opposite of a real assembly language programming.
Again, it's just my view, ... and sorry, i'm off topic (sticky opinion ? Smile )

(sorry for my bad english, i do my best, it's not easy for me)


Well, IMO, Imagine running all that code that has been created as a macro, at run time. How big would your file be?

here's an example of what i mean.

Code:
macro export dllname,[label,string]
 { common
    local module,addresses,names,ordinal,count
    count = 0
   forward
    count = count+1
   common
    dd 0,0,0,RVA module,1
    dd count,count,RVA addresses,RVA names,RVA ordinal
    addresses:
   forward
    dd RVA label
   common
    names:
   forward
    local name
    dd RVA name
   common
    ordinal: count = 0
   forward
    dw count
    count = count+1
   common
    module db dllname,0
   forward
    name db string,0
   common
    local x,y,z,str1,str2,v1,v2
    x = count shr 1
    while x > 0
     y = x
     while y < count
      z = y
      while z-x >= 0
       load v1 dword from names+z*4
       str1=($-RVA $)+v1
       load v2 dword from names+(z-x)*4
       str2=($-RVA $)+v2
       while v1 > 0
        load v1 from str1+%-1
       load v2 from str2+%-1
       if v1 <> v2
    break
      end if
       end while
       if v1<v2
    load v1 dword from names+z*4
        load v2 dword from names+(z-x)*4
    store dword v1 at names+(z-x)*4
     store dword v2 at names+z*4
 load v1 word from ordinal+z*2
       load v2 word from ordinal+(z-x)*2
   store word v1 at ordinal+(z-x)*2
    store word v2 at ordinal+z*2
       else
 break
       end if
       z = z-x
      end while
      y = y+1
     end while
     x = x shr 1
    end while }

    


Thats a code to export a function from a DLL. Imagine you were making a DLL, Kernel32.DLL, would you go writing that over and over again?

Its OK though, Its your opinion, and this is my thread Laughing

I feel you. Anyways. My problem has already been solved. Thanks to you all with your contribs.Cheers
Post 04 Oct 2010, 18:24
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4227
Location: vpcmpistri
bitRAKE 05 Oct 2010, 06:04
ouadji wrote:
Why do you want absolutely create high-level language directives in assembler ?
It's called 'Domain Specific Language'. The absolutely best thing about assembly is building up the program structure. This could be either the run-time structure; or also the assemble-time structure (with macros).

The downside is, of course, the learning curve for people reading the code -- they must first understand the local language created for the task. Ideally, the language should fit the task in a way which makes this very easy. Very Happy

IMHO, this would be a great topic for the Heap. There are purists which shun use of macros - I certainly do when it comes to optimization for execution speed. Once I get better with macros I might optimize for my own time, or with regard to the project continuity.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 05 Oct 2010, 06:04
View user's profile Send private message Visit poster's website Reply with quote
ouadji



Joined: 24 Dec 2008
Posts: 1081
Location: Belgium
ouadji 05 Oct 2010, 07:58
Quote:

There are purists which shun use of macros
I'm one of them.
Only one exception, the macro "struct", I sometimes use this macro.
This is a macro which I define as "static", it doesn't create any code,
just an easy way to access to a data set ...
otherwise, nothing else.
Quote:
A) I might optimize for my own time, or with regard to the project continuity.

B) I certainly do when it comes to optimization for execution speed.

A) 100% agree with you. (but despite that, I hate macros)

B) "optimization" for execution time

a faster code using macros ?
macro_language still faster than the assembly language ???

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



Joined: 21 Jul 2003
Posts: 4227
Location: vpcmpistri
bitRAKE 05 Oct 2010, 16:08
B) In context it is a more complete thought.
Quote:
There are purists which shun use of macros - I certainly do [shun] when it comes to optimization for execution speed.
I am strictly against macros when execution speed is the goal. There are enough abstractions for my little brain to content with. Macros which reach beyond pigeon-hole type replacements would be compiler-like and hide the important process.
Post 05 Oct 2010, 16:08
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.