Enumerations: Difference between revisions

Content added Content deleted
m (→‎Without Explicit Values: minor formatting edit)
m (→‎{{header|68000 Assembly}}: added 68000 assembly and also fixed formatting and added clarification for 6502 Assembly)
Line 25: Line 25:
Saturday equ 6</lang>
Saturday equ 6</lang>


Some assemblers have an actual <code>ENUM</code> directive, where only the 0th element needs a defined value and the rest follow sequentially. This is often used for allocating RAM locations rather than a [[C]]-style enumeration, however. A byte count is listed after the label so that the assembler knows how "big" that label is. In the example below the variable <code>OBJECT_XPOS</code> begins at $0400 and <code>OBJECT_XPOS</code> begins at $0410:
Some assemblers have an actual <code>ENUM</code> directive, where only the 0th element needs a defined value and the rest follow sequentially. This is often used for allocating RAM locations rather than a [[C]]-style enumeration, however. <code>.DSB</code> is a directive that stands for "data storage byte" and is listed after the label so that the assembler knows how big the variable is. In the example below the variable <code>OBJECT_XPOS</code> begins at $0400 and <code>OBJECT_XPOS</code> begins at $0410:
<lang 6502asm>enum $0400
<lang 6502asm>enum $0400
OBJECT_XPOS .dsb 16 ;define 16 bytes for object X position
OBJECT_XPOS .dsb 16 ;define 16 bytes for object X position
Line 35: Line 35:


<lang 6502asm>Days_Of_The_Week:
<lang 6502asm>Days_Of_The_Week:
word Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
word Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday


Sunday:
Sunday:
byte "Sunday",0
byte "Sunday",0
Monday:
Monday:
byte "Monday,0
byte "Monday",0
Tuesday:
Tuesday:
byte "Tuesday",0
byte "Tuesday",0
Wednesday:
Wednesday:
byte "Wednesday",0
byte "Wednesday",0
Thursday:
Thursday:
byte "Thursday",0
byte "Thursday",0
Friday:
Friday:
byte "Friday",0
byte "Friday",0
Saturday:
Saturday:
byte "Saturday",0
byte "Saturday",0




Line 64: Line 64:


LDA ($00),Y ;Load the "W" of Wednesday into accumulator</lang>
LDA ($00),Y ;Load the "W" of Wednesday into accumulator</lang>

=={{header|68000 Assembly}}==
{{trans|6502 Assembly}}
===With Explicit Values===
You can use labels to "name" any numeric value, whether it represents a constant or a memory location is up to the programmer. Code labels are automatically assigned a value based on what memory location they are assembled to. The syntax for labels depends on your assembler; the example below uses VASM syntax and Motorola mnemonics.

Keep in mind that these names do not exist at runtime and are just for the programmer's convenience. None of this "code" below actually takes up any space in the assembled program.
<lang 68000devpac>Sunday equ 0
Monday equ 1
Tuesday equ 2
Wednesday equ 3
Thursday equ 4
Friday equ 5
Saturday equ 6</lang>


===Without Explicit Values===
A lookup table is the most common method of enumeration of actual data in assembly. Each element of the table can be accessed by an index, and the starting index is zero. (The index may need to be adjusted for data sizes larger than 1 byte, i.e. doubled for 16-bit data and quadrupled for 32-bit data.) Unlike the above example, these values do indeed take up memory. Using this method when the above enumeration would suffice is incredibly wasteful.

Like in a [[C]]-style enumeration, Sunday would be 0, Monday 1, Tuesday 2, and so on. (Actually, Monday would be 4 and Tuesday would be 8 and so on, since these are 32-bit pointers.) It's a common practice to have the index live in RAM as a one-byte index, load it in a register, and then scale its register copy during the lookup process only. That way if multiple tables with different data sizes have a common index, the program doesn't need to remember which data type the index was last used to access.

<lang 68000devpac>Days_Of_The_Week:
DC.L Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday

Sunday:
DC.B "Sunday",0
EVEN ;conditionally aligns to a 2-byte boundary if the data isn't aligned already
Monday:
DC.B "Monday",0
EVEN
Tuesday:
DC.B "Tuesday",0
EVEN
Wednesday:
DC.B "Wednesday",0
EVEN
Thursday:
DC.B "Thursday",0
EVEN
Friday:
DC.B "Friday",0
EVEN
Saturday:
DC.B "Saturday",0
EVEN

;In this example, load Thursday.

LEA Days_Of_The_Week,A0 ;load base address of table into A0
MOVE.W #4,D0 ;Thursday's index
LSL.W #2,D0 ;multiply by 4 since each pointer is 32-bit
LEA (A0,D0),A1 ;load table offset by D0 into A1
MOVE.L (A1),A1 ;dereference the pointer, now the address of "Thursday" is in A1.
MOVE.B (A1)+,D1 ;Load the "T" of Thursday into D1, auto-increment to next letter for the next load.</lang>


=={{header|ACL2}}==
=={{header|ACL2}}==