Pointers and references: Difference between revisions

68000 Assembly
No edit summary
(68000 Assembly)
Line 1:
{{Task|Basic Data Operations}}{{basic data operation}}In this task, the goal is to demonstrate common operations on pointers and references. These examples show pointer operations on the stack, which can be dangerous and is rarely done. Pointers and references are commonly used along with [[Memory allocation]] on the [[heap]].
=={{header|68000 Assembly}}==
===Pointers===
 
In most assemblers, a line of code or a value in work RAM can be given a label.
 
<lang 68000devpac> myVar equ $100000 ; a section of user ram given a label
myData: ; a data table given a label
dc.b $80,$81,$82,$83</lang>
 
The opcode <lang 68000devpac>LEA</lang> 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 A0-A6 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 "myData" shown above is is loaded into A0, and that D0 equaled 0x00000000 prior to the code below.
 
<lang 68000devpac>MOVE.B (A0),D0 ;D0 = 0x00000080
MOVE.W (A0),D0 ;D0 = 0x00008081
MOVE.L (A0),D0 ;D0 = 0x80818283</lang>
 
Putting a + after (A0) auto-increments the pointer by 1,2,or 4 for MOVE.B, MOVE.W, and MOVE.L respectively. The increment happens after the move.
 
Putting a - before (A0) pre-decrements the pointer by 1,2, or 4 for MOVE.B, MOVE.W, and MOVE.L respectively, before the move takes place.
 
===References===
 
Labeled memory can be read/written to directly, unlike in ARM Assembly.
<lang 68000devpac>MOVE.B #$10,myVar</lang>
 
Unlike in C, pointers do not have to point to any specific type of data.
<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/from myVar
 
 
=={{header|Ada}}==
1,489

edits