Array: Difference between revisions

Content added Content deleted
m (→‎Assembly: clarification on rom/ram arrays)
Line 41: Line 41:
* [[letter frequency]]
* [[letter frequency]]
===[[Assembly]]===
===[[Assembly]]===
An array is simply a sequence of values stored in consecutive memory locations. Its beginning is typically defined with some sort of label that points to the address where that array is stored. Arrays are mutable unless they are stored in ROM, such as on a video game cartridge.
An array is simply a sequence of values stored in consecutive memory locations. Its beginning is typically defined with some sort of label that points to the address where that array is stored. Whether an array is mutable or immutable depends on the hardware; in older assembly languages, an array is only typically immutable if it's stored in ROM. Home computer software that runs on a disk can define an array at compile time that can be mutable; ROM cartridge programs cannot. The syntax is the same for both, however. ROM cartridge programs will need to construct their array in ROM, copy it to RAM, and alter the copy.


<lang 6502asm>;6502 Assembly example
<lang 6502asm>;6502 Assembly example
ArrayRAM equ $00 ;the beginning of an array, stored in zero page RAM
ArrayRAM equ $00 ;the beginning of an array, stored in zero page RAM
ArrayROM: db 0,5,10,15,20,25,30,35,40,45,50 ;an array stored in ROM</lang>
ArrayROM: db 0,5,10,15,20,25,30,35,40,45,50
;on Commodore 64 (for example) this can be RAM but on the NES or something similar it would be read-only


Almost all assembly languages have a method of loading from a memory address offset by some sort of variable amount. That offset is the index into the array. Depending on the size of each element that index is multiplied by the number of bytes each element takes up. What constitutes an "element," "row," or "column" of the array is entirely decided by the programmer. Arrays in assembly are always zero-indexed.
Almost all assembly languages have a method of loading from a memory address offset by some sort of variable amount. That offset is the index into the array. Depending on the size of each element that index is multiplied by the number of bytes each element takes up. What constitutes an "element," "row," or "column" of the array is entirely decided by the programmer. Arrays in assembly are always zero-indexed.