Array: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) |
Puppydrum64 (talk | contribs) m (→Assembly) |
||
Line 65: | Line 65: | ||
DC.B 50,51,52,53,54</lang> |
DC.B 50,51,52,53,54</lang> |
||
Iteration over the elements of an array is fairly straightforward. |
|||
<lang 68000devpac> |
|||
LEA myArray,A0 |
|||
loop: |
|||
MOVE.B (A0)+,D0 |
|||
JMP loop</lang> |
|||
Skipping elements can be easily done with a "dummy read," whereby an auto-incrementing/decrementing addressing mode is used solely for updating the pointer, or by incrementing/decrementing a loop counter multiple times per loop. |
|||
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! |
|||
<lang asm>;8086 Assembly example |
|||
iterate: |
|||
movsb ;store [ds:si] into [es:di], increment both pointers, and decrement cx. |
|||
lodsb ;dummy read to increment the pointer and decrement cx. The value loaded into AL gets discarded. |
|||
inc di ;increment destination index |
|||
jcxz exitloop ;exit loop if cx equals zero. |
|||
jmp iterate</lang> |
|||
Implementation of a reverse array slice such as <code>a[100:0:-2]</code> example for [[Python]] is much more difficult. First of all, computers cannot implicitly understand the concept of the "end" of an array. The start of an array is easy for a computer to grasp, it is simply a pointer to its first element. However, encoding an array's end point requires some form of metadata, such as a null terminator or an additional variable representing the array's maximum size. High-level languages typically handle this automatically. |
|||
===[[Fortran]]=== |
===[[Fortran]]=== |