News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How to declare an enum?

Started by dannycoh, February 04, 2024, 05:33:29 PM

Previous topic - Next topic

dannycoh

I want to have a list of named constants.
That list is final so any const that is not in that list will fail assembling.
So, how to define an Enum in UASM?
I tried using a struct but I read it is not the correct solution.

Biterider

Hi dannycoh
There is no equivalent to an enum in assembly language, but there is a way to define constant values for the assembler using equates.
Most of the programmers use prefixes to group them together, such as:

;Constants for my procedure
MYPROC_SUCCESS  equ 0
MYPROC_FAILED   equ 1
MYPROC_DELAYED  equ 2
...

See MASM manual chapter 8.5 here
 https://www.plantation-productions.com/Webster/www.artofasm.com/DOS/pdf/ch08.pdf

Biterider

mabdelouahab

enum MYPROC_SUCCESS, \
     MYPROC_FAILED, \
     MYPROC_DELAYED
or
enum MYPROC_SUCCESS, MYPROC_FAILED, MYPROC_DELAYED
or
enum MYPROC_SUCCESS=0, MYPROC_FAILED, MYPROC_DELAYED


enum MACRO __vargs:VARARG
local val__
    val__=0
    FOR __varg,<__vargs>
        instr_  INSTR <__varg>,<=>
        IF instr_
            catstr_ SUBSTR <__varg>,instr_+1
            val__ =catstr_
            __varg
        else
            __varg EQU val__
        ENDIF
        val__ = val__ +1
    ENDM
ENDM 

jj2007

Quote from: dannycoh on February 04, 2024, 05:33:29 PMSo, how to define an Enum in UASM?
include \masm32\MasmBasic\MasmBasic.inc
  Init
  ; #common, start:member, member, member
  Enum #MYPROC_, 100:SUCCESS, FAILURE, DONTKNOW
  Print Str$("mps=%i\n", MYPROC_SUCCESS)
  Print Str$("mpf=%i\n", MYPROC_FAILURE)
  Print Str$("mpd=%i\n", MYPROC_DONTKNOW)
EndOfCode

Output:
mps=100
mpf=101
mpd=102

Works with Masm, UAsm, AsmC and JWasm.

Vortex

Hi mabdelouahab,

Nice work :thumbsup:  Here is a quick test :

include     \masm32\include\masm32rt.inc

; enum macro by mabdelouahab

; https://masm32.com/board/index.php?msg=126825

enum MACRO __vargs:VARARG

LOCAL val__

    val__=0
   
    FOR __varg,<__vargs>
   
        instr_  INSTR <__varg>,<=>

        IF instr_
       
            catstr_ SUBSTR <__varg>,instr_+1
            val__ =catstr_
            __varg
           
        ELSE
       
            __varg EQU val__
           
        ENDIF
       
        val__ = val__ +1
       
    ENDM
   
ENDM

.data

format  db 'test1 = %u',13,10
        db 'sample = %u',13,10
        db 'experiment = %u',0

.data?

buffer  db 64 dup(?)

.code

start:

    enum    test1=2,sample,experiment

    invoke  wsprintf,ADDR buffer,\
            ADDR format,test1,sample,\
            experiment

    invoke  StdOut,ADDR buffer

    invoke  ExitProcess,0

END start