Jump to content

Array: Difference between revisions

2,815 bytes added ,  2 years ago
Line 68:
 
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 is to pad the ends of each shorter row with arbitrary bytes. This lets you select an arbitrary row with a simple bit shift applied to the index.
 
 
 
Iteration over the elements of an array is fairly straightforward.
Line 109 ⟶ 111:
;ARM Assembly
ldr r0,[r1,#4] ;load the 1st element of the array whose pointer to its 0th element is stored in r1, into r0.</lang>
 
====Encoding an Array's End====
=====Null Terminator=====
This method is most commonly used with strings. An ASCII value that is not associated with any keyboard key, typically 0, is placed at the end of a string. In a typical PrintString assembly routine, the routine is given a pointer to the 0th entry of the string as its only parameter. The routine reads from the pointer, prints that letter, increments the pointer, and repeats until the terminator is read, at which point the routine ends. A string variable in [[C]] will place a 0 at the end of a string without you having to define it yourself. This method works well for strings and other arrays where the terminator's value is not a possible value for actual data. On more general arrays where the entries represent non-ASCII data, this causes problems where you have a datum that just so happens to equal the terminator.
 
In cases like this, high-level languages often implement <i>escape characters</i> which when encountered in a string, result in a branch to a section that reads the next character without checking if it's a terminator or other control code. Effectively this removes the special meaning of that character but only if an escape character is before it. This concept is often reversed to allow the programmer to easily implement ASCII control characters, such as <code>\n<code> for new line (in ASCII this is represented by a 13 followed by a 10, for carriage return + line feed.) In this case the backslash signals to <code>printf()</code> that the next letter is associated with a particular ASCII control code. If the next character read is an "n" then ASCII 13 gets printed, followed by ASCII 10. After this, normal reading and printing resumes.
<lang asm>
HelloText: ;6502, z80, 8086
db "Hello World",0
 
HelloText:
dc.b "Hello World",0 ;68000
even ;aligns to the next 2 byte boundary if not already aligned.
 
HelloText
.byte "Hello World",0
.align 4 ;aligns to the next 4 byte boundary if not already aligned.</lang>
 
=====End Label=====
This method is best for pre-defined arrays. Its usage was shown in earlier examples. Most assemblers can create a constant based off simple arithmetic using labels. A label's numeric value is the address it gets assembled to, decided at assemble time. Placing another label immediately after the end of an array will point it to the next byte after that array. The assembler subtracts the array's beginning label from this value to create a size constant that you don't have to manually adjust if you change the number of entries the array has.
This method can be used with array variables in conjunction with a null terminator and padding the distance between the terminator and the end of the range dedicated to storing array variables.
 
===[[Fortran]]===
1,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.