Lecture Array vs. Pointers
Lecture Array vs. Pointers
of the Computer
Computer Organization and Assembly Language
Arrays versus Pointers
• This section shows C and MIPS assembly versions of two procedures to clear a
sequence of words in memory
• one using array indices
• one using pointers
• The purpose of this section is to show how pointers map into MIPS instructions
Array Version
move $t0,$zero # i = 0
loop1: sll $t1,$t0,2 # $t1 = i * 4
add $t2,$a0,$t1 # $t2 = address of array[i]
sw $zero, 0($t2) # array[i] = 0
addi $t0,$t0,1 # i = i + 1
slt $t3,$t0,$a1 # $t3 = (i < size)
bne t3,$zero,loop1 # if (i < size) go to loop1
Array Version
• In which registers the two parameters array[] and size are stored?
• In the registers $a0 and $a1
• Assume that int i is allocated to register $t0
• The initialization of i, the first part of the for loop, is straightforward:
• move $t0,$zero # i = 0 (register $t0 = 0)
Array Version
• To set array[i] to 0 we must first get its address. Start by multiplying i
by 4 to get the byte address:
• loop1: sll $t1,$t0,2 # $t1 = i * 4
• Since the starting address of the array is in a register, we must add it
to the index to get the address of array[i] using an add instruction:
• add $t2,$a0,$t1 # $t2 = address of array[i]
• Finally, we can store 0 in that address:
• sw $zero, 0($t2) # array[i] = 0
Array Version
• This instruction is the end of the body of the loop, so the next step is
to increment i:
• addi $t0,$t0,1 # i = i + 1
• The loop test checks if i is less than size:
• slt $t3,$t0,$a1 # $t3 = (i < size)
• bne $t3,$zero,loop1 # if (i < size) go to loop1
Array Version
move $t0,$zero # i = 0
loop1: sll $t1,$t0,2 # $t1 = i * 4
add $t2,$a0,$t1 # $t2 = address of array[i]
sw $zero, 0($t2 # array[i] = 0
addi $t0,$t0,1 # i = i + 1
slt $t3,$t0,$a1 # $t3 = (i < size)
bne t3,$zero,loop1 # if (i < size) go to loop1
Pointer Version
move $t0,$a0 # p = address of array[0]
loop2: sw zero,0($t0) # Memory[p] = 0
addi $t0,$t0,4 # p = p + 4
# $t1 = size * 4
sll $t1,$a1,2
# $t2 = address of array[size]
add $t2,$a0,$t1
# $t3 = (p<&array[size])
slt $t3,$t0,$t2 # if (p<&array[size]) go to
bne $t3,$zero,loop2 loop2
Pointer Version
• The two parameters array and size to the registers $a0 and $a1 and allocates
p to register $t0
• The code for the second procedure starts with assigning the pointer p to the
address of the first element of the array:
• move $t0,$a0 # p = address of array[0]
• The next code is the body of the for loop, which simply stores 0 into p:
• loop2: sw $zero,0($t0) # Memory[p] = 0
• This instruction implements the body of the loop, so the next code is the iteration
increment, which changes p to point to the next word:
• addi $t0,$t0,4 # p = p + 4
Pointer Version
• Incrementing a pointer by 1 means moving the pointer to the next
sequential object in C
• Since p is a pointer to integers, each of which uses 4 bytes, the compiler
increments p by 4.
• The loop test is next. The first step is calculating the address of the last
element of array. Start with multiplying size by 4 to get its byte address:
• sll $t1,$a1,2 # $t1 = size * 4
• and then we add the product to the starting address of the array to get the
address of the first word after the array:
• add $t2,$a0,$t1 # $t2 = address of array[size]
Pointer Version
• The loop test is simply to see if p is less than the last element of array:
• slt $t3,$t0,$t2 # $t3 = (p<&array[size])
• bne $t3,$zero,loop2 # if (p<&array[size]) go to
loop2
Pointer Version
move $t0,$a0 # p = address of array[0]
loop2: sw zero,0($t0) # Memory[p] = 0
addi $t0,$t0,4 # p = p + 4
# $t1 = size * 4
sll $t1,$a1,2
# $t2 = address of array[size]
add $t2,$a0,$t1
# $t3 = (p<&array[size])
slt $t3,$t0,$t2 # if (p<&array[size]) go to
bne $t3,$zero,loop2 loop2
Pointer Version
• Note that this program calculates the address of the end of the array in every
iteration of the loop, even though it does not change.
• A faster version of the code moves this calculation outside the loop:
move $t0,$a0 # p = address of array[0]
sll $t1,$a1,2 # $t1 = size * 4
add $t2,$a0,$t1 # $t2 = address of array[size]
loop2: sw $zero,0($t0) # Memory[p] = 0
addi $t0,$t0,4 # p = p + 4
slt $t3,$t0,$t2 # $t3 = (p<&array[size])
bne $t3,$zero,loop2 # if (p<&array[size]) go to loop2
Comparing the Two Versions of Clear