0% found this document useful (0 votes)
4 views15 pages

Lecture Array vs. Pointers

The document compares two methods for clearing an array in memory using C and MIPS assembly: one with array indices and the other with pointers. The array version recalculates addresses within the loop, while the pointer version optimizes by moving calculations outside the loop, reducing instruction execution. The conclusion highlights the benefits of compiler optimizations such as strength reduction and induction variable elimination.

Uploaded by

adeenhassan7575
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Lecture Array vs. Pointers

The document compares two methods for clearing an array in memory using C and MIPS assembly: one with array indices and the other with pointers. The array version recalculates addresses within the loop, while the pointer version optimizes by moving calculations outside the loop, reducing instruction execution. The conclusion highlights the benefits of compiler optimizations such as strength reduction and induction variable elimination.

Uploaded by

adeenhassan7575
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Instructions: Language

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

• Array version must have the • Pointer version increments the


“multiply” and add inside the pointer p directly. The pointer version
loop because i is incremented moves the scaling shift and the array
bound addition outside the loop,
and each address must be thereby reducing the instructions
recalculated from the new index executed per iteration from 6 to 4
Comparing the Two Versions of Clear
• What is the conclusion?
• This manual optimization corresponds to the compiler optimization of
strength reduction (shift instead of multiply) and induction variable
elimination (eliminating array address calculations within loops).

You might also like