Enumerations: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) |
|||
Line 11: | Line 11: | ||
CONSTANT |
CONSTANT |
||
TEST_CATEGORY = 10</lang> |
TEST_CATEGORY = 10</lang> |
||
=={{header|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. |
|||
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 6502asm>Sunday equ 0 |
|||
Monday equ 1 |
|||
Tuesday equ 2 |
|||
Wednesday equ 3 |
|||
Thursday equ 4 |
|||
Friday equ 5 |
|||
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: |
|||
<lang 6502asm>enum $0400 |
|||
OBJECT_XPOS .dsb 16 ;define 16 bytes for object X position |
|||
OBJECT_YPOS .dsb 16 ;define 16 bytes for object Y position |
|||
ende</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. |
|||
<lang 6502asm> |
|||
Days_Of_The_Week: |
|||
word Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday |
|||
Sunday: |
|||
byte "Sunday",0 |
|||
Monday: |
|||
byte "Monday,0 |
|||
Tuesday: |
|||
byte "Tuesday",0 |
|||
Wednesday: |
|||
byte "Wednesday",0 |
|||
Thursday: |
|||
byte "Thursday",0 |
|||
Friday: |
|||
byte "Friday",0 |
|||
Saturday: |
|||
byte "Saturday",0 |
|||
LDA #$03 ;we want to load Wednesday |
|||
ASL A ;these are 16-bit pointers to strings, so double A |
|||
TAX ;transfer A to X so that we can use this index as a lookup |
|||
LDA Days_Of_The_Week,x ;get low byte |
|||
STA $00 ;store in zero page memory |
|||
LDA Days_Of_The_Week+1,x ;get high byte |
|||
STA $01 ;store in zero page memory directly after low byte |
|||
LDY #0 ;clear Y |
|||
LDA ($00),Y ;Load the "W" of Wednesday into accumulator</lang> |
|||
=={{header|ACL2}}== |
=={{header|ACL2}}== |