Arrays: Difference between revisions

1,904 bytes added ,  1 year ago
m
→‎{{header|68000 Assembly}}: added constant array declaration
m (→‎{{header|68000 Assembly}}: added constant array declaration)
Line 208:
 
=={{header|68000 Assembly}}==
Creating an array is as simple as declaring its base address. Note that all bounds checking must be done by the programmer and is not built in by default. You also will need to have some sort of knowledge about what is stored nearby, so that you don't clobber it.
 
<lang 68000devpac>MOVE.L #$00100000,A0 ;define an array at memory address $100000</lang>
 
Defining an array in ROM (or RAM if you're making a program that is loaded from disk) is very simple:
The data type associated with the elements of an array is determined by the move operation used to assign elements to an array. The language does not prohibit you from storing 1-byte data into an array intended for 32-bit values.
<lang 68000devpac>;8-bit data
MyArray:
DC.B 1,2,3,4,5
DC.B 6,7,8,9,10
DC.B 11,12,13,14,15
EVEN ;needed to ensure proper alignment after a byte array with an odd number of entries.
 
;16-bit data
MyArrayW:
DC.W 1,2,3,4,5
DC.W 6,7,8,9,10
DC.W 11,12,13,14,15
 
;32-bit data
MyArrayL:
DC.L 1,2,3,4,5
DC.L 6,7,8,9,10
DC.L 11,12,13,14,15
</lang>
 
Strings are also arrays and most assemblers accept single or double quotes. The following are equivalent:
<lang 68000devpac>MyString:
DC.B "Hello World",0
even
 
MyString2:
DC.B 'H','e','l','l','o',' ','W','o','r','l','d',0
even</lang>
 
The assembler will automatically substitute each letter with its ASCII equivalent. Notice the lack of quotes around the null terminator 0. If it had quotes, it would be assembled as 0x30 instead of 0. Not good if your printing routine expects a 0 as the terminator byte.
 
The above declarations are useful as compile-time constants or mutable pre-loaded values. Whether an array is mutable or not depends solely on whether it is stored in ROM or RAM.
 
<i>Side note: Some systems, such as the Sega Genesis or other ROM cartridge-based computers, cannot use the above declaration to initialize an array in RAM at assemble time; only in ROM. While an array can be declared at any arbitrary RAM location on any system, you won't be able to define a data block in RAM the same way you would on an assembly program meant to run on the Macintosh or Commodore Amiga for example. The examples below will still work on any system, you just won't be able to "see" the array before running the program, if that makes sense. A simple alternative can be to define the array in ROM then copy it to RAM and work with it there.</i>
 
 
 
The data type associated with the elements of an array is determined by the move operation used to assign elements to an array. The language does not prohibit you from storing 18-bytebit data into an array intended for 32-bit values.
 
The base address can be offset by the value in a data register, to allow for assigning values to an array. The offset is always measured in bytes, so if your array is intended to contain a larger data size you will need to adjust it accordingly.
1,489

edits