Pointers and references: Difference between revisions

m
→‎{{header|68000 Assembly}}: used code tags for better style
(→‎Double Pointers: Fixed incorrect example - double pointers can only work from zero page memory)
m (→‎{{header|68000 Assembly}}: used code tags for better style)
Line 120:
dc.b $80,$81,$82,$83</lang>
 
The opcode <lang 68000devpaccode>LEA</langcode> instruction can load the address of a given label.
This isn't needed for values in RAM, but it is needed for loading from data tables in ROM.
 
<lang 68000devpac>LEA myData,A0 ;address of myData is stored in A0</lang>
 
The address registers <code>A0-</code> through <code>A6</code> hold 24-bit addresses. While they can contain 32 bits of data, the top byte is ignored.
 
68000 Assembly doesn't care about what type of data is being pointed to. It makes no distinction between bytes, words, longs, or executable code. That is up to the opcodes that interact with the data. For the following, assume that the address "<code>myData"</code> shown above is is loaded into <code>A0</code>, and that <code>D0</code> equaled 0x00000000 prior to the code below.
 
<lang 68000devpac>MOVE.B (A0),D0 ;D0 = 0x00000080
Line 133:
MOVE.L (A0),D0 ;D0 = 0x80818283</lang>
 
Putting a + after <code>(A0)</code> auto-increments the pointer by 1,2,or 4 for <code>MOVE.B</code>, <code>MOVE.W</code>, and <code>MOVE.L</code> respectively. The increment happens after the move.
 
<lang 68000devpac>LEA myData,A0 ;Assume for this example that data registers all equal 0.
Line 141:
 
 
Putting a - before <code>(A0)</code> pre-decrements the pointer by 1,2, or 4 for <code>MOVE.B</code>, <code>MOVE.W</code>, and <code>MOVE.L</code> respectively, ''before the move takes place.''
<lang 68000devpac>LEA myData+4,A0 ;Assume for this example that data registers all equal 0.
MOVE.B -(A0),D0 ;D0 = 00000083
Line 147:
MOVE.B -(A0),D2 ;D2 = 00000081</lang>
 
<code>LEA</code> can also be used to pre-calculate a complex offset to an address.
<lang 68000devpac>LEA myData,A0
MOVE.W #$0C,D0
LEA (4,A0,D0),A3</lang> ;A3 = address of myData + 4 + $0C
 
An address can be offset by an immediate value, a register, or both. Data register offsets are measured at 16 bit word length. Although the second <code>LEA</code> is in brackets, it does NOT dereference the pointer. Let's look at the following example data:
 
<lang 68000devpac>myPointers: dc.l myData, myData2
Line 173:
<lang 68000devpac>MOVE.L #$FFEEDDCC,myVar</lang>
 
There is nothing stopping you from clobbering nearby stored data. In the above example, the byte FF gets written to $100000, EE to $100001, DD to $100002, and CC to $100003. If you intended myVar to be of byte length, you need to make sure you only use MOVE.B to read/write to/write from myVar.
 
=={{header|Ada}}==
1,489

edits