Array: Difference between revisions

563 bytes added ,  1 year ago
m
 
(2 intermediate revisions by the same user not shown)
Line 41:
* [[letter frequency]]
===[[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. 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 runsis stored on a disk or tape 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.
 
'''Example using [[6502 Assembly]]:'''
<lang 6502asm>ArrayRAM equ $00 ;the beginning of an array, stored in zero page RAM
;the beginning of an array, stored in zero page RAM
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</lang>
ArrayROM: db 0,5,10,15,20,25,30,35,40,45,50
;on Commodore 64 (for example) thisthese values can be RAMmodified at runtime, but on the NES orthey something similar it would beare read-only.</lang>
 
 
Line 70 ⟶ 72:
 
It is <b>much</b> easier to work with arrays in assembly if all rows are the same length. The best way to deal with a ragged or jagged array, such as an array of strings of different lengths, is to store ''pointers to the array entries rather than the intended entries themselves.'' This equalizes the size of all the elements of the array which makes it much easier to index. Another way to handle such arrays is with padding, by adding extra null bytes to the ends until all rows are the same length, but for many data types it's better to construct an array of pointers.
 
<lang 68000devpac>main:
;goal: Print "Orange" to stdout
LEA Strings,A0
LEA (4,A0),A0 ;this is NOT a dereference operation, it merely adds 4 to A0.
MOVE.L (A0),A0 ;now we dereference the pointer so that we have the address of "Orange" in A0.
 
;If we did MOVE.B (A0),D0 now, we'd load the "O" in "Orange" into D0.
 
JSR PrintString ;or whatever you use to write to stdout
RTS ;return
 
 
Strings:
DC.L AppleAddress
DC.L OrangeAddress
DC.L GrapeAddress
 
AppleAddress:
DC.B "Apple",0
even
OrangeAddress:
DC.B "Orange",0
even
GrapeAddress:
DC.B "Grape",0
even</lang>
 
 
In assembly, there is '''nothing''' stopping your program from indexing an array out of bounds! The computer doesn't have an understanding of where your array "ends," so asking it to return (for example) the 500th element of an array with only 25 elements is perfectly legal. This typically won't happen for arrays that aren't indexed based on user input, but be careful nonetheless. Indexing an array out of bounds and trying to read from it can cause segmentation faults on more modern machines, which is the operating system's way of preventing your code from reading memory that is outside of the program that is trying to index the array. Older CPUs don't have this sort of protection, and indexing out of bounds will read whatever is stored in memory at that location, which is why it falls into the realm of undefined behavior- the result entirely depends on how the code in your program is arranged.
1,489

edits