Skip to content
UoL CS Notes

Variable Addressing & Arrays

COMP124 Lectures

Every variable is just a location in memory at which a value is stored. Pointers will refer to the value of a memory location but sometimes we want it’s address.

Variable Addresses in Assembly

In assembly language we can get the address of a variable with the LEA (load effective address) instruction.

We often use the base register, ebx:

lea ebx, val

We can access the value pointed to by using the register indirect addressing mode:

mov eax, [ebx]

Using square brackets will treat the value as an address and fetch the value from that address.

Arrays

An array is just a sequence of values, stored consecutively in memory.

Array Storage

Different data types many occupy different amounts of memory.

If a grades[5] array is stored at memory address 1000 onwards, then:

  • grades[0] is at 1000
  • grades[1] is at 1004
  • grades[2] is at 1008
  • grades[3] is at 10016

Each value is 4 bytes as we are storing an int which is 4 bytes long in a 32 bit system.

We can use this to develop assembly code that accesses arrays.

Example

/* Sum the elements of an array. */
...
int my array[5]; 	// declaration of an array of integers
myarray[0] = 1;	// initialise the array
myarray[1] = 3;
myarray[2] = 5;
myarray[3] = 7;
myarray[4] = 9;

_asm {
	...
		lea ebx, myarray	// addr of array (0th element) in ebx
		mov ecx, 5	// size of the array in ecx (for Loop1)
		mov eax, 0	// initialise sum to 0
	Loop1:	add eax, [ebx]	// get element pointed to by ebx
		add ebx, 4	// to pint to next integer element
		loop Loop1	// go round again
	...
}
...