CS255 Syllabus

### Operations on array variables

• The address of an array element

• Important facts:

• The computer can only operate on one array element

 The computer connot operate on the entire array at once !!!

• An address in M68000 is always a 32 bit value

Therefore:

 The address of an array element is always 32 bits (= a long word in M68000)

• Special emphasis on address (of a variable or array element):

• An address is used to identify a memory location (e.g., where a variable is stored)

• An address is just like a social security number is used to identify a person in the US

A social security number is always 9 digits:

 Leading zeroes in a social security number is significant !!!

• An address in M68000 program is always 32 bits long (because the M68000 CPU can accommodate 232 different memory locations)

 Just like a social security number is is always 9 decimal digits long (because the US can accommodate 109 different individuals)

• Therefore, we must use .l in:

 move.l #B, A0

because the address value #B is always a 32 bits value !!!

• The array element variable

• Fact:

• The data type of an array element can be different for different arrays

Example:

 int B[10]; short C[10];

Important note:

 You must use the correct modifier (.b, .w or .l) of an M68000 instruction to operate on an array element (The address of an array element is always 32 bits --- you must always use .l to operate on an address !!!)

• Example 1: adding integer array elements

 int B[10] (We assumed array B have been defined) B[4] = B[3] + B[7]; // Each array element is an int !!! In assembler code: movea.l #B,A0 * A0 = base address of array B move.l 12(A0),D0 * Get the int type variable B[3] in D0 move.l 28(A0),D1 * Get the int type variable B[7] in D1 add.l D0,D1 * Add the two int type values together * The sum is an int and is stored in D1 move.l D1,16(A0) * Store the int type value into B[4]

• Example 2: adding short array elements

 short B[10]; (We assumed array B have been defined) B[4] = B[3] + B[7]; // Each array element is a short !!! In assembler code: movea.l #B,A0 * A0 = base address of array B move.w 6(A0),D0 * Get the short type variable B[3] in D0 move.w 14(A0),D1 * Get the short type variable B[7] in D1 add.w D0,D1 * Add the two short type values together * The sum is a short and is stored in D1 move.w D1,8(A0) * Store the short type value into B[4]

Notice that:

• Each int value will occupy 4 bytes of memory space, therefore:

 If   B[ ] is an int typed array, the offset of the variable B[k] is: 4 × k

• Each short value will occupy 2 bytes of memory space, therefore:

 If   B[ ] is a short typed array, the offset of the variable B[k] is: 2 × k