Enumerations: Difference between revisions

m (→‎{{header|68000 Assembly}}: added 68000 assembly and also fixed formatting and added clarification for 6502 Assembly)
Line 1,935:
def Apple=1, Banana=2, Cherry=4;
</lang>
 
=={{header|Z80 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.
 
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 z80>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.
 
<lang z80>align 8 ;aligns "Days_Of_The_Week" to the next 256-byte boundary. The low byte of "Sunday" will be at memory location &XX00.
;this simplifies the lookup process significantly.
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
 
;This example will load Friday.
ld hl,Days_Of_The_Week ;get base address of table. (Thanks to the align 8, we know that L = 0.)
ld a,5 ;0 = Sunday, 1 = Monday, ... 5 = Friday, 6 = Saturday
add a ;Multiply A by 2 (this is faster than SLA A. RLCA would have also worked here)
ld L,a ;since the table was page-aligned it is sufficient to load A directly into L to properly index the table.
ld e,(hl) ;get the low byte into E
inc hl ;increment HL to high byte
ld d,(hl) ;get the high byte into D
 
;now DE contains the pointer to "Friday"
ex de,hl ;my PrintString routine takes the pointer in HL as the argument so we need to swap DE with HL.
call PrintString ;prints a null-terminated string to the screen.
ret ;return to basic
</lang>
 
{{out}}
<pre>
Friday
</pre>
 
=={{header|zkl}}==
1,489

edits