assembly68000retro-computingamiga

How do you define an enumeration in 68000 assembly language?


I'm using the assembler that came with the Manx Aztec C compiler (version 5.0) on a Commodore Amiga 500.

I want to code the equivalent of the following C code:

enum STATUS {
    STATUS_OKAY,
    STATUS_WAITING,
    STATUS_ERROR
};

I tried the following—which works—but it seems kind of hokey:

s_id            set 0
STATUS_OKAY     equ s_id
s_id            set s_id+1
STATUS_WAITING  equ s_id
s_id            equ s_id+1
STATUS_ERROR    equ s_id

I know I could do:

STATUS_OKAY    equ 0
STATUS_WAITING equ 1
STATUS_ERROR   equ 2

But I would like to be able to insert and rearrange values without having to manually renumber.

I was thinking I might be able to do something with macros, but I don't have much experience with them.


Solution

  • I'd guess you'd want to write a macro that you can use like AUTONUMBER s_id STATUS_OKAY that takes two args: 2nd is the symbol name to define, 1st is the counter to increment.

    You'd want it to expand to something like:

    STATUS_OKAY     equ s_id
    s_id            set s_id+1    # post-incr so it uses the initial value of s_id
    

    (I don't know that assembler or its syntax for defining macros; I assume that's possible though.)

    Some assemblers have a special macro directive for redefining a preprocessor constant, allowing you to increment. e.g. NASM wouldn't work with foo equ foo+1, you'd need %define. You used s_id set s_id+1 in most of your lines, but equ in the last one, so that's probably just a typo.


    So full usage would look like:

    s_id  set 0       # starting value
    AUTONUMBER s_id  STATUS_OKAY
    AUTONUMBER s_id  STATUS_WAITING
    AUTONUMBER s_id  STATUS_ERROR
    

    I put the counter name first because it's the same length every time (and short). Putting it 2nd required more indenting to avoid ragged columns. And it puts the unique part of the line at one end, where it's visually more obvious.