Array: Difference between revisions

1,732 bytes added ,  2 years ago
Line 40:
==Examples==
* [[letter frequency]]
===[[Assembly]]===
An array is simply a sequence of values stored in memory. 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.
 
</lang 6502asm>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>
 
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.
 
 
<lang 68000devpac> LEA myArray,A0
MOVE.W #4*5*1,D1 ;five elements per row, so to get the 4th row we multiply the row number by the elements per row,
;times the number of bytes per element
LEA (A0,D1),A0
LEA (2*1,A0),A0 ;column number times the number of bytes per element (the times 1s aren't needed but it's here for clarity)
MOVE.B (A0),D0 ;load decimal 52 into D0
 
myArray:
DC.B 10,11,12,13,14
DC.B 20,21,22,23,24
DC.B 30,31,32,33,34
DC.B 40,41,42,43,44
DC.B 50,51,52,53,54</lang>
 
 
In assembly, there are no built-in safeguards for indexing out of bounds, using an index of the wrong size, etc. The computer has no built-in way of knowing where an array "ends." Higher-level languages often use a "null terminator" at the end of a one-dimensional array to mark the end. C uses this method for strings. Assembly won't do that for you!
 
 
===[[Fortran]]===
1,489

edits